14fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner//===- Reassociate.cpp - Reassociate binary expressions -------------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
94fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner//
104fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// This pass reassociates commutative expressions in an order that is designed
119046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner// to promote better constant propagation, GCSE, LICM, PRE, etc.
124fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner//
134fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// For example: 4 + (x + 5) -> x + (4 + 5)
144fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner//
154fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// In the implementation of this algorithm, constants are assigned rank = 0,
164fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// function arguments are rank = 1, and other values are assigned ranks
174fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// corresponding to the reverse post order traversal of current function
184fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// (starting at 2), which effectively gives values in deep loops higher rank
194fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// than values not in loops.
204fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner//
214fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner//===----------------------------------------------------------------------===//
224fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
2308b43921e18f314c4fd38049291d323830934c36Chris Lattner#define DEBUG_TYPE "reassociate"
244fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner#include "llvm/Transforms/Scalar.h"
25fa0e6facc793d0a67e89f873e18cd35a9d7c02e0Dan Gohman#include "llvm/Transforms/Utils/Local.h"
260975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner#include "llvm/Constants.h"
27ae74f555522298bef3be8a173163bf778d59adf9Chris Lattner#include "llvm/DerivedTypes.h"
284fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner#include "llvm/Function.h"
2906cb8ed00696eb14d1b831921452e50ec0568ea2Chandler Carruth#include "llvm/IRBuilder.h"
30d8e1eea678833cc2b15e4ea69a5a403ba9c3b013Misha Brukman#include "llvm/Instructions.h"
3103afd02ca2486aebb3b29edd2f77920d4e5020fdDale Johannesen#include "llvm/IntrinsicInst.h"
324fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner#include "llvm/Pass.h"
3306cb8ed00696eb14d1b831921452e50ec0568ea2Chandler Carruth#include "llvm/ADT/DenseMap.h"
3406cb8ed00696eb14d1b831921452e50ec0568ea2Chandler Carruth#include "llvm/ADT/PostOrderIterator.h"
3506cb8ed00696eb14d1b831921452e50ec0568ea2Chandler Carruth#include "llvm/ADT/STLExtras.h"
3606cb8ed00696eb14d1b831921452e50ec0568ea2Chandler Carruth#include "llvm/ADT/SetVector.h"
3706cb8ed00696eb14d1b831921452e50ec0568ea2Chandler Carruth#include "llvm/ADT/Statistic.h"
38c9fd097a01383323f166c14c17d3984620cad766Chris Lattner#include "llvm/Assembly/Writer.h"
394fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner#include "llvm/Support/CFG.h"
40551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h"
41d3c7b7359d4992b9ab9f8e12ccd0a9b7d2446566Chris Lattner#include "llvm/Support/ValueHandle.h"
42bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner#include "llvm/Support/raw_ostream.h"
43c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner#include <algorithm>
44d7456026629fc1760a45e6e955e9834246493147Chris Lattnerusing namespace llvm;
45d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
460e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumChanged, "Number of insts reassociated");
470e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumAnnihil, "Number of expr tree annihilated");
480e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumFactor , "Number of multiplies factored");
49a92f696b74a99325026ebbdbffd2a44317e0c10bChris Lattner
500e5f499638c8d277b9dc4a4385712177c53b5681Chris Lattnernamespace {
513e8b6631e67e01e4960a7ba4668a50c596607473Chris Lattner  struct ValueEntry {
52c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner    unsigned Rank;
53c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner    Value *Op;
54c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner    ValueEntry(unsigned R, Value *O) : Rank(R), Op(O) {}
55c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner  };
56c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner  inline bool operator<(const ValueEntry &LHS, const ValueEntry &RHS) {
57c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner    return LHS.Rank > RHS.Rank;   // Sort so that highest rank goes to start.
58c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner  }
59e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner}
60c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner
6150cacb2a520b93530e79220a307c907163b9e370Devang Patel#ifndef NDEBUG
62e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// PrintOps - Print out the expression identified in the Ops list.
63e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner///
649f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattnerstatic void PrintOps(Instruction *I, const SmallVectorImpl<ValueEntry> &Ops) {
65e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  Module *M = I->getParent()->getParent()->getParent();
66a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene  dbgs() << Instruction::getOpcodeName(I->getOpcode()) << " "
671befe643b2a030f5e2433ce0034a27fb65b5f26bChris Lattner       << *Ops[0].Op->getType() << '\t';
687de3b5db26bb3c8dcca5348fb7c0be4f9bd1bcb7Chris Lattner  for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
69a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene    dbgs() << "[ ";
70a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene    WriteAsOperand(dbgs(), Ops[i].Op, false, M);
71a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene    dbgs() << ", #" << Ops[i].Rank << "] ";
727de3b5db26bb3c8dcca5348fb7c0be4f9bd1bcb7Chris Lattner  }
73e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner}
7459500c8f9a76b3386329b6f837255c16f4e8b61bDevang Patel#endif
75e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
76844731a7f1909f55935e3514c9e713a62d67662eDan Gohmannamespace {
77464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  /// \brief Utility class representing a base and exponent pair which form one
78464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  /// factor of some product.
79464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  struct Factor {
80464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    Value *Base;
81464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    unsigned Power;
82464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
83464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    Factor(Value *Base, unsigned Power) : Base(Base), Power(Power) {}
84464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
85464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    /// \brief Sort factors by their Base.
86464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    struct BaseSorter {
87464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      bool operator()(const Factor &LHS, const Factor &RHS) {
88464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth        return LHS.Base < RHS.Base;
89464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      }
90464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    };
91464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
92464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    /// \brief Compare factors for equal bases.
93464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    struct BaseEqual {
94464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      bool operator()(const Factor &LHS, const Factor &RHS) {
95464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth        return LHS.Base == RHS.Base;
96464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      }
97464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    };
98464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
99464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    /// \brief Sort factors in descending order by their power.
100464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    struct PowerDescendingSorter {
101464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      bool operator()(const Factor &LHS, const Factor &RHS) {
102464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth        return LHS.Power > RHS.Power;
103464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      }
104464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    };
105464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
106464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    /// \brief Compare factors for equal powers.
107464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    struct PowerEqual {
108464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      bool operator()(const Factor &LHS, const Factor &RHS) {
109464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth        return LHS.Power == RHS.Power;
110464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      }
111464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    };
112464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  };
113464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth}
114464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
115464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruthnamespace {
1163e8b6631e67e01e4960a7ba4668a50c596607473Chris Lattner  class Reassociate : public FunctionPass {
117f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner    DenseMap<BasicBlock*, unsigned> RankMap;
118f1d0f7781e766df878bec4e7977fa3204374f394Craig Topper    DenseMap<AssertingVH<Value>, unsigned> ValueRankMap;
119841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    SetVector<AssertingVH<Instruction> > RedoInsts;
120c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner    bool MadeChange;
1214fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner  public:
122ecd94c804a563f2a86572dcf1d2e81f397e19daaNick Lewycky    static char ID; // Pass identification, replacement for typeid
123081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    Reassociate() : FunctionPass(ID) {
124081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson      initializeReassociatePass(*PassRegistry::getPassRegistry());
125081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    }
126794fd75c67a2cdc128d67342c6d88a504d186896Devang Patel
1277e70829632f82de15db187845666aaca6e04b792Chris Lattner    bool runOnFunction(Function &F);
1284fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
1294fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
130cb2610ea037a17115ef3a01a6bdaab4e3cfdca27Chris Lattner      AU.setPreservesCFG();
1314fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner    }
1324fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner  private:
1337e70829632f82de15db187845666aaca6e04b792Chris Lattner    void BuildRankMap(Function &F);
1344fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner    unsigned getRank(Value *V);
135cd117f736c47947af5c6549734549e135e626c5cDuncan Sands    void ReassociateExpression(BinaryOperator *I);
1360fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    void RewriteExprTree(BinaryOperator *I, SmallVectorImpl<ValueEntry> &Ops);
1379f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner    Value *OptimizeExpression(BinaryOperator *I,
1389f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner                              SmallVectorImpl<ValueEntry> &Ops);
1399f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner    Value *OptimizeAdd(Instruction *I, SmallVectorImpl<ValueEntry> &Ops);
140464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    bool collectMultiplyFactors(SmallVectorImpl<ValueEntry> &Ops,
141464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                                SmallVectorImpl<Factor> &Factors);
142464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    Value *buildMinimalMultiplyDAG(IRBuilder<> &Builder,
143464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                                   SmallVectorImpl<Factor> &Factors);
144464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    Value *OptimizeMul(BinaryOperator *I, SmallVectorImpl<ValueEntry> &Ops);
145e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner    Value *RemoveFactorFromExpression(Value *V, Value *Factor);
146841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    void EraseInst(Instruction *I);
147841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    void OptimizeInst(Instruction *I);
1484fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner  };
1494fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner}
1504fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
151844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar Reassociate::ID = 0;
152d13db2c59cc94162d6cf0a04187d408bfef6d4a7Owen AndersonINITIALIZE_PASS(Reassociate, "reassociate",
153ce665bd2e2b581ab0858d1afe359192bac96b868Owen Anderson                "Reassociate expressions", false, false)
154844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
155d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke// Public interface to the Reassociate pass
156d7456026629fc1760a45e6e955e9834246493147Chris LattnerFunctionPass *llvm::createReassociatePass() { return new Reassociate(); }
1574fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
1580fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// isReassociableOp - Return true if V is an instruction of the specified
1590fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// opcode and if it only has one use.
1600fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sandsstatic BinaryOperator *isReassociableOp(Value *V, unsigned Opcode) {
1610fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  if (V->hasOneUse() && isa<Instruction>(V) &&
1620fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      cast<Instruction>(V)->getOpcode() == Opcode)
1630fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    return cast<BinaryOperator>(V);
1640fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  return 0;
1650fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands}
1660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
1679c723199384b16899831937e2800d52f4f953569Chris Lattnerstatic bool isUnmovableInstruction(Instruction *I) {
1689c723199384b16899831937e2800d52f4f953569Chris Lattner  if (I->getOpcode() == Instruction::PHI ||
16998bda3dfefcb08e6ce81fa9545b05eb433cd5b87Bill Wendling      I->getOpcode() == Instruction::LandingPad ||
1709c723199384b16899831937e2800d52f4f953569Chris Lattner      I->getOpcode() == Instruction::Alloca ||
1719c723199384b16899831937e2800d52f4f953569Chris Lattner      I->getOpcode() == Instruction::Load ||
1729c723199384b16899831937e2800d52f4f953569Chris Lattner      I->getOpcode() == Instruction::Invoke ||
17303afd02ca2486aebb3b29edd2f77920d4e5020fdDale Johannesen      (I->getOpcode() == Instruction::Call &&
17403afd02ca2486aebb3b29edd2f77920d4e5020fdDale Johannesen       !isa<DbgInfoIntrinsic>(I)) ||
175e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling      I->getOpcode() == Instruction::UDiv ||
1761628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer      I->getOpcode() == Instruction::SDiv ||
1771628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer      I->getOpcode() == Instruction::FDiv ||
1780a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer      I->getOpcode() == Instruction::URem ||
1790a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer      I->getOpcode() == Instruction::SRem ||
1800a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer      I->getOpcode() == Instruction::FRem)
1819c723199384b16899831937e2800d52f4f953569Chris Lattner    return true;
1829c723199384b16899831937e2800d52f4f953569Chris Lattner  return false;
1839c723199384b16899831937e2800d52f4f953569Chris Lattner}
1849c723199384b16899831937e2800d52f4f953569Chris Lattner
1857e70829632f82de15db187845666aaca6e04b792Chris Lattnervoid Reassociate::BuildRankMap(Function &F) {
1866007cb6c4d923e2dee4a1133fb6d1bb00a37062dChris Lattner  unsigned i = 2;
187fb5be090f59997deb7a2e89c92bac19528ba6755Chris Lattner
188fb5be090f59997deb7a2e89c92bac19528ba6755Chris Lattner  // Assign distinct ranks to function arguments
189e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner  for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
190d3c7b7359d4992b9ab9f8e12ccd0a9b7d2446566Chris Lattner    ValueRankMap[&*I] = ++i;
191fb5be090f59997deb7a2e89c92bac19528ba6755Chris Lattner
1927e70829632f82de15db187845666aaca6e04b792Chris Lattner  ReversePostOrderTraversal<Function*> RPOT(&F);
1934fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner  for (ReversePostOrderTraversal<Function*>::rpo_iterator I = RPOT.begin(),
1949c723199384b16899831937e2800d52f4f953569Chris Lattner         E = RPOT.end(); I != E; ++I) {
1959c723199384b16899831937e2800d52f4f953569Chris Lattner    BasicBlock *BB = *I;
1969c723199384b16899831937e2800d52f4f953569Chris Lattner    unsigned BBRank = RankMap[BB] = ++i << 16;
1979c723199384b16899831937e2800d52f4f953569Chris Lattner
1989c723199384b16899831937e2800d52f4f953569Chris Lattner    // Walk the basic block, adding precomputed ranks for any instructions that
1999c723199384b16899831937e2800d52f4f953569Chris Lattner    // we cannot move.  This ensures that the ranks for these instructions are
2009c723199384b16899831937e2800d52f4f953569Chris Lattner    // all different in the block.
2019c723199384b16899831937e2800d52f4f953569Chris Lattner    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
2029c723199384b16899831937e2800d52f4f953569Chris Lattner      if (isUnmovableInstruction(I))
203d3c7b7359d4992b9ab9f8e12ccd0a9b7d2446566Chris Lattner        ValueRankMap[&*I] = ++BBRank;
2049c723199384b16899831937e2800d52f4f953569Chris Lattner  }
2054fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner}
2064fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
2074fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattnerunsigned Reassociate::getRank(Value *V) {
20808b43921e18f314c4fd38049291d323830934c36Chris Lattner  Instruction *I = dyn_cast<Instruction>(V);
209f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner  if (I == 0) {
210f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner    if (isa<Argument>(V)) return ValueRankMap[V];   // Function argument.
211f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner    return 0;  // Otherwise it's a global or constant, rank 0.
212f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner  }
2134fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
214f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner  if (unsigned Rank = ValueRankMap[I])
215f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner    return Rank;    // Rank already known?
21600b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen
21708b43921e18f314c4fd38049291d323830934c36Chris Lattner  // If this is an expression, return the 1+MAX(rank(LHS), rank(RHS)) so that
21808b43921e18f314c4fd38049291d323830934c36Chris Lattner  // we can reassociate expressions for code motion!  Since we do not recurse
21908b43921e18f314c4fd38049291d323830934c36Chris Lattner  // for PHI nodes, we cannot have infinite recursion here, because there
22008b43921e18f314c4fd38049291d323830934c36Chris Lattner  // cannot be loops in the value graph that do not go through PHI nodes.
22108b43921e18f314c4fd38049291d323830934c36Chris Lattner  unsigned Rank = 0, MaxRank = RankMap[I->getParent()];
22208b43921e18f314c4fd38049291d323830934c36Chris Lattner  for (unsigned i = 0, e = I->getNumOperands();
22308b43921e18f314c4fd38049291d323830934c36Chris Lattner       i != e && Rank != MaxRank; ++i)
22408b43921e18f314c4fd38049291d323830934c36Chris Lattner    Rank = std::max(Rank, getRank(I->getOperand(i)));
22500b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen
226cc8a2b98f28c10d93f45489b8c6f0c8b8205bb3bChris Lattner  // If this is a not or neg instruction, do not count it for rank.  This
227cc8a2b98f28c10d93f45489b8c6f0c8b8205bb3bChris Lattner  // assures us that X and ~X will have the same rank.
228b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands  if (!I->getType()->isIntegerTy() ||
229fa82b6eba4e1584d7dba291c28fe908272e1e002Owen Anderson      (!BinaryOperator::isNot(I) && !BinaryOperator::isNeg(I)))
230cc8a2b98f28c10d93f45489b8c6f0c8b8205bb3bChris Lattner    ++Rank;
231cc8a2b98f28c10d93f45489b8c6f0c8b8205bb3bChris Lattner
232a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene  //DEBUG(dbgs() << "Calculated Rank[" << V->getName() << "] = "
233bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner  //     << Rank << "\n");
23400b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen
235f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner  return ValueRankMap[I] = Rank;
2364fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner}
2374fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
238f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner/// LowerNegateToMultiply - Replace 0-X with X*-1.
239f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner///
240841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsstatic BinaryOperator *LowerNegateToMultiply(Instruction *Neg) {
241a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson  Constant *Cst = Constant::getAllOnesValue(Neg->getType());
242f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner
2430fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  BinaryOperator *Res =
2440fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    BinaryOperator::CreateMul(Neg->getOperand(1), Cst, "",Neg);
245841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Neg->setOperand(1, Constant::getNullValue(Neg->getType())); // Drop use of op.
2466934a04a8c15e9971cd1ea4d5c8df2d7afdd5be5Chris Lattner  Res->takeName(Neg);
247f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner  Neg->replaceAllUsesWith(Res);
2485367b23f76e75ebb680956575346fa8c3d56780fDevang Patel  Res->setDebugLoc(Neg->getDebugLoc());
249f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner  return Res;
250f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner}
251f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner
252c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// CarmichaelShift - Returns k such that lambda(2^Bitwidth) = 2^k, where lambda
253c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// is the Carmichael function. This means that x^(2^k) === 1 mod 2^Bitwidth for
254c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// every odd x, i.e. x^(2^k) = 1 for every odd x in Bitwidth-bit arithmetic.
255c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// Note that 0 <= k < Bitwidth, and if Bitwidth > 3 then x^(2^k) = 0 for every
256c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// even x in Bitwidth-bit arithmetic.
257c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandsstatic unsigned CarmichaelShift(unsigned Bitwidth) {
258c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (Bitwidth < 3)
259c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    return Bitwidth - 1;
260c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  return Bitwidth - 2;
261c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands}
262c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
263c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// IncorporateWeight - Add the extra weight 'RHS' to the existing weight 'LHS',
264c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// reducing the combined weight using any special properties of the operation.
265c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// The existing weight LHS represents the computation X op X op ... op X where
266c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// X occurs LHS times.  The combined weight represents  X op X op ... op X with
267c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// X occurring LHS + RHS times.  If op is "Xor" for example then the combined
268c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// operation is equivalent to X if LHS + RHS is odd, or 0 if LHS + RHS is even;
269c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// the routine returns 1 in LHS in the first case, and 0 in LHS in the second.
270c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandsstatic void IncorporateWeight(APInt &LHS, const APInt &RHS, unsigned Opcode) {
271c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // If we were working with infinite precision arithmetic then the combined
272c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // weight would be LHS + RHS.  But we are using finite precision arithmetic,
273c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // and the APInt sum LHS + RHS may not be correct if it wraps (it is correct
274c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // for nilpotent operations and addition, but not for idempotent operations
275c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // and multiplication), so it is important to correctly reduce the combined
276c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // weight back into range if wrapping would be wrong.
277c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
278c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // If RHS is zero then the weight didn't change.
279c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (RHS.isMinValue())
280c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    return;
281c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // If LHS is zero then the combined weight is RHS.
282c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (LHS.isMinValue()) {
283c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    LHS = RHS;
284c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    return;
285c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
286c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // From this point on we know that neither LHS nor RHS is zero.
287c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
288c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (Instruction::isIdempotent(Opcode)) {
289c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // Idempotent means X op X === X, so any non-zero weight is equivalent to a
290c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // weight of 1.  Keeping weights at zero or one also means that wrapping is
291c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // not a problem.
292c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    assert(LHS == 1 && RHS == 1 && "Weights not reduced!");
293c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    return; // Return a weight of 1.
294c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
295c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (Instruction::isNilpotent(Opcode)) {
296c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // Nilpotent means X op X === 0, so reduce weights modulo 2.
297c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    assert(LHS == 1 && RHS == 1 && "Weights not reduced!");
298c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    LHS = 0; // 1 + 1 === 0 modulo 2.
299c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    return;
300c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
301c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (Opcode == Instruction::Add) {
302c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // TODO: Reduce the weight by exploiting nsw/nuw?
303c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    LHS += RHS;
304c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    return;
305c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
306c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
307c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  assert(Opcode == Instruction::Mul && "Unknown associative operation!");
308c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  unsigned Bitwidth = LHS.getBitWidth();
309c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // If CM is the Carmichael number then a weight W satisfying W >= CM+Bitwidth
310c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // can be replaced with W-CM.  That's because x^W=x^(W-CM) for every Bitwidth
311c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // bit number x, since either x is odd in which case x^CM = 1, or x is even in
312c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // which case both x^W and x^(W - CM) are zero.  By subtracting off multiples
313c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // of CM like this weights can always be reduced to the range [0, CM+Bitwidth)
314c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // which by a happy accident means that they can always be represented using
315c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // Bitwidth bits.
316c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // TODO: Reduce the weight by exploiting nsw/nuw?  (Could do much better than
317c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // the Carmichael number).
318c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (Bitwidth > 3) {
319c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    /// CM - The value of Carmichael's lambda function.
320c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    APInt CM = APInt::getOneBitSet(Bitwidth, CarmichaelShift(Bitwidth));
321c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // Any weight W >= Threshold can be replaced with W - CM.
322c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    APInt Threshold = CM + Bitwidth;
323c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    assert(LHS.ult(Threshold) && RHS.ult(Threshold) && "Weights not reduced!");
324c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // For Bitwidth 4 or more the following sum does not overflow.
325c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    LHS += RHS;
326c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    while (LHS.uge(Threshold))
327c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      LHS -= CM;
328c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  } else {
329c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // To avoid problems with overflow do everything the same as above but using
330c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // a larger type.
331c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    unsigned CM = 1U << CarmichaelShift(Bitwidth);
332c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    unsigned Threshold = CM + Bitwidth;
333c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    assert(LHS.getZExtValue() < Threshold && RHS.getZExtValue() < Threshold &&
334c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands           "Weights not reduced!");
335c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    unsigned Total = LHS.getZExtValue() + RHS.getZExtValue();
336c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    while (Total >= Threshold)
337c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      Total -= CM;
338c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    LHS = Total;
339c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
340c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands}
341c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
342c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// EvaluateRepeatedConstant - Compute C op C op ... op C where the constant C
343c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// is repeated Weight times.
344c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandsstatic Constant *EvaluateRepeatedConstant(unsigned Opcode, Constant *C,
345c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands                                          APInt Weight) {
346c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // For addition the result can be efficiently computed as the product of the
347c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // constant and the weight.
348c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (Opcode == Instruction::Add)
349c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    return ConstantExpr::getMul(C, ConstantInt::get(C->getContext(), Weight));
350c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
351c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // The weight might be huge, so compute by repeated squaring to ensure that
352c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // compile time is proportional to the logarithm of the weight.
353c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  Constant *Result = 0;
354c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  Constant *Power = C; // Successively C, C op C, (C op C) op (C op C) etc.
355c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // Visit the bits in Weight.
356c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  while (Weight != 0) {
357c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // If the current bit in Weight is non-zero do Result = Result op Power.
358c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    if (Weight[0])
359c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      Result = Result ? ConstantExpr::get(Opcode, Result, Power) : Power;
360c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // Move on to the next bit if any more are non-zero.
361c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    Weight = Weight.lshr(1);
362c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    if (Weight.isMinValue())
363c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      break;
364c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // Square the power.
365c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    Power = ConstantExpr::get(Opcode, Power, Power);
366c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
367c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
368c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  assert(Result && "Only positive weights supported!");
369c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  return Result;
370c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands}
371c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
372c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandstypedef std::pair<Value*, APInt> RepeatedValue;
373c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
3740fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// LinearizeExprTree - Given an associative binary expression, return the leaf
375c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// nodes in Ops along with their weights (how many times the leaf occurs).  The
376c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// original expression is the same as
377c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands///   (Ops[0].first op Ops[0].first op ... Ops[0].first)  <- Ops[0].second times
378a94d6e87c4c49f2e81b01d66d8bfb591277f8f96Nadav Rotem/// op
379c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands///   (Ops[1].first op Ops[1].first op ... Ops[1].first)  <- Ops[1].second times
380c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// op
381c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands///   ...
382c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// op
383c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands///   (Ops[N].first op Ops[N].first op ... Ops[N].first)  <- Ops[N].second times
384c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands///
385c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// Note that the values Ops[0].first, ..., Ops[N].first are all distinct, and
386c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// they are all non-constant except possibly for the last one, which if it is
387c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// constant will have weight one (Ops[N].second === 1).
388c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands///
389c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// This routine may modify the function, in which case it returns 'true'.  The
390c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// changes it makes may well be destructive, changing the value computed by 'I'
391c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// to something completely different.  Thus if the routine returns 'true' then
392c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// you MUST either replace I with a new expression computed from the Ops array,
393c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// or use RewriteExprTree to put the values back in.
3940fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
3950fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// A leaf node is either not a binary operation of the same kind as the root
3960fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// node 'I' (i.e. is not a binary operator at all, or is, but with a different
3970fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// opcode), or is the same kind of binary operator but has a use which either
3980fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// does not belong to the expression, or does belong to the expression but is
3990fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// a leaf node.  Every leaf node has at least one use that is a non-leaf node
4000fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// of the expression, while for non-leaf nodes (except for the root 'I') every
4010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// use is a non-leaf node of the expression.
4020fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
4030fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// For example:
4040fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///           expression graph        node names
4050fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
4060fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                     +        |        I
4070fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                    / \       |
4080fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                   +   +      |      A,  B
4090fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                  / \ / \     |
4100fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                 *   +   *    |    C,  D,  E
4110fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                / \ / \ / \   |
4120fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                   +   *      |      F,  G
4130fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
4140fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// The leaf nodes are C, E, F and G.  The Ops array will contain (maybe not in
415c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// that order) (C, 1), (E, 1), (F, 2), (G, 2).
4160fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
4170fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// The expression is maximal: if some instruction is a binary operator of the
4180fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// same kind as 'I', and all of its uses are non-leaf nodes of the expression,
4190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// then the instruction also belongs to the expression, is not a leaf node of
4200fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// it, and its operands also belong to the expression (but may be leaf nodes).
421c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner///
4220fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// NOTE: This routine will set operands of non-leaf non-root nodes to undef in
4230fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// order to ensure that every non-root node in the expression has *exactly one*
4240fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// use by a non-leaf node of the expression.  This destruction means that the
425eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands/// caller MUST either replace 'I' with a new expression or use something like
426c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// RewriteExprTree to put the values back in if the routine indicates that it
427c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// made a change by returning 'true'.
428e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner///
4290fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// In the above example either the right operand of A or the left operand of B
4300fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// will be replaced by undef.  If it is B's operand then this gives:
4310fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
4320fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                     +        |        I
4330fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                    / \       |
4340fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                   +   +      |      A,  B - operand of B replaced with undef
4350fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                  / \   \     |
4360fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                 *   +   *    |    C,  D,  E
4370fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                / \ / \ / \   |
4380fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                   +   *      |      F,  G
4390fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
440eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands/// Note that such undef operands can only be reached by passing through 'I'.
441eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands/// For example, if you visit operands recursively starting from a leaf node
442eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands/// then you will never see such an undef operand unless you get back to 'I',
4430fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// which requires passing through a phi node.
4440fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
4450fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// Note that this routine may also mutate binary operators of the wrong type
4460fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// that have all uses inside the expression (i.e. only used by non-leaf nodes
4470fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// of the expression) if it can turn them into binary operators of the right
4480fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// type and thus make the expression bigger.
4490fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
450c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandsstatic bool LinearizeExprTree(BinaryOperator *I,
451c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands                              SmallVectorImpl<RepeatedValue> &Ops) {
4520fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  DEBUG(dbgs() << "LINEARIZE: " << *I << '\n');
453c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  unsigned Bitwidth = I->getType()->getScalarType()->getPrimitiveSizeInBits();
454c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  unsigned Opcode = I->getOpcode();
455c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  assert(Instruction::isAssociative(Opcode) &&
456c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands         Instruction::isCommutative(Opcode) &&
457c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands         "Expected an associative and commutative operation!");
458ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands  // If we see an absorbing element then the entire expression must be equal to
459ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands  // it.  For example, if this is a multiplication expression and zero occurs as
460ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands  // an operand somewhere in it then the result of the expression must be zero.
461ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands  Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, I->getType());
4620fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
4630fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Visit all operands of the expression, keeping track of their weight (the
4640fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // number of paths from the expression root to the operand, or if you like
4650fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // the number of times that operand occurs in the linearized expression).
4660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // For example, if I = X + A, where X = A + B, then I, X and B have weight 1
4670fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // while A has weight two.
4680fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
4690fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Worklist of non-leaf nodes (their operands are in the expression too) along
4700fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // with their weights, representing a certain number of paths to the operator.
4710fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // If an operator occurs in the worklist multiple times then we found multiple
4720fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // ways to get to it.
473c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  SmallVector<std::pair<BinaryOperator*, APInt>, 8> Worklist; // (Op, Weight)
474c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  Worklist.push_back(std::make_pair(I, APInt(Bitwidth, 1)));
475c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  bool MadeChange = false;
476c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner
4770fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Leaves of the expression are values that either aren't the right kind of
4780fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // operation (eg: a constant, or a multiply in an add tree), or are, but have
4790fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // some uses that are not inside the expression.  For example, in I = X + X,
4800fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // X = A + B, the value X has two uses (by I) that are in the expression.  If
4810fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // X has any other uses, for example in a return instruction, then we consider
4820fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // X to be a leaf, and won't analyze it further.  When we first visit a value,
4830fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // if it has more than one use then at first we conservatively consider it to
4840fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // be a leaf.  Later, as the expression is explored, we may discover some more
4850fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // uses of the value from inside the expression.  If all uses turn out to be
4860fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // from within the expression (and the value is a binary operator of the right
4870fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // kind) then the value is no longer considered to be a leaf, and its operands
4880fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // are explored.
4890fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
4900fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Leaves - Keeps track of the set of putative leaves as well as the number of
4910fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // paths to each leaf seen so far.
4925f9e4c1189ab4a8ea1b0000d9337060ac3cac26eDuncan Sands  typedef DenseMap<Value*, APInt> LeafMap;
4930fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  LeafMap Leaves; // Leaf -> Total weight so far.
4940fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  SmallVector<Value*, 8> LeafOrder; // Ensure deterministic leaf output order.
495f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner
4960fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands#ifndef NDEBUG
4970fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  SmallPtrSet<Value*, 8> Visited; // For sanity checking the iteration scheme.
4980fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands#endif
4990fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  while (!Worklist.empty()) {
500c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    std::pair<BinaryOperator*, APInt> P = Worklist.pop_back_val();
5010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    I = P.first; // We examine the operands of this binary operator.
5020fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
5030fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    for (unsigned OpIdx = 0; OpIdx < 2; ++OpIdx) { // Visit operands.
5040fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      Value *Op = I->getOperand(OpIdx);
505c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      APInt Weight = P.second; // Number of paths to this operand.
5060fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      DEBUG(dbgs() << "OPERAND: " << *Op << " (" << Weight << ")\n");
5070fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      assert(!Op->use_empty() && "No uses, so how did we get to it?!");
5080fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
509ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands      // If the expression contains an absorbing element then there is no need
510ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands      // to analyze it further: it must evaluate to the absorbing element.
511ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands      if (Op == Absorber && !Weight.isMinValue()) {
512ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands        Ops.push_back(std::make_pair(Absorber, APInt(Bitwidth, 1)));
513ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands        return MadeChange;
514ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands      }
515ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands
5160fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // If this is a binary operation of the right kind with only one use then
5170fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // add its operands to the expression.
5180fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (BinaryOperator *BO = isReassociableOp(Op, Opcode)) {
5190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        assert(Visited.insert(Op) && "Not first visit!");
5200fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        DEBUG(dbgs() << "DIRECT ADD: " << *Op << " (" << Weight << ")\n");
5210fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Worklist.push_back(std::make_pair(BO, Weight));
5220fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        continue;
5230fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
524e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
5250fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // Appears to be a leaf.  Is the operand already in the set of leaves?
5260fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      LeafMap::iterator It = Leaves.find(Op);
5270fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (It == Leaves.end()) {
5280fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // Not in the leaf map.  Must be the first time we saw this operand.
5290fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        assert(Visited.insert(Op) && "Not first visit!");
5300fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        if (!Op->hasOneUse()) {
5310fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          // This value has uses not accounted for by the expression, so it is
5320fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          // not safe to modify.  Mark it as being a leaf.
5330fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          DEBUG(dbgs() << "ADD USES LEAF: " << *Op << " (" << Weight << ")\n");
5340fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          LeafOrder.push_back(Op);
5350fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          Leaves[Op] = Weight;
5360fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          continue;
5370fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        }
5380fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // No uses outside the expression, try morphing it.
5390fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      } else if (It != Leaves.end()) {
5400fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // Already in the leaf map.
5410fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        assert(Visited.count(Op) && "In leaf map but not visited!");
5420fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
5430fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // Update the number of paths to the leaf.
544c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands        IncorporateWeight(It->second, Weight, Opcode);
5450fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
54620b2d21509b3d5a10ec5d7be6dea8afa9e92fdeeDuncan Sands#if 0   // TODO: Re-enable once PR13021 is fixed.
5470fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // The leaf already has one use from inside the expression.  As we want
5480fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // exactly one such use, drop this new use of the leaf.
5490fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        assert(!Op->hasOneUse() && "Only one use, but we got here twice!");
5500fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        I->setOperand(OpIdx, UndefValue::get(I->getType()));
5510fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        MadeChange = true;
552e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
5530fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // If the leaf is a binary operation of the right kind and we now see
5540fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // that its multiple original uses were in fact all by nodes belonging
5550fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // to the expression, then no longer consider it to be a leaf and add
5560fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // its operands to the expression.
5570fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        if (BinaryOperator *BO = isReassociableOp(Op, Opcode)) {
5580fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          DEBUG(dbgs() << "UNLEAF: " << *Op << " (" << It->second << ")\n");
5590fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          Worklist.push_back(std::make_pair(BO, It->second));
5600fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          Leaves.erase(It);
5610fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          continue;
5620fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        }
56320b2d21509b3d5a10ec5d7be6dea8afa9e92fdeeDuncan Sands#endif
564fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
5650fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // If we still have uses that are not accounted for by the expression
5660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // then it is not safe to modify the value.
5670fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        if (!Op->hasOneUse())
5680fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          continue;
5694fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
5700fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // No uses outside the expression, try morphing it.
5710fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Weight = It->second;
5720fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Leaves.erase(It); // Since the value may be morphed below.
5730fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
574c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner
5750fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // At this point we have a value which, first of all, is not a binary
5760fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // expression of the right kind, and secondly, is only used inside the
5770fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // expression.  This means that it can safely be modified.  See if we
5780fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // can usefully morph it into an expression of the right kind.
5790fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      assert((!isa<Instruction>(Op) ||
5800fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands              cast<Instruction>(Op)->getOpcode() != Opcode) &&
5810fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands             "Should have been handled above!");
5820fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      assert(Op->hasOneUse() && "Has uses outside the expression tree!");
5830fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
5840fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // If this is a multiply expression, turn any internal negations into
5850fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // multiplies by -1 so they can be reassociated.
5860fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      BinaryOperator *BO = dyn_cast<BinaryOperator>(Op);
5870fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (Opcode == Instruction::Mul && BO && BinaryOperator::isNeg(BO)) {
5880fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        DEBUG(dbgs() << "MORPH LEAF: " << *Op << " (" << Weight << ") TO ");
589841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        BO = LowerNegateToMultiply(BO);
5900fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        DEBUG(dbgs() << *BO << 'n');
5910fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Worklist.push_back(std::make_pair(BO, Weight));
5920fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        MadeChange = true;
5930fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        continue;
5940fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
595c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner
5960fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // Failed to morph into an expression of the right type.  This really is
5970fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // a leaf.
5980fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      DEBUG(dbgs() << "ADD LEAF: " << *Op << " (" << Weight << ")\n");
5990fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      assert(!isReassociableOp(Op, Opcode) && "Value was morphed?");
6000fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      LeafOrder.push_back(Op);
6010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      Leaves[Op] = Weight;
6020fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    }
6030fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  }
604e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
6050fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // The leaves, repeated according to their weights, represent the linearized
6060fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // form of the expression.
607c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  Constant *Cst = 0; // Accumulate constants here.
6080fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  for (unsigned i = 0, e = LeafOrder.size(); i != e; ++i) {
6090fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Value *V = LeafOrder[i];
6100fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    LeafMap::iterator It = Leaves.find(V);
6110fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    if (It == Leaves.end())
612c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      // Node initially thought to be a leaf wasn't.
6130fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      continue;
6140fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    assert(!isReassociableOp(V, Opcode) && "Shouldn't be a leaf!");
615c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    APInt Weight = It->second;
616c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    if (Weight.isMinValue())
617c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      // Leaf already output or weight reduction eliminated it.
618c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      continue;
6190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Ensure the leaf is only output once.
620c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    It->second = 0;
621c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // Glob all constants together into Cst.
622c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    if (Constant *C = dyn_cast<Constant>(V)) {
623c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      C = EvaluateRepeatedConstant(Opcode, C, Weight);
624c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      Cst = Cst ? ConstantExpr::get(Opcode, Cst, C) : C;
625c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      continue;
626c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    }
627c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // Add non-constant
628c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    Ops.push_back(std::make_pair(V, Weight));
629c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
630c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
631c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // Add any constants back into Ops, all globbed together and reduced to having
632c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // weight 1 for the convenience of users.
633ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands  Constant *Identity = ConstantExpr::getBinOpIdentity(Opcode, I->getType());
634d34491f6751ae2f8daf3e857c84bcb5b06fba889Duncan Sands  if (Cst && Cst != Identity) {
635d34491f6751ae2f8daf3e857c84bcb5b06fba889Duncan Sands    // If combining multiple constants resulted in the absorber then the entire
636d34491f6751ae2f8daf3e857c84bcb5b06fba889Duncan Sands    // expression must evaluate to the absorber.
637d34491f6751ae2f8daf3e857c84bcb5b06fba889Duncan Sands    if (Cst == Absorber)
638d34491f6751ae2f8daf3e857c84bcb5b06fba889Duncan Sands      Ops.clear();
639c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    Ops.push_back(std::make_pair(Cst, APInt(Bitwidth, 1)));
640d34491f6751ae2f8daf3e857c84bcb5b06fba889Duncan Sands  }
641c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
642c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // For nilpotent operations or addition there may be no operands, for example
643c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // because the expression was "X xor X" or consisted of 2^Bitwidth additions:
644c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // in both cases the weight reduces to 0 causing the value to be skipped.
645c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (Ops.empty()) {
646ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands    assert(Identity && "Associative operation without identity!");
647c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    Ops.push_back(std::make_pair(Identity, APInt(Bitwidth, 1)));
6480fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  }
649c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
650c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  return MadeChange;
651c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner}
652c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner
653c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner// RewriteExprTree - Now that the operands for this expression tree are
6540fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands// linearized and optimized, emit them in-order.
655e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattnervoid Reassociate::RewriteExprTree(BinaryOperator *I,
6560fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands                                  SmallVectorImpl<ValueEntry> &Ops) {
6570fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  assert(Ops.size() > 1 && "Single values should be used directly!");
6580fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
6590fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Since our optimizations never increase the number of operations, the new
6600fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // expression can always be written by reusing the existing binary operators
6610fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // from the original expression tree, without creating any new instructions,
6620fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // though the rewritten expression may have a completely different topology.
6630fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // We take care to not change anything if the new expression will be the same
6640fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // as the original.  If more than trivial changes (like commuting operands)
6650fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // were made then we are obliged to clear out any optional subclass data like
6660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // nsw flags.
6670fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
6680fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  /// NodesToRewrite - Nodes from the original expression available for writing
6690fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  /// the new expression into.
6700fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  SmallVector<BinaryOperator*, 8> NodesToRewrite;
6710fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  unsigned Opcode = I->getOpcode();
6722923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands  BinaryOperator *Op = I;
6730fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
674eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands  // ExpressionChanged - Non-null if the rewritten expression differs from the
675eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands  // original in some non-trivial way, requiring the clearing of optional flags.
676eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands  // Flags are cleared from the operator in ExpressionChanged up to I inclusive.
677eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands  BinaryOperator *ExpressionChanged = 0;
6782d5f8ca3d180832d168e59e2bf3d85317e86287dDuncan Sands  for (unsigned i = 0; ; ++i) {
6790fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // The last operation (which comes earliest in the IR) is special as both
6800fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // operands will come from Ops, rather than just one with the other being
6810fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // a subexpression.
6820fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    if (i+2 == Ops.size()) {
6830fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      Value *NewLHS = Ops[i].Op;
6840fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      Value *NewRHS = Ops[i+1].Op;
6850fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      Value *OldLHS = Op->getOperand(0);
6860fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      Value *OldRHS = Op->getOperand(1);
6870fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
6880fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (NewLHS == OldLHS && NewRHS == OldRHS)
6890fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // Nothing changed, leave it alone.
6900fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        break;
6910fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
6920fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (NewLHS == OldRHS && NewRHS == OldLHS) {
6930fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // The order of the operands was reversed.  Swap them.
6940fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        DEBUG(dbgs() << "RA: " << *Op << '\n');
6950fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Op->swapOperands();
6960fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        DEBUG(dbgs() << "TO: " << *Op << '\n');
6970fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        MadeChange = true;
6980fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        ++NumChanged;
6990fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        break;
7000fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
7010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
7020fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // The new operation differs non-trivially from the original. Overwrite
7030fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // the old operands with the new ones.
7040fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      DEBUG(dbgs() << "RA: " << *Op << '\n');
7050fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (NewLHS != OldLHS) {
7060fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        if (BinaryOperator *BO = isReassociableOp(OldLHS, Opcode))
7070fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          NodesToRewrite.push_back(BO);
7080fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Op->setOperand(0, NewLHS);
7090fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
7100fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (NewRHS != OldRHS) {
7110fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        if (BinaryOperator *BO = isReassociableOp(OldRHS, Opcode))
7120fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          NodesToRewrite.push_back(BO);
7130fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Op->setOperand(1, NewRHS);
7140fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
7150fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      DEBUG(dbgs() << "TO: " << *Op << '\n');
7160fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
717eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands      ExpressionChanged = Op;
718c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner      MadeChange = true;
719c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner      ++NumChanged;
720e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
7210fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      break;
722c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner    }
723c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner
7240fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Not the last operation.  The left-hand side will be a sub-expression
7250fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // while the right-hand side will be the current element of Ops.
7260fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Value *NewRHS = Ops[i].Op;
7270fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    if (NewRHS != Op->getOperand(1)) {
7280fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      DEBUG(dbgs() << "RA: " << *Op << '\n');
7290fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (NewRHS == Op->getOperand(0)) {
7300fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // The new right-hand side was already present as the left operand.  If
7310fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // we are lucky then swapping the operands will sort out both of them.
7320fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Op->swapOperands();
7330fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      } else {
7340fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // Overwrite with the new right-hand side.
7350fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        if (BinaryOperator *BO = isReassociableOp(Op->getOperand(1), Opcode))
7360fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          NodesToRewrite.push_back(BO);
7370fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Op->setOperand(1, NewRHS);
738eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands        ExpressionChanged = Op;
7390fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
7400fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      DEBUG(dbgs() << "TO: " << *Op << '\n');
7410fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      MadeChange = true;
7420fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      ++NumChanged;
7430fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    }
74446985a14409486293b689ca07dd07d7482734795Dan Gohman
7450fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Now deal with the left-hand side.  If this is already an operation node
7460fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // from the original expression then just rewrite the rest of the expression
7470fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // into it.
7480fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    if (BinaryOperator *BO = isReassociableOp(Op->getOperand(0), Opcode)) {
7492923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands      Op = BO;
7500fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      continue;
7510fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    }
75246985a14409486293b689ca07dd07d7482734795Dan Gohman
7530fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Otherwise, grab a spare node from the original expression and use that as
75496d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    // the left-hand side.  If there are no nodes left then the optimizers made
75596d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    // an expression with more nodes than the original!  This usually means that
75696d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    // they did something stupid but it might mean that the problem was just too
75796d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    // hard (finding the mimimal number of multiplications needed to realize a
75896d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    // multiplication expression is NP-complete).  Whatever the reason, smart or
75996d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    // stupid, create a new node if there are none left.
7602923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands    BinaryOperator *NewOp;
76196d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    if (NodesToRewrite.empty()) {
76296d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands      Constant *Undef = UndefValue::get(I->getType());
7632923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands      NewOp = BinaryOperator::Create(Instruction::BinaryOps(Opcode),
7642923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands                                     Undef, Undef, "", I);
7652923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands    } else {
7662923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands      NewOp = NodesToRewrite.pop_back_val();
76796d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    }
76896d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands
7690fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    DEBUG(dbgs() << "RA: " << *Op << '\n');
7702923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands    Op->setOperand(0, NewOp);
7710fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    DEBUG(dbgs() << "TO: " << *Op << '\n');
772eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands    ExpressionChanged = Op;
773c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner    MadeChange = true;
774c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner    ++NumChanged;
7752923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands    Op = NewOp;
776c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner  }
777e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
778eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands  // If the expression changed non-trivially then clear out all subclass data
7792d5f8ca3d180832d168e59e2bf3d85317e86287dDuncan Sands  // starting from the operator specified in ExpressionChanged, and compactify
7802d5f8ca3d180832d168e59e2bf3d85317e86287dDuncan Sands  // the operators to just before the expression root to guarantee that the
7812d5f8ca3d180832d168e59e2bf3d85317e86287dDuncan Sands  // expression tree is dominated by all of Ops.
7822d5f8ca3d180832d168e59e2bf3d85317e86287dDuncan Sands  if (ExpressionChanged)
7830fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    do {
784eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands      ExpressionChanged->clearSubclassOptionalData();
785eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands      if (ExpressionChanged == I)
7860fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        break;
7872d5f8ca3d180832d168e59e2bf3d85317e86287dDuncan Sands      ExpressionChanged->moveBefore(I);
788eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands      ExpressionChanged = cast<BinaryOperator>(*ExpressionChanged->use_begin());
7890fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    } while (1);
790e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
7910fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Throw away any left over nodes from the original expression.
7920fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  for (unsigned i = 0, e = NodesToRewrite.size(); i != e; ++i)
793841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    RedoInsts.insert(NodesToRewrite[i]);
7944fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner}
7954fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
796e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// NegateValue - Insert instructions before the instruction pointed to by BI,
797e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// that computes the negative version of the value specified.  The negative
798e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// version of the value is returned, and BI is left pointing at the instruction
799e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// that should be processed next by the reassociation pass.
800e79fddedcae1ee8fe7d8571db58447bc722f75dcNick Lewyckystatic Value *NegateValue(Value *V, Instruction *BI) {
80135239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner  if (Constant *C = dyn_cast<Constant>(V))
80235239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    return ConstantExpr::getNeg(C);
803e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
804a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // We are trying to expose opportunity for reassociation.  One of the things
805a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // that we want to do to achieve this is to push a negation as deep into an
806a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // expression chain as possible, to expose the add instructions.  In practice,
807a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // this means that we turn this:
808a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  //   X = -(A+12+C+D)   into    X = -A + -12 + -C + -D = -12 + -A + -C + -D
809a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // so that later, a: Y = 12+X could get reassociated with the -12 to eliminate
810a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // the constants.  We assume that instcombine will clean up the mess later if
8119046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner  // we introduce tons of unnecessary negation instructions.
812a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  //
8130fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  if (BinaryOperator *I = isReassociableOp(V, Instruction::Add)) {
8140fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Push the negates through the add.
8150fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    I->setOperand(0, NegateValue(I->getOperand(0), BI));
8160fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    I->setOperand(1, NegateValue(I->getOperand(1), BI));
8170fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
8180fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // We must move the add instruction here, because the neg instructions do
8190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // not dominate the old add instruction in general.  By moving it, we are
8200fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // assured that the neg instructions we just inserted dominate the
8210fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // instruction we are about to insert after them.
8220fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    //
8230fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    I->moveBefore(BI);
8240fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    I->setName(I->getName()+".neg");
8250fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    return I;
8260fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  }
827e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
82835239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner  // Okay, we need to materialize a negated version of V with an instruction.
82935239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner  // Scan the use lists of V to see if we have one already.
83035239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
831110b75aa7572d3b59b308da7ec1d759e86788f97Gabor Greif    User *U = *UI;
832110b75aa7572d3b59b308da7ec1d759e86788f97Gabor Greif    if (!BinaryOperator::isNeg(U)) continue;
83335239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner
83435239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    // We found one!  Now we have to make sure that the definition dominates
83535239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    // this use.  We do this by moving it to the entry block (if it is a
83635239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    // non-instruction value) or right after the definition.  These negates will
83735239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    // be zapped by reassociate later, so we don't need much finesse here.
838110b75aa7572d3b59b308da7ec1d759e86788f97Gabor Greif    BinaryOperator *TheNeg = cast<BinaryOperator>(U);
8391c91fae649734abe6f8271862fe3ba917e191279Chris Lattner
8401c91fae649734abe6f8271862fe3ba917e191279Chris Lattner    // Verify that the negate is in this function, V might be a constant expr.
8411c91fae649734abe6f8271862fe3ba917e191279Chris Lattner    if (TheNeg->getParent()->getParent() != BI->getParent()->getParent())
8421c91fae649734abe6f8271862fe3ba917e191279Chris Lattner      continue;
843e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
84435239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    BasicBlock::iterator InsertPt;
84535239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    if (Instruction *InstInput = dyn_cast<Instruction>(V)) {
84635239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner      if (InvokeInst *II = dyn_cast<InvokeInst>(InstInput)) {
84735239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner        InsertPt = II->getNormalDest()->begin();
84835239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner      } else {
84935239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner        InsertPt = InstInput;
85035239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner        ++InsertPt;
85135239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner      }
85235239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner      while (isa<PHINode>(InsertPt)) ++InsertPt;
85335239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    } else {
85435239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner      InsertPt = TheNeg->getParent()->getParent()->getEntryBlock().begin();
85535239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    }
85635239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    TheNeg->moveBefore(InsertPt);
85735239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    return TheNeg;
85835239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner  }
859a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner
860a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // Insert a 'neg' instruction that subtracts the value from zero to get the
861a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // negation.
8624ae5126d041768ab9665cf2f11c024becd76c41fDan Gohman  return BinaryOperator::CreateNeg(V, V->getName() + ".neg", BI);
863a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner}
864a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner
8659bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner/// ShouldBreakUpSubtract - Return true if we should break up this subtract of
8669bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner/// X-Y into (X + -Y).
867e79fddedcae1ee8fe7d8571db58447bc722f75dcNick Lewyckystatic bool ShouldBreakUpSubtract(Instruction *Sub) {
8689bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner  // If this is a negation, we can't split it up!
869fa82b6eba4e1584d7dba291c28fe908272e1e002Owen Anderson  if (BinaryOperator::isNeg(Sub))
8709bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner    return false;
871e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
8729bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner  // Don't bother to break this up unless either the LHS is an associable add or
8730b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner  // subtract or if this is only used by one.
8740b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner  if (isReassociableOp(Sub->getOperand(0), Instruction::Add) ||
8750b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner      isReassociableOp(Sub->getOperand(0), Instruction::Sub))
8769bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner    return true;
8770b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner  if (isReassociableOp(Sub->getOperand(1), Instruction::Add) ||
8785329bb22e9b6374d62919981c1ef8775b42945ebChris Lattner      isReassociableOp(Sub->getOperand(1), Instruction::Sub))
8799bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner    return true;
880e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling  if (Sub->hasOneUse() &&
8810b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner      (isReassociableOp(Sub->use_back(), Instruction::Add) ||
8820b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner       isReassociableOp(Sub->use_back(), Instruction::Sub)))
8839bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner    return true;
884e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
8859bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner  return false;
8869bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner}
8879bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner
88808b43921e18f314c4fd38049291d323830934c36Chris Lattner/// BreakUpSubtract - If we have (X-Y), and if either X is an add, or if this is
88908b43921e18f314c4fd38049291d323830934c36Chris Lattner/// only used by an add, transform this into (X+(0-Y)) to promote better
89008b43921e18f314c4fd38049291d323830934c36Chris Lattner/// reassociation.
891841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsstatic BinaryOperator *BreakUpSubtract(Instruction *Sub) {
8929046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner  // Convert a subtract into an add and a neg instruction. This allows sub
8939046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner  // instructions to be commuted with other add instructions.
89408b43921e18f314c4fd38049291d323830934c36Chris Lattner  //
8959046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner  // Calculate the negative value of Operand 1 of the sub instruction,
8969046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner  // and set it as the RHS of the add instruction we just made.
89708b43921e18f314c4fd38049291d323830934c36Chris Lattner  //
898e79fddedcae1ee8fe7d8571db58447bc722f75dcNick Lewycky  Value *NegVal = NegateValue(Sub->getOperand(1), Sub);
899841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  BinaryOperator *New =
9007cbd8a3e92221437048b484d5ef9c0a22d0f8c58Gabor Greif    BinaryOperator::CreateAdd(Sub->getOperand(0), NegVal, "", Sub);
901841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Sub->setOperand(0, Constant::getNullValue(Sub->getType())); // Drop use of op.
902841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Sub->setOperand(1, Constant::getNullValue(Sub->getType())); // Drop use of op.
9036934a04a8c15e9971cd1ea4d5c8df2d7afdd5be5Chris Lattner  New->takeName(Sub);
90408b43921e18f314c4fd38049291d323830934c36Chris Lattner
90508b43921e18f314c4fd38049291d323830934c36Chris Lattner  // Everyone now refers to the add instruction.
90608b43921e18f314c4fd38049291d323830934c36Chris Lattner  Sub->replaceAllUsesWith(New);
9075367b23f76e75ebb680956575346fa8c3d56780fDevang Patel  New->setDebugLoc(Sub->getDebugLoc());
90800b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen
909a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene  DEBUG(dbgs() << "Negated: " << *New << '\n');
91008b43921e18f314c4fd38049291d323830934c36Chris Lattner  return New;
91108b43921e18f314c4fd38049291d323830934c36Chris Lattner}
91208b43921e18f314c4fd38049291d323830934c36Chris Lattner
9130975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner/// ConvertShiftToMul - If this is a shift of a reassociable multiply or is used
9140975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner/// by one, change this into a multiply by a constant to assist with further
9150975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner/// reassociation.
916841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsstatic BinaryOperator *ConvertShiftToMul(Instruction *Shl) {
917841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Constant *MulCst = ConstantInt::get(Shl->getType(), 1);
918841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  MulCst = ConstantExpr::getShl(MulCst, cast<Constant>(Shl->getOperand(1)));
919841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands
920841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  BinaryOperator *Mul =
921841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    BinaryOperator::CreateMul(Shl->getOperand(0), MulCst, "", Shl);
922841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Shl->setOperand(0, UndefValue::get(Shl->getType())); // Drop use of op.
923841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Mul->takeName(Shl);
924841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Shl->replaceAllUsesWith(Mul);
925841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Mul->setDebugLoc(Shl->getDebugLoc());
926841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  return Mul;
9270975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner}
9280975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner
929e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// FindInOperandList - Scan backwards and forwards among values with the same
930e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// rank as element i to see if X exists.  If X does not exist, return i.  This
931e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// is useful when scanning for 'x' when we see '-x' because they both get the
932e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// same rank.
9339f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattnerstatic unsigned FindInOperandList(SmallVectorImpl<ValueEntry> &Ops, unsigned i,
934109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner                                  Value *X) {
935109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  unsigned XRank = Ops[i].Rank;
936109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  unsigned e = Ops.size();
937109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  for (unsigned j = i+1; j != e && Ops[j].Rank == XRank; ++j)
938109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner    if (Ops[j].Op == X)
939109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner      return j;
9409506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  // Scan backwards.
941109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  for (unsigned j = i-1; j != ~0U && Ops[j].Rank == XRank; --j)
942109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner    if (Ops[j].Op == X)
943109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner      return j;
944109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  return i;
945109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner}
946109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner
947e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// EmitAddTreeOfValues - Emit a tree of add instructions, summing Ops together
948e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// and returning the result.  Insert the tree before I.
94955e7098bbc363473c01229517097d2a04e04e9b0Bill Wendlingstatic Value *EmitAddTreeOfValues(Instruction *I,
95055e7098bbc363473c01229517097d2a04e04e9b0Bill Wendling                                  SmallVectorImpl<WeakVH> &Ops){
951e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  if (Ops.size() == 1) return Ops.back();
952e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
953e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  Value *V1 = Ops.back();
954e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  Ops.pop_back();
955e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  Value *V2 = EmitAddTreeOfValues(I, Ops);
9567cbd8a3e92221437048b484d5ef9c0a22d0f8c58Gabor Greif  return BinaryOperator::CreateAdd(V2, V1, "tmp", I);
957e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner}
958e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner
959e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// RemoveFactorFromExpression - If V is an expression tree that is a
960e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// multiplication sequence, and if this sequence contains a multiply by Factor,
961e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// remove Factor from the tree and return the new tree.
962e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris LattnerValue *Reassociate::RemoveFactorFromExpression(Value *V, Value *Factor) {
963e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  BinaryOperator *BO = isReassociableOp(V, Instruction::Mul);
964e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  if (!BO) return 0;
965e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
966c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  SmallVector<RepeatedValue, 8> Tree;
967c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  MadeChange |= LinearizeExprTree(BO, Tree);
9689f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner  SmallVector<ValueEntry, 8> Factors;
969c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  Factors.reserve(Tree.size());
970c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  for (unsigned i = 0, e = Tree.size(); i != e; ++i) {
971c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    RepeatedValue E = Tree[i];
972c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    Factors.append(E.second.getZExtValue(),
973c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands                   ValueEntry(getRank(E.first), E.first));
974c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
975e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner
976e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  bool FoundFactor = false;
9779506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  bool NeedsNegate = false;
9789506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  for (unsigned i = 0, e = Factors.size(); i != e; ++i) {
979e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner    if (Factors[i].Op == Factor) {
980e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner      FoundFactor = true;
981e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner      Factors.erase(Factors.begin()+i);
982e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner      break;
983e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner    }
984e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
9859506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    // If this is a negative version of this factor, remove it.
9869506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    if (ConstantInt *FC1 = dyn_cast<ConstantInt>(Factor))
9879506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      if (ConstantInt *FC2 = dyn_cast<ConstantInt>(Factors[i].Op))
9889506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner        if (FC1->getValue() == -FC2->getValue()) {
9899506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          FoundFactor = NeedsNegate = true;
9909506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          Factors.erase(Factors.begin()+i);
9919506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          break;
9929506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner        }
9939506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  }
994e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
995e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner  if (!FoundFactor) {
996e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner    // Make sure to restore the operands to the expression tree.
997e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner    RewriteExprTree(BO, Factors);
998e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner    return 0;
999e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner  }
1000e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
10019506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  BasicBlock::iterator InsertPt = BO; ++InsertPt;
1002e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
10031e7558b65689999089f53ce40ff07564cf498c68Chris Lattner  // If this was just a single multiply, remove the multiply and return the only
10041e7558b65689999089f53ce40ff07564cf498c68Chris Lattner  // remaining operand.
10051e7558b65689999089f53ce40ff07564cf498c68Chris Lattner  if (Factors.size() == 1) {
1006841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    RedoInsts.insert(BO);
10079506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    V = Factors[0].Op;
10089506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  } else {
10099506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    RewriteExprTree(BO, Factors);
10109506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    V = BO;
10111e7558b65689999089f53ce40ff07564cf498c68Chris Lattner  }
1012e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
10139506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  if (NeedsNegate)
10149506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    V = BinaryOperator::CreateNeg(V, "neg", InsertPt);
1015e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
10169506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  return V;
1017e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner}
1018e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner
1019e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner/// FindSingleUseMultiplyFactors - If V is a single-use multiply, recursively
1020e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner/// add its operands as factors, otherwise add V to the list of factors.
1021893075f46e9d07e3fe94e2b0e0f3ff8ae4061549Chris Lattner///
1022893075f46e9d07e3fe94e2b0e0f3ff8ae4061549Chris Lattner/// Ops is the top-level list of add operands we're trying to factor.
1023e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattnerstatic void FindSingleUseMultiplyFactors(Value *V,
1024893075f46e9d07e3fe94e2b0e0f3ff8ae4061549Chris Lattner                                         SmallVectorImpl<Value*> &Factors,
10250fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands                                       const SmallVectorImpl<ValueEntry> &Ops) {
10260fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  BinaryOperator *BO = isReassociableOp(V, Instruction::Mul);
10270fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  if (!BO) {
1028e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner    Factors.push_back(V);
1029e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner    return;
1030e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner  }
1031e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1032e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner  // Otherwise, add the LHS and RHS to the list of factors.
10330fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  FindSingleUseMultiplyFactors(BO->getOperand(1), Factors, Ops);
10340fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  FindSingleUseMultiplyFactors(BO->getOperand(0), Factors, Ops);
1035e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner}
1036e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner
1037f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// OptimizeAndOrXor - Optimize a series of operands to an 'and', 'or', or 'xor'
1038f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// instruction.  This optimizes based on identities.  If it can be reduced to
1039f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// a single Value, it is returned, otherwise the Ops list is mutated as
1040f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// necessary.
10419f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattnerstatic Value *OptimizeAndOrXor(unsigned Opcode,
10429f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner                               SmallVectorImpl<ValueEntry> &Ops) {
1043f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  // Scan the operand lists looking for X and ~X pairs, along with X,X pairs.
1044f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  // If we find any, we can simplify the expression. X&~X == 0, X|~X == -1.
1045f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1046f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    // First, check for X and ~X in the operand list.
1047f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    assert(i < Ops.size());
1048f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    if (BinaryOperator::isNot(Ops[i].Op)) {    // Cannot occur for ^.
1049f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      Value *X = BinaryOperator::getNotArgument(Ops[i].Op);
1050f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      unsigned FoundX = FindInOperandList(Ops, i, X);
1051f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      if (FoundX != i) {
10529fdaefad580194353f34b6d72669591f8f9d811aChris Lattner        if (Opcode == Instruction::And)   // ...&X&~X = 0
1053f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner          return Constant::getNullValue(X->getType());
1054e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
10559fdaefad580194353f34b6d72669591f8f9d811aChris Lattner        if (Opcode == Instruction::Or)    // ...|X|~X = -1
1056f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner          return Constant::getAllOnesValue(X->getType());
1057f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      }
1058f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    }
1059e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1060f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    // Next, check for duplicate pairs of values, which we assume are next to
1061f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    // each other, due to our sorting criteria.
1062f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    assert(i < Ops.size());
1063f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    if (i+1 != Ops.size() && Ops[i+1].Op == Ops[i].Op) {
1064f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      if (Opcode == Instruction::And || Opcode == Instruction::Or) {
1065f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner        // Drop duplicate values for And and Or.
1066f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner        Ops.erase(Ops.begin()+i);
1067f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner        --i; --e;
1068f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner        ++NumAnnihil;
1069f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner        continue;
1070f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      }
1071e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1072f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      // Drop pairs of values for Xor.
1073f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      assert(Opcode == Instruction::Xor);
1074f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      if (e == 2)
1075f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner        return Constant::getNullValue(Ops[0].Op->getType());
1076e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
10779046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner      // Y ^ X^X -> Y
1078f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      Ops.erase(Ops.begin()+i, Ops.begin()+i+2);
1079f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      i -= 1; e -= 2;
1080f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      ++NumAnnihil;
1081f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    }
1082f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  }
1083f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  return 0;
1084f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner}
1085e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner
1086f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// OptimizeAdd - Optimize a series of operands to an 'add' instruction.  This
1087f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// optimizes based on identities.  If it can be reduced to a single Value, it
1088f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// is returned, otherwise the Ops list is mutated as necessary.
10899f7b7089be854c323f8d9a4627d80e47adf496e6Chris LattnerValue *Reassociate::OptimizeAdd(Instruction *I,
10909f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner                                SmallVectorImpl<ValueEntry> &Ops) {
1091f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  // Scan the operand lists looking for X and -X pairs.  If we find any, we
109269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  // can simplify the expression. X+-X == 0.  While we're at it, scan for any
109369e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  // duplicates.  We want to canonicalize Y+Y+Y+Z -> 3*Y+Z.
10949506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  //
10959506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  // TODO: We could handle "X + ~X" -> "-1" if we wanted, since "-X = ~X+1".
10969506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  //
1097f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
109869e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    Value *TheOp = Ops[i].Op;
109969e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    // Check to see if we've seen this operand before.  If so, we factor all
1100f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner    // instances of the operand together.  Due to our sorting criteria, we know
1101f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner    // that these need to be next to each other in the vector.
1102f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner    if (i+1 != Ops.size() && Ops[i+1].Op == TheOp) {
1103f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      // Rescan the list, remove all instances of this operand from the expr.
110469e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      unsigned NumFound = 0;
1105f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      do {
1106f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner        Ops.erase(Ops.begin()+i);
110769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner        ++NumFound;
1108f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      } while (i != Ops.size() && Ops[i].Op == TheOp);
1109e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1110f8a447de162a2896a8a044931fb63de713dbc6b9Chris Lattner      DEBUG(errs() << "\nFACTORING [" << NumFound << "]: " << *TheOp << '\n');
111169e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      ++NumFactor;
1112e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
111369e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // Insert a new multiply.
111469e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      Value *Mul = ConstantInt::get(cast<IntegerType>(I->getType()), NumFound);
111569e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      Mul = BinaryOperator::CreateMul(TheOp, Mul, "factor", I);
1116e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
111769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // Now that we have inserted a multiply, optimize it. This allows us to
111869e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // handle cases that require multiple factoring steps, such as this:
111969e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // (X*2) + (X*2) + (X*2) -> (X*2)*3 -> X*6
1120841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      RedoInsts.insert(cast<Instruction>(Mul));
1121e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
112269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // If every add operand was a duplicate, return the multiply.
112369e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      if (Ops.empty())
112469e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner        return Mul;
1125e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
112669e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // Otherwise, we had some input that didn't have the dupe, such as
112769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // "A + A + B" -> "A*2 + B".  Add the new multiply to the list of
112869e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // things being added by this operation.
112969e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      Ops.insert(Ops.begin(), ValueEntry(getRank(Mul), Mul));
1130e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1131f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      --i;
1132f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      e = Ops.size();
1133f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      continue;
113469e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    }
1135e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1136f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    // Check for X and -X in the operand list.
113769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    if (!BinaryOperator::isNeg(TheOp))
1138f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      continue;
1139e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
114069e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    Value *X = BinaryOperator::getNegArgument(TheOp);
1141f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    unsigned FoundX = FindInOperandList(Ops, i, X);
1142f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    if (FoundX == i)
1143f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      continue;
1144e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1145f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    // Remove X and -X from the operand list.
11469fdaefad580194353f34b6d72669591f8f9d811aChris Lattner    if (Ops.size() == 2)
1147f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      return Constant::getNullValue(X->getType());
1148e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1149f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    Ops.erase(Ops.begin()+i);
1150f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    if (i < FoundX)
1151f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      --FoundX;
1152f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    else
1153f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      --i;   // Need to back up an extra one.
1154f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    Ops.erase(Ops.begin()+FoundX);
1155f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    ++NumAnnihil;
1156f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    --i;     // Revisit element.
1157f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    e -= 2;  // Removed two elements.
1158f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  }
1159e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
116094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // Scan the operand list, checking to see if there are any common factors
116194285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // between operands.  Consider something like A*A+A*B*C+D.  We would like to
116294285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // reassociate this to A*(A+B*C)+D, which reduces the number of multiplies.
116394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // To efficiently find this, we count the number of times a factor occurs
116494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // for any ADD operands that are MULs.
116594285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  DenseMap<Value*, unsigned> FactorOccurrences;
1166e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
116794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // Keep track of each multiply we see, to avoid triggering on (X*4)+(X*4)
116894285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // where they are actually the same multiply.
116994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  unsigned MaxOcc = 0;
117094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  Value *MaxOccVal = 0;
117194285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
11720fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    BinaryOperator *BOp = isReassociableOp(Ops[i].Op, Instruction::Mul);
11730fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    if (!BOp)
117494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner      continue;
1175e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
117694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // Compute all of the factors of this added value.
117794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    SmallVector<Value*, 8> Factors;
11780fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    FindSingleUseMultiplyFactors(BOp, Factors, Ops);
117994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    assert(Factors.size() > 1 && "Bad linearize!");
1180e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
118194285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // Add one to FactorOccurrences for each unique factor in this op.
11829506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    SmallPtrSet<Value*, 8> Duplicates;
11839506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    for (unsigned i = 0, e = Factors.size(); i != e; ++i) {
11849506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      Value *Factor = Factors[i];
11859506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      if (!Duplicates.insert(Factor)) continue;
1186e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
11879506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      unsigned Occ = ++FactorOccurrences[Factor];
11889506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factor; }
1189e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
11909506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      // If Factor is a negative constant, add the negated value as a factor
11919506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      // because we can percolate the negate out.  Watch for minint, which
11929506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      // cannot be positivified.
11939506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      if (ConstantInt *CI = dyn_cast<ConstantInt>(Factor))
1194c73b24db5f6226ed44ebc44ce1c25bb357206623Chris Lattner        if (CI->isNegative() && !CI->isMinValue(true)) {
11959506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          Factor = ConstantInt::get(CI->getContext(), -CI->getValue());
11969506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          assert(!Duplicates.count(Factor) &&
11979506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner                 "Shouldn't have two constant factors, missed a canonicalize");
1198e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
11999506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          unsigned Occ = ++FactorOccurrences[Factor];
12009506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factor; }
12019506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner        }
120294285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    }
120394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  }
1204e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
120594285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // If any factor occurred more than one time, we can pull it out.
120694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  if (MaxOcc > 1) {
120769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    DEBUG(errs() << "\nFACTORING [" << MaxOcc << "]: " << *MaxOccVal << '\n');
120894285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    ++NumFactor;
120994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner
121094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // Create a new instruction that uses the MaxOccVal twice.  If we don't do
121194285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // this, we could otherwise run into situations where removing a factor
1212e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling    // from an expression will drop a use of maxocc, and this can cause
121394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // RemoveFactorFromExpression on successive values to behave differently.
121494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    Instruction *DummyInst = BinaryOperator::CreateAdd(MaxOccVal, MaxOccVal);
121555e7098bbc363473c01229517097d2a04e04e9b0Bill Wendling    SmallVector<WeakVH, 4> NewMulOps;
121637f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands    for (unsigned i = 0; i != Ops.size(); ++i) {
1217c2d1b6949c5141d21827cc94daea6ae4b1a9c750Chris Lattner      // Only try to remove factors from expressions we're allowed to.
12180fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      BinaryOperator *BOp = isReassociableOp(Ops[i].Op, Instruction::Mul);
12190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (!BOp)
1220c2d1b6949c5141d21827cc94daea6ae4b1a9c750Chris Lattner        continue;
1221e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
122294285e620b845e09b18939e8d6448e01e692f3ceChris Lattner      if (Value *V = RemoveFactorFromExpression(Ops[i].Op, MaxOccVal)) {
122337f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands        // The factorized operand may occur several times.  Convert them all in
122437f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands        // one fell swoop.
122537f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands        for (unsigned j = Ops.size(); j != i;) {
122637f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands          --j;
122737f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands          if (Ops[j].Op == Ops[i].Op) {
122837f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands            NewMulOps.push_back(V);
122937f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands            Ops.erase(Ops.begin()+j);
123037f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands          }
123137f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands        }
123237f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands        --i;
123394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner      }
123494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    }
1235e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
123694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // No need for extra uses anymore.
123794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    delete DummyInst;
123854a57045ebcf8e31b1542098d1cd2bda9a718725Duncan Sands
123994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    unsigned NumAddedValues = NewMulOps.size();
124094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    Value *V = EmitAddTreeOfValues(I, NewMulOps);
124154a57045ebcf8e31b1542098d1cd2bda9a718725Duncan Sands
124269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    // Now that we have inserted the add tree, optimize it. This allows us to
124369e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    // handle cases that require multiple factoring steps, such as this:
124494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // A*A*B + A*A*C   -->   A*(A*B+A*C)   -->   A*(A*(B+C))
12459cd1bc4f8b3e98892a2b9856eccd2a2ec9afdf7fChris Lattner    assert(NumAddedValues > 1 && "Each occurrence should contribute a value");
124654a57045ebcf8e31b1542098d1cd2bda9a718725Duncan Sands    (void)NumAddedValues;
1247841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    if (Instruction *VI = dyn_cast<Instruction>(V))
1248841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      RedoInsts.insert(VI);
124969e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner
125069e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    // Create the multiply.
1251841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    Instruction *V2 = BinaryOperator::CreateMul(V, MaxOccVal, "tmp", I);
125269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner
1253f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner    // Rerun associate on the multiply in case the inner expression turned into
1254f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner    // a multiply.  We want to make sure that we keep things in canonical form.
1255841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    RedoInsts.insert(V2);
1256e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
125794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // If every add operand included the factor (e.g. "A*B + A*C"), then the
125894285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // entire result expression is just the multiply "A*(B+C)".
125994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    if (Ops.empty())
126094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner      return V2;
1261e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
12629cd1bc4f8b3e98892a2b9856eccd2a2ec9afdf7fChris Lattner    // Otherwise, we had some input that didn't have the factor, such as
126394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // "A*B + A*C + D" -> "A*(B+C) + D".  Add the new multiply to the list of
12649cd1bc4f8b3e98892a2b9856eccd2a2ec9afdf7fChris Lattner    // things being added by this operation.
126594285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    Ops.insert(Ops.begin(), ValueEntry(getRank(V2), V2));
126694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  }
1267e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1268f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  return 0;
1269f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner}
1270e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner
1271464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruthnamespace {
1272464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  /// \brief Predicate tests whether a ValueEntry's op is in a map.
1273464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  struct IsValueInMap {
1274464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    const DenseMap<Value *, unsigned> &Map;
1275464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1276464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    IsValueInMap(const DenseMap<Value *, unsigned> &Map) : Map(Map) {}
1277464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1278464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    bool operator()(const ValueEntry &Entry) {
1279464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      return Map.find(Entry.Op) != Map.end();
1280464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    }
1281464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  };
1282464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth}
1283464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1284464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// \brief Build up a vector of value/power pairs factoring a product.
1285464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///
1286464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// Given a series of multiplication operands, build a vector of factors and
1287464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// the powers each is raised to when forming the final product. Sort them in
1288464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// the order of descending power.
1289464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///
1290464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///      (x*x)          -> [(x, 2)]
1291464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///     ((x*x)*x)       -> [(x, 3)]
1292464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///   ((((x*y)*x)*y)*x) -> [(x, 3), (y, 2)]
1293464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///
1294464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// \returns Whether any factors have a power greater than one.
1295464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruthbool Reassociate::collectMultiplyFactors(SmallVectorImpl<ValueEntry> &Ops,
1296464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                                         SmallVectorImpl<Factor> &Factors) {
12970fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // FIXME: Have Ops be (ValueEntry, Multiplicity) pairs, simplifying this.
12980fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Compute the sum of powers of simplifiable factors.
1299464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  unsigned FactorPowerSum = 0;
13000fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  for (unsigned Idx = 1, Size = Ops.size(); Idx < Size; ++Idx) {
13010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Value *Op = Ops[Idx-1].Op;
13020fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
13030fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Count the number of occurrences of this value.
13040fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    unsigned Count = 1;
13050fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    for (; Idx < Size && Ops[Idx].Op == Op; ++Idx)
13060fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      ++Count;
1307464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    // Track for simplification all factors which occur 2 or more times.
13080fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    if (Count > 1)
13090fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      FactorPowerSum += Count;
1310464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  }
13110fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
1312464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // We can only simplify factors if the sum of the powers of our simplifiable
1313464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // factors is 4 or higher. When that is the case, we will *always* have
1314464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // a simplification. This is an important invariant to prevent cyclicly
1315464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // trying to simplify already minimal formations.
1316464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (FactorPowerSum < 4)
1317464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    return false;
1318464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
13190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Now gather the simplifiable factors, removing them from Ops.
13200fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  FactorPowerSum = 0;
13210fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  for (unsigned Idx = 1; Idx < Ops.size(); ++Idx) {
13220fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Value *Op = Ops[Idx-1].Op;
1323464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
13240fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Count the number of occurrences of this value.
13250fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    unsigned Count = 1;
13260fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    for (; Idx < Ops.size() && Ops[Idx].Op == Op; ++Idx)
13270fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      ++Count;
13280fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    if (Count == 1)
13290fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      continue;
1330d9b0b025612992a0b724eeca8bdf10b1d7a5c355Benjamin Kramer    // Move an even number of occurrences to Factors.
13310fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Count &= ~1U;
13320fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Idx -= Count;
13330fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    FactorPowerSum += Count;
13340fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Factors.push_back(Factor(Op, Count));
13350fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Ops.erase(Ops.begin()+Idx, Ops.begin()+Idx+Count);
1336464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  }
13370fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
1338464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // None of the adjustments above should have reduced the sum of factor powers
1339464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // below our mininum of '4'.
1340464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  assert(FactorPowerSum >= 4);
1341464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1342464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  std::sort(Factors.begin(), Factors.end(), Factor::PowerDescendingSorter());
1343464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  return true;
1344464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth}
1345464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1346464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// \brief Build a tree of multiplies, computing the product of Ops.
1347464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruthstatic Value *buildMultiplyTree(IRBuilder<> &Builder,
1348464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                                SmallVectorImpl<Value*> &Ops) {
1349464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (Ops.size() == 1)
1350464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    return Ops.back();
1351464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1352464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  Value *LHS = Ops.pop_back_val();
1353464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  do {
1354464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    LHS = Builder.CreateMul(LHS, Ops.pop_back_val());
1355464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  } while (!Ops.empty());
1356464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1357464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  return LHS;
1358464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth}
1359464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1360464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// \brief Build a minimal multiplication DAG for (a^x)*(b^y)*(c^z)*...
1361464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///
1362464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// Given a vector of values raised to various powers, where no two values are
1363464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// equal and the powers are sorted in decreasing order, compute the minimal
1364464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// DAG of multiplies to compute the final product, and return that product
1365464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// value.
1366464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler CarruthValue *Reassociate::buildMinimalMultiplyDAG(IRBuilder<> &Builder,
1367464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                                            SmallVectorImpl<Factor> &Factors) {
1368464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  assert(Factors[0].Power);
1369464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  SmallVector<Value *, 4> OuterProduct;
1370464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  for (unsigned LastIdx = 0, Idx = 1, Size = Factors.size();
1371464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth       Idx < Size && Factors[Idx].Power > 0; ++Idx) {
1372464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    if (Factors[Idx].Power != Factors[LastIdx].Power) {
1373464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      LastIdx = Idx;
1374464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      continue;
1375464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    }
1376464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1377464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    // We want to multiply across all the factors with the same power so that
1378464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    // we can raise them to that power as a single entity. Build a mini tree
1379464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    // for that.
1380464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    SmallVector<Value *, 4> InnerProduct;
1381464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    InnerProduct.push_back(Factors[LastIdx].Base);
1382464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    do {
1383464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      InnerProduct.push_back(Factors[Idx].Base);
1384464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      ++Idx;
1385464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    } while (Idx < Size && Factors[Idx].Power == Factors[LastIdx].Power);
1386464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1387464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    // Reset the base value of the first factor to the new expression tree.
1388464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    // We'll remove all the factors with the same power in a second pass.
1389841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    Value *M = Factors[LastIdx].Base = buildMultiplyTree(Builder, InnerProduct);
1390841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    if (Instruction *MI = dyn_cast<Instruction>(M))
1391841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      RedoInsts.insert(MI);
1392464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1393464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    LastIdx = Idx;
1394464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  }
1395464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // Unique factors with equal powers -- we've folded them into the first one's
1396464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // base.
1397464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  Factors.erase(std::unique(Factors.begin(), Factors.end(),
1398464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                            Factor::PowerEqual()),
1399464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                Factors.end());
1400464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1401464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // Iteratively collect the base of each factor with an add power into the
1402464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // outer product, and halve each power in preparation for squaring the
1403464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // expression.
1404464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  for (unsigned Idx = 0, Size = Factors.size(); Idx != Size; ++Idx) {
1405464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    if (Factors[Idx].Power & 1)
1406464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      OuterProduct.push_back(Factors[Idx].Base);
1407464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    Factors[Idx].Power >>= 1;
1408464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  }
1409464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (Factors[0].Power) {
1410464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    Value *SquareRoot = buildMinimalMultiplyDAG(Builder, Factors);
1411464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    OuterProduct.push_back(SquareRoot);
1412464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    OuterProduct.push_back(SquareRoot);
1413464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  }
1414464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (OuterProduct.size() == 1)
1415464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    return OuterProduct.front();
1416464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1417a33701098936ffba12326d96e98d388357f3e098Duncan Sands  Value *V = buildMultiplyTree(Builder, OuterProduct);
1418a33701098936ffba12326d96e98d388357f3e098Duncan Sands  return V;
1419464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth}
1420464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1421464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler CarruthValue *Reassociate::OptimizeMul(BinaryOperator *I,
1422464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                                SmallVectorImpl<ValueEntry> &Ops) {
1423464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // We can only optimize the multiplies when there is a chain of more than
1424464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // three, such that a balanced tree might require fewer total multiplies.
1425464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (Ops.size() < 4)
1426464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    return 0;
1427464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1428464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // Try to turn linear trees of multiplies without other uses of the
1429464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // intermediate stages into minimal multiply DAGs with perfect sub-expression
1430464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // re-use.
1431464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  SmallVector<Factor, 4> Factors;
1432464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (!collectMultiplyFactors(Ops, Factors))
1433464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    return 0; // All distinct factors, so nothing left for us to do.
1434464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1435464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  IRBuilder<> Builder(I);
1436464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  Value *V = buildMinimalMultiplyDAG(Builder, Factors);
1437464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (Ops.empty())
1438464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    return V;
1439464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1440464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  ValueEntry NewEntry = ValueEntry(getRank(V), V);
1441464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  Ops.insert(std::lower_bound(Ops.begin(), Ops.end(), NewEntry), NewEntry);
1442464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  return 0;
1443464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth}
1444464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1445e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris LattnerValue *Reassociate::OptimizeExpression(BinaryOperator *I,
14469f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner                                       SmallVectorImpl<ValueEntry> &Ops) {
1447469001000620df176decd093a300db84a06cc78bChris Lattner  // Now that we have the linearized expression tree, try to optimize it.
1448469001000620df176decd093a300db84a06cc78bChris Lattner  // Start by folding any constants that we found.
1449e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  if (Ops.size() == 1) return Ops[0].Op;
1450469001000620df176decd093a300db84a06cc78bChris Lattner
1451e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  unsigned Opcode = I->getOpcode();
1452e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1453ec531233a16605756a84d175178e1ee0fac4791cChris Lattner  // Handle destructive annihilation due to identities between elements in the
1454469001000620df176decd093a300db84a06cc78bChris Lattner  // argument list here.
1455464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  unsigned NumOps = Ops.size();
1456109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  switch (Opcode) {
1457109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  default: break;
1458109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  case Instruction::And:
1459109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  case Instruction::Or:
1460464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  case Instruction::Xor:
1461f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    if (Value *Result = OptimizeAndOrXor(Opcode, Ops))
1462f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      return Result;
1463109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner    break;
1464109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner
1465464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  case Instruction::Add:
146694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    if (Value *Result = OptimizeAdd(I, Ops))
1467f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      return Result;
1468464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    break;
1469e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner
1470464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  case Instruction::Mul:
1471464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    if (Value *Result = OptimizeMul(I, Ops))
1472464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      return Result;
1473109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner    break;
1474109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  }
1475109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner
1476841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (Ops.size() != NumOps)
1477e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner    return OptimizeExpression(I, Ops);
1478e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  return 0;
1479469001000620df176decd093a300db84a06cc78bChris Lattner}
1480469001000620df176decd093a300db84a06cc78bChris Lattner
1481841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands/// EraseInst - Zap the given instruction, adding interesting operands to the
1482841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands/// work list.
1483841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsvoid Reassociate::EraseInst(Instruction *I) {
1484841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  assert(isInstructionTriviallyDead(I) && "Trivially dead instructions only!");
1485841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
1486841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  // Erase the dead instruction.
1487841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  ValueRankMap.erase(I);
1488917f99354fa558e50d17191f593f81155b4ab2c3Nick Lewycky  RedoInsts.remove(I);
1489841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  I->eraseFromParent();
1490841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  // Optimize its operands.
1491cd117f736c47947af5c6549734549e135e626c5cDuncan Sands  SmallPtrSet<Instruction *, 8> Visited; // Detect self-referential nodes.
1492841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1493841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    if (Instruction *Op = dyn_cast<Instruction>(Ops[i])) {
1494841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      // If this is a node in an expression tree, climb to the expression root
1495841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      // and add that since that's where optimization actually happens.
1496841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      unsigned Opcode = Op->getOpcode();
1497cd117f736c47947af5c6549734549e135e626c5cDuncan Sands      while (Op->hasOneUse() && Op->use_back()->getOpcode() == Opcode &&
1498cd117f736c47947af5c6549734549e135e626c5cDuncan Sands             Visited.insert(Op))
1499841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        Op = Op->use_back();
1500841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      RedoInsts.insert(Op);
1501841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    }
1502841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands}
1503841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands
1504841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands/// OptimizeInst - Inspect and optimize the given instruction. Note that erasing
1505841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands/// instructions is not allowed.
1506841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsvoid Reassociate::OptimizeInst(Instruction *I) {
1507841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  // Only consider operations that we understand.
1508841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (!isa<BinaryOperator>(I))
1509841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    return;
1510841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands
1511841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (I->getOpcode() == Instruction::Shl &&
1512841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      isa<ConstantInt>(I->getOperand(1)))
1513841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    // If an operand of this shift is a reassociable multiply, or if the shift
1514841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    // is used by a reassociable multiply or add, turn into a multiply.
1515841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    if (isReassociableOp(I->getOperand(0), Instruction::Mul) ||
1516841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        (I->hasOneUse() &&
1517841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands         (isReassociableOp(I->use_back(), Instruction::Mul) ||
1518841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands          isReassociableOp(I->use_back(), Instruction::Add)))) {
1519841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      Instruction *NI = ConvertShiftToMul(I);
1520841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      RedoInsts.insert(I);
1521dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman      MadeChange = true;
1522841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      I = NI;
1523dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman    }
1524641f02f10f08c9a9add651c6f0169f5441eaeb49Chris Lattner
1525423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson  // Floating point binary operators are not associative, but we can still
1526423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson  // commute (some) of them, to canonicalize the order of their operands.
1527423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson  // This can potentially expose more CSE opportunities, and makes writing
1528423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson  // other transformations simpler.
1529841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if ((I->getType()->isFloatingPointTy() || I->getType()->isVectorTy())) {
1530423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    // FAdd and FMul can be commuted.
1531841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    if (I->getOpcode() != Instruction::FMul &&
1532841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        I->getOpcode() != Instruction::FAdd)
1533423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson      return;
1534423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson
1535841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    Value *LHS = I->getOperand(0);
1536841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    Value *RHS = I->getOperand(1);
1537423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    unsigned LHSRank = getRank(LHS);
1538423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    unsigned RHSRank = getRank(RHS);
1539423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson
1540423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    // Sort the operands by rank.
1541423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    if (RHSRank < LHSRank) {
1542841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      I->setOperand(0, RHS);
1543841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      I->setOperand(1, LHS);
1544423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    }
1545423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson
1546423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    return;
1547423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson  }
1548423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson
1549dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // Do not reassociate boolean (i1) expressions.  We want to preserve the
1550dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // original order of evaluation for short-circuited comparisons that
1551dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // SimplifyCFG has folded to AND/OR expressions.  If the expression
1552dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // is not further optimized, it is likely to be transformed back to a
1553dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // short-circuited form for code gen, and the source order may have been
1554dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // optimized for the most likely conditions.
1555841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (I->getType()->isIntegerTy(1))
1556dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman    return;
1557fc375d22001d27ba6d22db67821da057e36f7f89Bob Wilson
1558dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // If this is a subtract instruction which is not already in negate form,
1559dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // see if we can convert it to X+-Y.
1560841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (I->getOpcode() == Instruction::Sub) {
1561841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    if (ShouldBreakUpSubtract(I)) {
1562841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      Instruction *NI = BreakUpSubtract(I);
1563841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      RedoInsts.insert(I);
1564dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman      MadeChange = true;
1565841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      I = NI;
1566841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    } else if (BinaryOperator::isNeg(I)) {
1567dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman      // Otherwise, this is a negation.  See if the operand is a multiply tree
1568dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman      // and if this is not an inner node of a multiply tree.
1569841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      if (isReassociableOp(I->getOperand(1), Instruction::Mul) &&
1570841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands          (!I->hasOneUse() ||
1571841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands           !isReassociableOp(I->use_back(), Instruction::Mul))) {
1572841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        Instruction *NI = LowerNegateToMultiply(I);
1573841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        RedoInsts.insert(I);
1574d5b8d92b9f4dfb216e4f2a52b4e801d7559574baChris Lattner        MadeChange = true;
1575841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        I = NI;
157608b43921e18f314c4fd38049291d323830934c36Chris Lattner      }
1577f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner    }
1578dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  }
1579e4b730441dab4aff9a69aeddbdea98990e7703c4Chris Lattner
1580841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  // If this instruction is an associative binary operator, process it.
1581841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (!I->isAssociative()) return;
1582841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  BinaryOperator *BO = cast<BinaryOperator>(I);
158300b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen
1584dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // If this is an interior node of a reassociable tree, ignore it until we
1585dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // get to the root of the tree, to avoid N^2 analysis.
1586c1deb67d78ac987577e9caa22d60435239ad0e12Nadav Rotem  unsigned Opcode = BO->getOpcode();
1587c1deb67d78ac987577e9caa22d60435239ad0e12Nadav Rotem  if (BO->hasOneUse() && BO->use_back()->getOpcode() == Opcode)
1588dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman    return;
1589c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner
1590e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling  // If this is an add tree that is used by a sub instruction, ignore it
1591dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // until we process the subtract.
1592841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (BO->hasOneUse() && BO->getOpcode() == Instruction::Add &&
1593841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      cast<Instruction>(BO->use_back())->getOpcode() == Instruction::Sub)
1594dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman    return;
15957b4ad94282b94e1827be29b4db73fdf6e241f748Chris Lattner
1596841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  ReassociateExpression(BO);
1597895b392269cad07c34d59110d68dc86708c53adbChris Lattner}
1598c9fd097a01383323f166c14c17d3984620cad766Chris Lattner
1599cd117f736c47947af5c6549734549e135e626c5cDuncan Sandsvoid Reassociate::ReassociateExpression(BinaryOperator *I) {
1600e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
160169e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  // First, walk the expression tree, linearizing the tree, collecting the
160269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  // operand information.
1603c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  SmallVector<RepeatedValue, 8> Tree;
1604c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  MadeChange |= LinearizeExprTree(I, Tree);
16059f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner  SmallVector<ValueEntry, 8> Ops;
1606c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  Ops.reserve(Tree.size());
1607c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  for (unsigned i = 0, e = Tree.size(); i != e; ++i) {
1608c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    RepeatedValue E = Tree[i];
1609c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    Ops.append(E.second.getZExtValue(),
1610c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands               ValueEntry(getRank(E.first), E.first));
1611c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
1612e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
161324dfa52fa20eee39440e5dec4d23359e5a6773c7Duncan Sands  DEBUG(dbgs() << "RAIn:\t"; PrintOps(I, Ops); dbgs() << '\n');
161424dfa52fa20eee39440e5dec4d23359e5a6773c7Duncan Sands
1615895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // Now that we have linearized the tree to a list and have gathered all of
1616895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // the operands and their ranks, sort the operands by their rank.  Use a
1617895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // stable_sort so that values with equal ranks will have their relative
1618895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // positions maintained (and so the compiler is deterministic).  Note that
1619895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // this sorts so that the highest ranking values end up at the beginning of
1620895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // the vector.
1621895b392269cad07c34d59110d68dc86708c53adbChris Lattner  std::stable_sort(Ops.begin(), Ops.end());
1622e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1623895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // OptimizeExpression - Now that we have the expression tree in a convenient
1624895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // sorted form, optimize it globally if possible.
1625895b392269cad07c34d59110d68dc86708c53adbChris Lattner  if (Value *V = OptimizeExpression(I, Ops)) {
1626cd117f736c47947af5c6549734549e135e626c5cDuncan Sands    if (V == I)
1627cd117f736c47947af5c6549734549e135e626c5cDuncan Sands      // Self-referential expression in unreachable code.
1628cd117f736c47947af5c6549734549e135e626c5cDuncan Sands      return;
1629895b392269cad07c34d59110d68dc86708c53adbChris Lattner    // This expression tree simplified to something that isn't a tree,
1630895b392269cad07c34d59110d68dc86708c53adbChris Lattner    // eliminate it.
1631a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene    DEBUG(dbgs() << "Reassoc to scalar: " << *V << '\n');
1632895b392269cad07c34d59110d68dc86708c53adbChris Lattner    I->replaceAllUsesWith(V);
16335367b23f76e75ebb680956575346fa8c3d56780fDevang Patel    if (Instruction *VI = dyn_cast<Instruction>(V))
16345367b23f76e75ebb680956575346fa8c3d56780fDevang Patel      VI->setDebugLoc(I->getDebugLoc());
1635841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    RedoInsts.insert(I);
16369fdaefad580194353f34b6d72669591f8f9d811aChris Lattner    ++NumAnnihil;
1637cd117f736c47947af5c6549734549e135e626c5cDuncan Sands    return;
1638895b392269cad07c34d59110d68dc86708c53adbChris Lattner  }
1639e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1640895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // We want to sink immediates as deeply as possible except in the case where
1641895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // this is a multiply tree used only by an add, and the immediate is a -1.
1642895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // In this case we reassociate to put the negation on the outside so that we
1643895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // can fold the negation into the add: (-X)*Y + Z -> Z-X*Y
1644895b392269cad07c34d59110d68dc86708c53adbChris Lattner  if (I->getOpcode() == Instruction::Mul && I->hasOneUse() &&
1645895b392269cad07c34d59110d68dc86708c53adbChris Lattner      cast<Instruction>(I->use_back())->getOpcode() == Instruction::Add &&
1646895b392269cad07c34d59110d68dc86708c53adbChris Lattner      isa<ConstantInt>(Ops.back().Op) &&
1647895b392269cad07c34d59110d68dc86708c53adbChris Lattner      cast<ConstantInt>(Ops.back().Op)->isAllOnesValue()) {
16489f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner    ValueEntry Tmp = Ops.pop_back_val();
16499f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner    Ops.insert(Ops.begin(), Tmp);
1650895b392269cad07c34d59110d68dc86708c53adbChris Lattner  }
1651e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1652a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene  DEBUG(dbgs() << "RAOut:\t"; PrintOps(I, Ops); dbgs() << '\n');
1653e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1654895b392269cad07c34d59110d68dc86708c53adbChris Lattner  if (Ops.size() == 1) {
1655cd117f736c47947af5c6549734549e135e626c5cDuncan Sands    if (Ops[0].Op == I)
1656cd117f736c47947af5c6549734549e135e626c5cDuncan Sands      // Self-referential expression in unreachable code.
1657cd117f736c47947af5c6549734549e135e626c5cDuncan Sands      return;
1658cd117f736c47947af5c6549734549e135e626c5cDuncan Sands
1659895b392269cad07c34d59110d68dc86708c53adbChris Lattner    // This expression tree simplified to something that isn't a tree,
1660895b392269cad07c34d59110d68dc86708c53adbChris Lattner    // eliminate it.
1661895b392269cad07c34d59110d68dc86708c53adbChris Lattner    I->replaceAllUsesWith(Ops[0].Op);
16625367b23f76e75ebb680956575346fa8c3d56780fDevang Patel    if (Instruction *OI = dyn_cast<Instruction>(Ops[0].Op))
16635367b23f76e75ebb680956575346fa8c3d56780fDevang Patel      OI->setDebugLoc(I->getDebugLoc());
1664841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    RedoInsts.insert(I);
1665cd117f736c47947af5c6549734549e135e626c5cDuncan Sands    return;
16664fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner  }
1667e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
166869e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  // Now that we ordered and optimized the expressions, splat them back into
166969e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  // the expression tree, removing any unneeded nodes.
167069e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  RewriteExprTree(I, Ops);
16714fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner}
16724fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
16737e70829632f82de15db187845666aaca6e04b792Chris Lattnerbool Reassociate::runOnFunction(Function &F) {
1674841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  // Calculate the rank map for F
16754fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner  BuildRankMap(F);
16764fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
1677c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner  MadeChange = false;
1678841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
1679841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    // Optimize every instruction in the basic block.
1680841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE; )
1681841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      if (isInstructionTriviallyDead(II)) {
1682841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        EraseInst(II++);
1683841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      } else {
1684841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        OptimizeInst(II);
1685841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        assert(II->getParent() == BI && "Moved to a different block!");
1686841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        ++II;
1687841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      }
1688841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands
1689841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    // If this produced extra instructions to optimize, handle them now.
1690841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    while (!RedoInsts.empty()) {
1691841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      Instruction *I = RedoInsts.pop_back_val();
1692841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      if (isInstructionTriviallyDead(I))
1693841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        EraseInst(I);
1694841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      else
1695841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        OptimizeInst(I);
1696dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman    }
1697841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  }
16984fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
16990fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // We are done with the rank map.
17000fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  RankMap.clear();
17010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  ValueRankMap.clear();
17020fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
1703c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner  return MadeChange;
17044fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner}
1705