14df2826166f1339eb7ddf5c5c84565fccb794de8Shuxin Yang//===- 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"
25d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/DenseMap.h"
26d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/PostOrderIterator.h"
27d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/STLExtras.h"
28d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/SetVector.h"
29d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/Statistic.h"
30d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Assembly/Writer.h"
310b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h"
320b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
330b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Function.h"
340b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IRBuilder.h"
350b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instructions.h"
360b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IntrinsicInst.h"
374fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner#include "llvm/Pass.h"
384fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner#include "llvm/Support/CFG.h"
39551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h"
40d3c7b7359d4992b9ab9f8e12ccd0a9b7d2446566Chris Lattner#include "llvm/Support/ValueHandle.h"
41bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner#include "llvm/Support/raw_ostream.h"
42d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Transforms/Utils/Local.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  };
1132d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
1142d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  /// Utility class representing a non-constant Xor-operand. We classify
1152d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  /// non-constant Xor-Operands into two categories:
1162d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  ///  C1) The operand is in the form "X & C", where C is a constant and C != ~0
1172d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  ///  C2)
1182d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  ///    C2.1) The operand is in the form of "X | C", where C is a non-zero
1192d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  ///          constant.
1202d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  ///    C2.2) Any operand E which doesn't fall into C1 and C2.1, we view this
1212d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  ///          operand as "E | 0"
1222d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  class XorOpnd {
1232d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  public:
1242d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    XorOpnd(Value *V);
1252d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
1262d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    bool isInvalid() const { return SymbolicPart == 0; }
1272d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    bool isOrExpr() const { return isOr; }
1282d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Value *getValue() const { return OrigVal; }
1292d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Value *getSymbolicPart() const { return SymbolicPart; }
1302d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    unsigned getSymbolicRank() const { return SymbolicRank; }
1312d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    const APInt &getConstPart() const { return ConstPart; }
1322d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
1332d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    void Invalidate() { SymbolicPart = OrigVal = 0; }
1342d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    void setSymbolicRank(unsigned R) { SymbolicRank = R; }
1352d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
1362d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    // Sort the XorOpnd-Pointer in ascending order of symbolic-value-rank.
1372d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    // The purpose is twofold:
1382d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    // 1) Cluster together the operands sharing the same symbolic-value.
1392d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    // 2) Operand having smaller symbolic-value-rank is permuted earlier, which
1402d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    //   could potentially shorten crital path, and expose more loop-invariants.
1412d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    //   Note that values' rank are basically defined in RPO order (FIXME).
1422d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    //   So, if Rank(X) < Rank(Y) < Rank(Z), it means X is defined earlier
1432d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    //   than Y which is defined earlier than Z. Permute "x | 1", "Y & 2",
1442d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    //   "z" in the order of X-Y-Z is better than any other orders.
1454fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang    struct PtrSortFunctor {
1464fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang      bool operator()(XorOpnd * const &LHS, XorOpnd * const &RHS) {
1474fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang        return LHS->getSymbolicRank() < RHS->getSymbolicRank();
1482d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      }
1492d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    };
1502d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  private:
1512d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Value *OrigVal;
1522d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Value *SymbolicPart;
1532d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    APInt ConstPart;
1542d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    unsigned SymbolicRank;
1552d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    bool isOr;
1562d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  };
157464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth}
158464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
159464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruthnamespace {
1603e8b6631e67e01e4960a7ba4668a50c596607473Chris Lattner  class Reassociate : public FunctionPass {
161f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner    DenseMap<BasicBlock*, unsigned> RankMap;
162f1d0f7781e766df878bec4e7977fa3204374f394Craig Topper    DenseMap<AssertingVH<Value>, unsigned> ValueRankMap;
1634df2826166f1339eb7ddf5c5c84565fccb794de8Shuxin Yang    SetVector<AssertingVH<Instruction> > RedoInsts;
164c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner    bool MadeChange;
1654fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner  public:
166ecd94c804a563f2a86572dcf1d2e81f397e19daaNick Lewycky    static char ID; // Pass identification, replacement for typeid
167081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    Reassociate() : FunctionPass(ID) {
168081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson      initializeReassociatePass(*PassRegistry::getPassRegistry());
169081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    }
170794fd75c67a2cdc128d67342c6d88a504d186896Devang Patel
1717e70829632f82de15db187845666aaca6e04b792Chris Lattner    bool runOnFunction(Function &F);
1724fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
1734fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
174cb2610ea037a17115ef3a01a6bdaab4e3cfdca27Chris Lattner      AU.setPreservesCFG();
1754fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner    }
1764fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner  private:
1777e70829632f82de15db187845666aaca6e04b792Chris Lattner    void BuildRankMap(Function &F);
1784fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner    unsigned getRank(Value *V);
179cd117f736c47947af5c6549734549e135e626c5cDuncan Sands    void ReassociateExpression(BinaryOperator *I);
1800fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    void RewriteExprTree(BinaryOperator *I, SmallVectorImpl<ValueEntry> &Ops);
1819f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner    Value *OptimizeExpression(BinaryOperator *I,
1829f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner                              SmallVectorImpl<ValueEntry> &Ops);
1839f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner    Value *OptimizeAdd(Instruction *I, SmallVectorImpl<ValueEntry> &Ops);
1842d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Value *OptimizeXor(Instruction *I, SmallVectorImpl<ValueEntry> &Ops);
1852d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    bool CombineXorOpnd(Instruction *I, XorOpnd *Opnd1, APInt &ConstOpnd,
1862d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang                        Value *&Res);
1872d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    bool CombineXorOpnd(Instruction *I, XorOpnd *Opnd1, XorOpnd *Opnd2,
1882d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang                        APInt &ConstOpnd, Value *&Res);
189464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    bool collectMultiplyFactors(SmallVectorImpl<ValueEntry> &Ops,
190464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                                SmallVectorImpl<Factor> &Factors);
191464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    Value *buildMinimalMultiplyDAG(IRBuilder<> &Builder,
192464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                                   SmallVectorImpl<Factor> &Factors);
193464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    Value *OptimizeMul(BinaryOperator *I, SmallVectorImpl<ValueEntry> &Ops);
194e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner    Value *RemoveFactorFromExpression(Value *V, Value *Factor);
195841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    void EraseInst(Instruction *I);
196841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    void OptimizeInst(Instruction *I);
1974fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner  };
1984fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner}
1994fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
2002d1001064989b7fa79507816fc17d467fc00a2f2Shuxin YangXorOpnd::XorOpnd(Value *V) {
201ad26993e1a9b147c3ca4b170ab2eba260f89a1acShuxin Yang  assert(!isa<ConstantInt>(V) && "No ConstantInt");
2022d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  OrigVal = V;
2032d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  Instruction *I = dyn_cast<Instruction>(V);
2042d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  SymbolicRank = 0;
2052d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
2062d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  if (I && (I->getOpcode() == Instruction::Or ||
2072d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang            I->getOpcode() == Instruction::And)) {
2082d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Value *V0 = I->getOperand(0);
2092d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Value *V1 = I->getOperand(1);
2102d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (isa<ConstantInt>(V0))
2112d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      std::swap(V0, V1);
2122d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
2132d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (ConstantInt *C = dyn_cast<ConstantInt>(V1)) {
2142d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      ConstPart = C->getValue();
2152d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      SymbolicPart = V0;
2162d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      isOr = (I->getOpcode() == Instruction::Or);
2172d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      return;
2182d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    }
2192d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  }
2202d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
2212d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  // view the operand as "V | 0"
2222d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  SymbolicPart = V;
2232d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  ConstPart = APInt::getNullValue(V->getType()->getIntegerBitWidth());
2242d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  isOr = true;
2252d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang}
2262d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
227844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar Reassociate::ID = 0;
228d13db2c59cc94162d6cf0a04187d408bfef6d4a7Owen AndersonINITIALIZE_PASS(Reassociate, "reassociate",
229ce665bd2e2b581ab0858d1afe359192bac96b868Owen Anderson                "Reassociate expressions", false, false)
230844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
231d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke// Public interface to the Reassociate pass
232d7456026629fc1760a45e6e955e9834246493147Chris LattnerFunctionPass *llvm::createReassociatePass() { return new Reassociate(); }
2334fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
2340fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// isReassociableOp - Return true if V is an instruction of the specified
2350fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// opcode and if it only has one use.
2360fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sandsstatic BinaryOperator *isReassociableOp(Value *V, unsigned Opcode) {
2370fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  if (V->hasOneUse() && isa<Instruction>(V) &&
2380fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      cast<Instruction>(V)->getOpcode() == Opcode)
2390fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    return cast<BinaryOperator>(V);
2400fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  return 0;
2410fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands}
2420fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
2439c723199384b16899831937e2800d52f4f953569Chris Lattnerstatic bool isUnmovableInstruction(Instruction *I) {
244eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  switch (I->getOpcode()) {
245eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  case Instruction::PHI:
246eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  case Instruction::LandingPad:
247eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  case Instruction::Alloca:
248eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  case Instruction::Load:
249eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  case Instruction::Invoke:
250eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  case Instruction::UDiv:
251eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  case Instruction::SDiv:
252eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  case Instruction::FDiv:
253eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  case Instruction::URem:
254eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  case Instruction::SRem:
255eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  case Instruction::FRem:
2569c723199384b16899831937e2800d52f4f953569Chris Lattner    return true;
257eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  case Instruction::Call:
258eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak    return !isa<DbgInfoIntrinsic>(I);
259eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  default:
260eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak    return false;
261eb0588b9920984fed0e6740a52d0e36feeaa9904Jakub Staszak  }
2629c723199384b16899831937e2800d52f4f953569Chris Lattner}
2639c723199384b16899831937e2800d52f4f953569Chris Lattner
2647e70829632f82de15db187845666aaca6e04b792Chris Lattnervoid Reassociate::BuildRankMap(Function &F) {
2656007cb6c4d923e2dee4a1133fb6d1bb00a37062dChris Lattner  unsigned i = 2;
266fb5be090f59997deb7a2e89c92bac19528ba6755Chris Lattner
267fb5be090f59997deb7a2e89c92bac19528ba6755Chris Lattner  // Assign distinct ranks to function arguments
268e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner  for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
269d3c7b7359d4992b9ab9f8e12ccd0a9b7d2446566Chris Lattner    ValueRankMap[&*I] = ++i;
270fb5be090f59997deb7a2e89c92bac19528ba6755Chris Lattner
2717e70829632f82de15db187845666aaca6e04b792Chris Lattner  ReversePostOrderTraversal<Function*> RPOT(&F);
2724fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner  for (ReversePostOrderTraversal<Function*>::rpo_iterator I = RPOT.begin(),
2739c723199384b16899831937e2800d52f4f953569Chris Lattner         E = RPOT.end(); I != E; ++I) {
2749c723199384b16899831937e2800d52f4f953569Chris Lattner    BasicBlock *BB = *I;
2759c723199384b16899831937e2800d52f4f953569Chris Lattner    unsigned BBRank = RankMap[BB] = ++i << 16;
2769c723199384b16899831937e2800d52f4f953569Chris Lattner
2779c723199384b16899831937e2800d52f4f953569Chris Lattner    // Walk the basic block, adding precomputed ranks for any instructions that
2789c723199384b16899831937e2800d52f4f953569Chris Lattner    // we cannot move.  This ensures that the ranks for these instructions are
2799c723199384b16899831937e2800d52f4f953569Chris Lattner    // all different in the block.
2809c723199384b16899831937e2800d52f4f953569Chris Lattner    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
2819c723199384b16899831937e2800d52f4f953569Chris Lattner      if (isUnmovableInstruction(I))
282d3c7b7359d4992b9ab9f8e12ccd0a9b7d2446566Chris Lattner        ValueRankMap[&*I] = ++BBRank;
2839c723199384b16899831937e2800d52f4f953569Chris Lattner  }
2844fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner}
2854fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
2864fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattnerunsigned Reassociate::getRank(Value *V) {
28708b43921e18f314c4fd38049291d323830934c36Chris Lattner  Instruction *I = dyn_cast<Instruction>(V);
288f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner  if (I == 0) {
289f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner    if (isa<Argument>(V)) return ValueRankMap[V];   // Function argument.
290f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner    return 0;  // Otherwise it's a global or constant, rank 0.
291f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner  }
2924fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
293f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner  if (unsigned Rank = ValueRankMap[I])
294f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner    return Rank;    // Rank already known?
29500b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen
29608b43921e18f314c4fd38049291d323830934c36Chris Lattner  // If this is an expression, return the 1+MAX(rank(LHS), rank(RHS)) so that
29708b43921e18f314c4fd38049291d323830934c36Chris Lattner  // we can reassociate expressions for code motion!  Since we do not recurse
29808b43921e18f314c4fd38049291d323830934c36Chris Lattner  // for PHI nodes, we cannot have infinite recursion here, because there
29908b43921e18f314c4fd38049291d323830934c36Chris Lattner  // cannot be loops in the value graph that do not go through PHI nodes.
30008b43921e18f314c4fd38049291d323830934c36Chris Lattner  unsigned Rank = 0, MaxRank = RankMap[I->getParent()];
30108b43921e18f314c4fd38049291d323830934c36Chris Lattner  for (unsigned i = 0, e = I->getNumOperands();
30208b43921e18f314c4fd38049291d323830934c36Chris Lattner       i != e && Rank != MaxRank; ++i)
30308b43921e18f314c4fd38049291d323830934c36Chris Lattner    Rank = std::max(Rank, getRank(I->getOperand(i)));
30400b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen
305cc8a2b98f28c10d93f45489b8c6f0c8b8205bb3bChris Lattner  // If this is a not or neg instruction, do not count it for rank.  This
306cc8a2b98f28c10d93f45489b8c6f0c8b8205bb3bChris Lattner  // assures us that X and ~X will have the same rank.
307b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands  if (!I->getType()->isIntegerTy() ||
308fa82b6eba4e1584d7dba291c28fe908272e1e002Owen Anderson      (!BinaryOperator::isNot(I) && !BinaryOperator::isNeg(I)))
309cc8a2b98f28c10d93f45489b8c6f0c8b8205bb3bChris Lattner    ++Rank;
310cc8a2b98f28c10d93f45489b8c6f0c8b8205bb3bChris Lattner
311a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene  //DEBUG(dbgs() << "Calculated Rank[" << V->getName() << "] = "
312bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner  //     << Rank << "\n");
31300b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen
314f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner  return ValueRankMap[I] = Rank;
3154fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner}
3164fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
317f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner/// LowerNegateToMultiply - Replace 0-X with X*-1.
318f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner///
319841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsstatic BinaryOperator *LowerNegateToMultiply(Instruction *Neg) {
320a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson  Constant *Cst = Constant::getAllOnesValue(Neg->getType());
321f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner
3220fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  BinaryOperator *Res =
3230fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    BinaryOperator::CreateMul(Neg->getOperand(1), Cst, "",Neg);
324841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Neg->setOperand(1, Constant::getNullValue(Neg->getType())); // Drop use of op.
3256934a04a8c15e9971cd1ea4d5c8df2d7afdd5be5Chris Lattner  Res->takeName(Neg);
326f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner  Neg->replaceAllUsesWith(Res);
3275367b23f76e75ebb680956575346fa8c3d56780fDevang Patel  Res->setDebugLoc(Neg->getDebugLoc());
328f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner  return Res;
329f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner}
330f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner
331c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// CarmichaelShift - Returns k such that lambda(2^Bitwidth) = 2^k, where lambda
332c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// is the Carmichael function. This means that x^(2^k) === 1 mod 2^Bitwidth for
333c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// every odd x, i.e. x^(2^k) = 1 for every odd x in Bitwidth-bit arithmetic.
334c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// Note that 0 <= k < Bitwidth, and if Bitwidth > 3 then x^(2^k) = 0 for every
335c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// even x in Bitwidth-bit arithmetic.
336c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandsstatic unsigned CarmichaelShift(unsigned Bitwidth) {
337c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (Bitwidth < 3)
338c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    return Bitwidth - 1;
339c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  return Bitwidth - 2;
340c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands}
341c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
342c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// IncorporateWeight - Add the extra weight 'RHS' to the existing weight 'LHS',
343c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// reducing the combined weight using any special properties of the operation.
344c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// The existing weight LHS represents the computation X op X op ... op X where
345c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// X occurs LHS times.  The combined weight represents  X op X op ... op X with
346c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// X occurring LHS + RHS times.  If op is "Xor" for example then the combined
347c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// operation is equivalent to X if LHS + RHS is odd, or 0 if LHS + RHS is even;
348c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// the routine returns 1 in LHS in the first case, and 0 in LHS in the second.
349c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandsstatic void IncorporateWeight(APInt &LHS, const APInt &RHS, unsigned Opcode) {
350c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // If we were working with infinite precision arithmetic then the combined
351c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // weight would be LHS + RHS.  But we are using finite precision arithmetic,
352c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // and the APInt sum LHS + RHS may not be correct if it wraps (it is correct
353c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // for nilpotent operations and addition, but not for idempotent operations
354c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // and multiplication), so it is important to correctly reduce the combined
355c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // weight back into range if wrapping would be wrong.
356c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
357c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // If RHS is zero then the weight didn't change.
358c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (RHS.isMinValue())
359c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    return;
360c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // If LHS is zero then the combined weight is RHS.
361c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (LHS.isMinValue()) {
362c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    LHS = RHS;
363c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    return;
364c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
365c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // From this point on we know that neither LHS nor RHS is zero.
366c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
367c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (Instruction::isIdempotent(Opcode)) {
368c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // Idempotent means X op X === X, so any non-zero weight is equivalent to a
369c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // weight of 1.  Keeping weights at zero or one also means that wrapping is
370c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // not a problem.
371c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    assert(LHS == 1 && RHS == 1 && "Weights not reduced!");
372c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    return; // Return a weight of 1.
373c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
374c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (Instruction::isNilpotent(Opcode)) {
375c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // Nilpotent means X op X === 0, so reduce weights modulo 2.
376c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    assert(LHS == 1 && RHS == 1 && "Weights not reduced!");
377c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    LHS = 0; // 1 + 1 === 0 modulo 2.
378c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    return;
379c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
380c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (Opcode == Instruction::Add) {
381c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // TODO: Reduce the weight by exploiting nsw/nuw?
382c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    LHS += RHS;
383c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    return;
384c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
385c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
386c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  assert(Opcode == Instruction::Mul && "Unknown associative operation!");
387c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  unsigned Bitwidth = LHS.getBitWidth();
388c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // If CM is the Carmichael number then a weight W satisfying W >= CM+Bitwidth
389c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // can be replaced with W-CM.  That's because x^W=x^(W-CM) for every Bitwidth
390c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // bit number x, since either x is odd in which case x^CM = 1, or x is even in
391c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // which case both x^W and x^(W - CM) are zero.  By subtracting off multiples
392c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // of CM like this weights can always be reduced to the range [0, CM+Bitwidth)
393c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // which by a happy accident means that they can always be represented using
394c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // Bitwidth bits.
395c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // TODO: Reduce the weight by exploiting nsw/nuw?  (Could do much better than
396c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // the Carmichael number).
397c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (Bitwidth > 3) {
398c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    /// CM - The value of Carmichael's lambda function.
399c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    APInt CM = APInt::getOneBitSet(Bitwidth, CarmichaelShift(Bitwidth));
400c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // Any weight W >= Threshold can be replaced with W - CM.
401c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    APInt Threshold = CM + Bitwidth;
402c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    assert(LHS.ult(Threshold) && RHS.ult(Threshold) && "Weights not reduced!");
403c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // For Bitwidth 4 or more the following sum does not overflow.
404c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    LHS += RHS;
405c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    while (LHS.uge(Threshold))
406c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      LHS -= CM;
407c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  } else {
408c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // To avoid problems with overflow do everything the same as above but using
409c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    // a larger type.
410c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    unsigned CM = 1U << CarmichaelShift(Bitwidth);
411c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    unsigned Threshold = CM + Bitwidth;
412c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    assert(LHS.getZExtValue() < Threshold && RHS.getZExtValue() < Threshold &&
413c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands           "Weights not reduced!");
414c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    unsigned Total = LHS.getZExtValue() + RHS.getZExtValue();
415c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    while (Total >= Threshold)
416c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      Total -= CM;
417c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    LHS = Total;
418c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
419c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands}
420c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
421c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandstypedef std::pair<Value*, APInt> RepeatedValue;
422c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
4230fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// LinearizeExprTree - Given an associative binary expression, return the leaf
424c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// nodes in Ops along with their weights (how many times the leaf occurs).  The
425c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// original expression is the same as
426c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands///   (Ops[0].first op Ops[0].first op ... Ops[0].first)  <- Ops[0].second times
427a94d6e87c4c49f2e81b01d66d8bfb591277f8f96Nadav Rotem/// op
428c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands///   (Ops[1].first op Ops[1].first op ... Ops[1].first)  <- Ops[1].second times
429c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// op
430c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands///   ...
431c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// op
432c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands///   (Ops[N].first op Ops[N].first op ... Ops[N].first)  <- Ops[N].second times
433c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands///
4347ecfcc163956a9e27845ac217f6c650658631030Duncan Sands/// Note that the values Ops[0].first, ..., Ops[N].first are all distinct.
435c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands///
436c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// This routine may modify the function, in which case it returns 'true'.  The
437c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// changes it makes may well be destructive, changing the value computed by 'I'
438c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// to something completely different.  Thus if the routine returns 'true' then
439c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// you MUST either replace I with a new expression computed from the Ops array,
440c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// or use RewriteExprTree to put the values back in.
4410fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
4420fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// A leaf node is either not a binary operation of the same kind as the root
4430fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// node 'I' (i.e. is not a binary operator at all, or is, but with a different
4440fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// opcode), or is the same kind of binary operator but has a use which either
4450fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// does not belong to the expression, or does belong to the expression but is
4460fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// a leaf node.  Every leaf node has at least one use that is a non-leaf node
4470fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// of the expression, while for non-leaf nodes (except for the root 'I') every
4480fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// use is a non-leaf node of the expression.
4490fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
4500fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// For example:
4510fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///           expression graph        node names
4520fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
4530fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                     +        |        I
4540fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                    / \       |
4550fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                   +   +      |      A,  B
4560fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                  / \ / \     |
4570fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                 *   +   *    |    C,  D,  E
4580fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                / \ / \ / \   |
4590fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                   +   *      |      F,  G
4600fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
4610fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// The leaf nodes are C, E, F and G.  The Ops array will contain (maybe not in
462c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// that order) (C, 1), (E, 1), (F, 2), (G, 2).
4630fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
4640fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// The expression is maximal: if some instruction is a binary operator of the
4650fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// same kind as 'I', and all of its uses are non-leaf nodes of the expression,
4660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// then the instruction also belongs to the expression, is not a leaf node of
4670fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// it, and its operands also belong to the expression (but may be leaf nodes).
468c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner///
4690fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// NOTE: This routine will set operands of non-leaf non-root nodes to undef in
4700fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// order to ensure that every non-root node in the expression has *exactly one*
4710fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// use by a non-leaf node of the expression.  This destruction means that the
472eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands/// caller MUST either replace 'I' with a new expression or use something like
473c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// RewriteExprTree to put the values back in if the routine indicates that it
474c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// made a change by returning 'true'.
475e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner///
4760fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// In the above example either the right operand of A or the left operand of B
4770fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// will be replaced by undef.  If it is B's operand then this gives:
4780fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
4790fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                     +        |        I
4800fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                    / \       |
4810fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                   +   +      |      A,  B - operand of B replaced with undef
4820fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                  / \   \     |
4830fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                 *   +   *    |    C,  D,  E
4840fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                / \ / \ / \   |
4850fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///                   +   *      |      F,  G
4860fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
487eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands/// Note that such undef operands can only be reached by passing through 'I'.
488eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands/// For example, if you visit operands recursively starting from a leaf node
489eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands/// then you will never see such an undef operand unless you get back to 'I',
4900fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// which requires passing through a phi node.
4910fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands///
4920fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// Note that this routine may also mutate binary operators of the wrong type
4930fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// that have all uses inside the expression (i.e. only used by non-leaf nodes
4940fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// of the expression) if it can turn them into binary operators of the right
4950fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// type and thus make the expression bigger.
4960fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
497c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandsstatic bool LinearizeExprTree(BinaryOperator *I,
498c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands                              SmallVectorImpl<RepeatedValue> &Ops) {
4990fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  DEBUG(dbgs() << "LINEARIZE: " << *I << '\n');
500c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  unsigned Bitwidth = I->getType()->getScalarType()->getPrimitiveSizeInBits();
501c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  unsigned Opcode = I->getOpcode();
502c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  assert(Instruction::isAssociative(Opcode) &&
503c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands         Instruction::isCommutative(Opcode) &&
504c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands         "Expected an associative and commutative operation!");
5050fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
5060fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Visit all operands of the expression, keeping track of their weight (the
5070fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // number of paths from the expression root to the operand, or if you like
5080fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // the number of times that operand occurs in the linearized expression).
5090fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // For example, if I = X + A, where X = A + B, then I, X and B have weight 1
5100fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // while A has weight two.
5110fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
5120fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Worklist of non-leaf nodes (their operands are in the expression too) along
5130fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // with their weights, representing a certain number of paths to the operator.
5140fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // If an operator occurs in the worklist multiple times then we found multiple
5150fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // ways to get to it.
516c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  SmallVector<std::pair<BinaryOperator*, APInt>, 8> Worklist; // (Op, Weight)
517c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  Worklist.push_back(std::make_pair(I, APInt(Bitwidth, 1)));
518c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  bool MadeChange = false;
519c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner
5200fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Leaves of the expression are values that either aren't the right kind of
5210fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // operation (eg: a constant, or a multiply in an add tree), or are, but have
5220fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // some uses that are not inside the expression.  For example, in I = X + X,
5230fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // X = A + B, the value X has two uses (by I) that are in the expression.  If
5240fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // X has any other uses, for example in a return instruction, then we consider
5250fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // X to be a leaf, and won't analyze it further.  When we first visit a value,
5260fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // if it has more than one use then at first we conservatively consider it to
5270fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // be a leaf.  Later, as the expression is explored, we may discover some more
5280fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // uses of the value from inside the expression.  If all uses turn out to be
5290fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // from within the expression (and the value is a binary operator of the right
5300fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // kind) then the value is no longer considered to be a leaf, and its operands
5310fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // are explored.
5320fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
5330fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Leaves - Keeps track of the set of putative leaves as well as the number of
5340fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // paths to each leaf seen so far.
5355f9e4c1189ab4a8ea1b0000d9337060ac3cac26eDuncan Sands  typedef DenseMap<Value*, APInt> LeafMap;
5360fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  LeafMap Leaves; // Leaf -> Total weight so far.
5370fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  SmallVector<Value*, 8> LeafOrder; // Ensure deterministic leaf output order.
538f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner
5390fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands#ifndef NDEBUG
5400fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  SmallPtrSet<Value*, 8> Visited; // For sanity checking the iteration scheme.
5410fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands#endif
5420fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  while (!Worklist.empty()) {
543c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    std::pair<BinaryOperator*, APInt> P = Worklist.pop_back_val();
5440fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    I = P.first; // We examine the operands of this binary operator.
5450fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
5460fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    for (unsigned OpIdx = 0; OpIdx < 2; ++OpIdx) { // Visit operands.
5470fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      Value *Op = I->getOperand(OpIdx);
548c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      APInt Weight = P.second; // Number of paths to this operand.
5490fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      DEBUG(dbgs() << "OPERAND: " << *Op << " (" << Weight << ")\n");
5500fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      assert(!Op->use_empty() && "No uses, so how did we get to it?!");
5510fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
5520fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // If this is a binary operation of the right kind with only one use then
5530fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // add its operands to the expression.
5540fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (BinaryOperator *BO = isReassociableOp(Op, Opcode)) {
5550fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        assert(Visited.insert(Op) && "Not first visit!");
5560fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        DEBUG(dbgs() << "DIRECT ADD: " << *Op << " (" << Weight << ")\n");
5570fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Worklist.push_back(std::make_pair(BO, Weight));
5580fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        continue;
5590fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
560e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
5610fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // Appears to be a leaf.  Is the operand already in the set of leaves?
5620fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      LeafMap::iterator It = Leaves.find(Op);
5630fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (It == Leaves.end()) {
5640fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // Not in the leaf map.  Must be the first time we saw this operand.
5650fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        assert(Visited.insert(Op) && "Not first visit!");
5660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        if (!Op->hasOneUse()) {
5670fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          // This value has uses not accounted for by the expression, so it is
5680fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          // not safe to modify.  Mark it as being a leaf.
5690fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          DEBUG(dbgs() << "ADD USES LEAF: " << *Op << " (" << Weight << ")\n");
5700fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          LeafOrder.push_back(Op);
5710fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          Leaves[Op] = Weight;
5720fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          continue;
5730fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        }
5740fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // No uses outside the expression, try morphing it.
5750fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      } else if (It != Leaves.end()) {
5760fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // Already in the leaf map.
5770fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        assert(Visited.count(Op) && "In leaf map but not visited!");
5780fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
5790fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // Update the number of paths to the leaf.
580c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands        IncorporateWeight(It->second, Weight, Opcode);
5810fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
58220b2d21509b3d5a10ec5d7be6dea8afa9e92fdeeDuncan Sands#if 0   // TODO: Re-enable once PR13021 is fixed.
5830fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // The leaf already has one use from inside the expression.  As we want
5840fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // exactly one such use, drop this new use of the leaf.
5850fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        assert(!Op->hasOneUse() && "Only one use, but we got here twice!");
5860fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        I->setOperand(OpIdx, UndefValue::get(I->getType()));
5870fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        MadeChange = true;
588e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
5890fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // If the leaf is a binary operation of the right kind and we now see
5900fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // that its multiple original uses were in fact all by nodes belonging
5910fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // to the expression, then no longer consider it to be a leaf and add
5920fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // its operands to the expression.
5930fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        if (BinaryOperator *BO = isReassociableOp(Op, Opcode)) {
5940fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          DEBUG(dbgs() << "UNLEAF: " << *Op << " (" << It->second << ")\n");
5950fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          Worklist.push_back(std::make_pair(BO, It->second));
5960fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          Leaves.erase(It);
5970fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          continue;
5980fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        }
59920b2d21509b3d5a10ec5d7be6dea8afa9e92fdeeDuncan Sands#endif
600fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
6010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // If we still have uses that are not accounted for by the expression
6020fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // then it is not safe to modify the value.
6030fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        if (!Op->hasOneUse())
6040fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          continue;
6054fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
6060fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // No uses outside the expression, try morphing it.
6070fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Weight = It->second;
6080fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Leaves.erase(It); // Since the value may be morphed below.
6090fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
610c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner
6110fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // At this point we have a value which, first of all, is not a binary
6120fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // expression of the right kind, and secondly, is only used inside the
6130fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // expression.  This means that it can safely be modified.  See if we
6140fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // can usefully morph it into an expression of the right kind.
6150fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      assert((!isa<Instruction>(Op) ||
6160fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands              cast<Instruction>(Op)->getOpcode() != Opcode) &&
6170fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands             "Should have been handled above!");
6180fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      assert(Op->hasOneUse() && "Has uses outside the expression tree!");
6190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
6200fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // If this is a multiply expression, turn any internal negations into
6210fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // multiplies by -1 so they can be reassociated.
6220fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      BinaryOperator *BO = dyn_cast<BinaryOperator>(Op);
6230fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (Opcode == Instruction::Mul && BO && BinaryOperator::isNeg(BO)) {
6240fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        DEBUG(dbgs() << "MORPH LEAF: " << *Op << " (" << Weight << ") TO ");
625841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        BO = LowerNegateToMultiply(BO);
6260fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        DEBUG(dbgs() << *BO << 'n');
6270fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Worklist.push_back(std::make_pair(BO, Weight));
6280fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        MadeChange = true;
6290fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        continue;
6300fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
631c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner
6320fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // Failed to morph into an expression of the right type.  This really is
6330fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // a leaf.
6340fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      DEBUG(dbgs() << "ADD LEAF: " << *Op << " (" << Weight << ")\n");
6350fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      assert(!isReassociableOp(Op, Opcode) && "Value was morphed?");
6360fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      LeafOrder.push_back(Op);
6370fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      Leaves[Op] = Weight;
6380fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    }
6390fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  }
640e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
6410fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // The leaves, repeated according to their weights, represent the linearized
6420fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // form of the expression.
6430fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  for (unsigned i = 0, e = LeafOrder.size(); i != e; ++i) {
6440fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Value *V = LeafOrder[i];
6450fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    LeafMap::iterator It = Leaves.find(V);
6460fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    if (It == Leaves.end())
647c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      // Node initially thought to be a leaf wasn't.
6480fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      continue;
6490fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    assert(!isReassociableOp(V, Opcode) && "Shouldn't be a leaf!");
650c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    APInt Weight = It->second;
651c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    if (Weight.isMinValue())
652c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      // Leaf already output or weight reduction eliminated it.
653c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands      continue;
6540fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Ensure the leaf is only output once.
655c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    It->second = 0;
656c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    Ops.push_back(std::make_pair(V, Weight));
657c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
658c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
659c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // For nilpotent operations or addition there may be no operands, for example
660c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // because the expression was "X xor X" or consisted of 2^Bitwidth additions:
661c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  // in both cases the weight reduces to 0 causing the value to be skipped.
662c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  if (Ops.empty()) {
6637ecfcc163956a9e27845ac217f6c650658631030Duncan Sands    Constant *Identity = ConstantExpr::getBinOpIdentity(Opcode, I->getType());
664ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands    assert(Identity && "Associative operation without identity!");
665c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    Ops.push_back(std::make_pair(Identity, APInt(Bitwidth, 1)));
6660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  }
667c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands
668c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  return MadeChange;
669c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner}
670c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner
671c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner// RewriteExprTree - Now that the operands for this expression tree are
6720fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands// linearized and optimized, emit them in-order.
673e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattnervoid Reassociate::RewriteExprTree(BinaryOperator *I,
6740fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands                                  SmallVectorImpl<ValueEntry> &Ops) {
6750fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  assert(Ops.size() > 1 && "Single values should be used directly!");
6760fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
677b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  // Since our optimizations should never increase the number of operations, the
678b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  // new expression can usually be written reusing the existing binary operators
6790fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // from the original expression tree, without creating any new instructions,
6800fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // though the rewritten expression may have a completely different topology.
6810fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // We take care to not change anything if the new expression will be the same
6820fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // as the original.  If more than trivial changes (like commuting operands)
6830fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // were made then we are obliged to clear out any optional subclass data like
6840fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // nsw flags.
6850fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
6860fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  /// NodesToRewrite - Nodes from the original expression available for writing
6870fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  /// the new expression into.
6880fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  SmallVector<BinaryOperator*, 8> NodesToRewrite;
6890fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  unsigned Opcode = I->getOpcode();
6902923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands  BinaryOperator *Op = I;
6910fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
692b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  /// NotRewritable - The operands being written will be the leaves of the new
693b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  /// expression and must not be used as inner nodes (via NodesToRewrite) by
694b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  /// mistake.  Inner nodes are always reassociable, and usually leaves are not
695b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  /// (if they were they would have been incorporated into the expression and so
696b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  /// would not be leaves), so most of the time there is no danger of this.  But
697b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  /// in rare cases a leaf may become reassociable if an optimization kills uses
698b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  /// of it, or it may momentarily become reassociable during rewriting (below)
699b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  /// due it being removed as an operand of one of its uses.  Ensure that misuse
700b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  /// of leaf nodes as inner nodes cannot occur by remembering all of the future
701b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  /// leaves and refusing to reuse any of them as inner nodes.
702b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  SmallPtrSet<Value*, 8> NotRewritable;
703b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands  for (unsigned i = 0, e = Ops.size(); i != e; ++i)
704b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands    NotRewritable.insert(Ops[i].Op);
705b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands
706eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands  // ExpressionChanged - Non-null if the rewritten expression differs from the
707eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands  // original in some non-trivial way, requiring the clearing of optional flags.
708eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands  // Flags are cleared from the operator in ExpressionChanged up to I inclusive.
709eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands  BinaryOperator *ExpressionChanged = 0;
7102d5f8ca3d180832d168e59e2bf3d85317e86287dDuncan Sands  for (unsigned i = 0; ; ++i) {
7110fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // The last operation (which comes earliest in the IR) is special as both
7120fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // operands will come from Ops, rather than just one with the other being
7130fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // a subexpression.
7140fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    if (i+2 == Ops.size()) {
7150fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      Value *NewLHS = Ops[i].Op;
7160fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      Value *NewRHS = Ops[i+1].Op;
7170fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      Value *OldLHS = Op->getOperand(0);
7180fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      Value *OldRHS = Op->getOperand(1);
7190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
7200fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (NewLHS == OldLHS && NewRHS == OldRHS)
7210fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // Nothing changed, leave it alone.
7220fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        break;
7230fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
7240fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (NewLHS == OldRHS && NewRHS == OldLHS) {
7250fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // The order of the operands was reversed.  Swap them.
7260fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        DEBUG(dbgs() << "RA: " << *Op << '\n');
7270fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Op->swapOperands();
7280fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        DEBUG(dbgs() << "TO: " << *Op << '\n');
7290fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        MadeChange = true;
7300fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        ++NumChanged;
7310fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        break;
7320fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
7330fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
7340fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // The new operation differs non-trivially from the original. Overwrite
7350fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      // the old operands with the new ones.
7360fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      DEBUG(dbgs() << "RA: " << *Op << '\n');
7370fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (NewLHS != OldLHS) {
738b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands        BinaryOperator *BO = isReassociableOp(OldLHS, Opcode);
739b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands        if (BO && !NotRewritable.count(BO))
7400fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          NodesToRewrite.push_back(BO);
7410fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Op->setOperand(0, NewLHS);
7420fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
7430fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (NewRHS != OldRHS) {
744b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands        BinaryOperator *BO = isReassociableOp(OldRHS, Opcode);
745b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands        if (BO && !NotRewritable.count(BO))
7460fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          NodesToRewrite.push_back(BO);
7470fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Op->setOperand(1, NewRHS);
7480fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
7490fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      DEBUG(dbgs() << "TO: " << *Op << '\n');
7500fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
751eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands      ExpressionChanged = Op;
752c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner      MadeChange = true;
753c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner      ++NumChanged;
754e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
7550fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      break;
756c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner    }
757c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner
7580fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Not the last operation.  The left-hand side will be a sub-expression
7590fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // while the right-hand side will be the current element of Ops.
7600fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Value *NewRHS = Ops[i].Op;
7610fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    if (NewRHS != Op->getOperand(1)) {
7620fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      DEBUG(dbgs() << "RA: " << *Op << '\n');
7630fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (NewRHS == Op->getOperand(0)) {
7640fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // The new right-hand side was already present as the left operand.  If
7650fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // we are lucky then swapping the operands will sort out both of them.
7660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Op->swapOperands();
7670fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      } else {
7680fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        // Overwrite with the new right-hand side.
769b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands        BinaryOperator *BO = isReassociableOp(Op->getOperand(1), Opcode);
770b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands        if (BO && !NotRewritable.count(BO))
7710fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands          NodesToRewrite.push_back(BO);
7720fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        Op->setOperand(1, NewRHS);
773eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands        ExpressionChanged = Op;
7740fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      }
7750fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      DEBUG(dbgs() << "TO: " << *Op << '\n');
7760fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      MadeChange = true;
7770fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      ++NumChanged;
7780fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    }
77946985a14409486293b689ca07dd07d7482734795Dan Gohman
7800fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Now deal with the left-hand side.  If this is already an operation node
7810fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // from the original expression then just rewrite the rest of the expression
7820fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // into it.
783b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands    BinaryOperator *BO = isReassociableOp(Op->getOperand(0), Opcode);
784b8e1111fbf71766903d2fc7b158dc612df097ea3Duncan Sands    if (BO && !NotRewritable.count(BO)) {
7852923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands      Op = BO;
7860fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      continue;
7870fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    }
78846985a14409486293b689ca07dd07d7482734795Dan Gohman
7890fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Otherwise, grab a spare node from the original expression and use that as
79096d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    // the left-hand side.  If there are no nodes left then the optimizers made
79196d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    // an expression with more nodes than the original!  This usually means that
79296d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    // they did something stupid but it might mean that the problem was just too
79396d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    // hard (finding the mimimal number of multiplications needed to realize a
79496d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    // multiplication expression is NP-complete).  Whatever the reason, smart or
79596d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    // stupid, create a new node if there are none left.
7962923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands    BinaryOperator *NewOp;
79796d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    if (NodesToRewrite.empty()) {
79896d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands      Constant *Undef = UndefValue::get(I->getType());
7992923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands      NewOp = BinaryOperator::Create(Instruction::BinaryOps(Opcode),
8002923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands                                     Undef, Undef, "", I);
8012923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands    } else {
8022923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands      NewOp = NodesToRewrite.pop_back_val();
80396d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands    }
80496d2eff5c6713a2c5fd2cd61545e49637c332975Duncan Sands
8050fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    DEBUG(dbgs() << "RA: " << *Op << '\n');
8062923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands    Op->setOperand(0, NewOp);
8070fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    DEBUG(dbgs() << "TO: " << *Op << '\n');
808eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands    ExpressionChanged = Op;
809c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner    MadeChange = true;
810c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner    ++NumChanged;
8112923bca2b5fe46189c4c5572047e1d95946ac549Duncan Sands    Op = NewOp;
812c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner  }
813e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
814eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands  // If the expression changed non-trivially then clear out all subclass data
8152d5f8ca3d180832d168e59e2bf3d85317e86287dDuncan Sands  // starting from the operator specified in ExpressionChanged, and compactify
8162d5f8ca3d180832d168e59e2bf3d85317e86287dDuncan Sands  // the operators to just before the expression root to guarantee that the
8172d5f8ca3d180832d168e59e2bf3d85317e86287dDuncan Sands  // expression tree is dominated by all of Ops.
8182d5f8ca3d180832d168e59e2bf3d85317e86287dDuncan Sands  if (ExpressionChanged)
8190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    do {
820eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands      ExpressionChanged->clearSubclassOptionalData();
821eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands      if (ExpressionChanged == I)
8220fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands        break;
8232d5f8ca3d180832d168e59e2bf3d85317e86287dDuncan Sands      ExpressionChanged->moveBefore(I);
824eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands      ExpressionChanged = cast<BinaryOperator>(*ExpressionChanged->use_begin());
8250fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    } while (1);
826e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
8270fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Throw away any left over nodes from the original expression.
8280fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  for (unsigned i = 0, e = NodesToRewrite.size(); i != e; ++i)
829841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    RedoInsts.insert(NodesToRewrite[i]);
8304fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner}
8314fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
832e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// NegateValue - Insert instructions before the instruction pointed to by BI,
833e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// that computes the negative version of the value specified.  The negative
834e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// version of the value is returned, and BI is left pointing at the instruction
835e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// that should be processed next by the reassociation pass.
836e79fddedcae1ee8fe7d8571db58447bc722f75dcNick Lewyckystatic Value *NegateValue(Value *V, Instruction *BI) {
83735239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner  if (Constant *C = dyn_cast<Constant>(V))
83835239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    return ConstantExpr::getNeg(C);
839e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
840a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // We are trying to expose opportunity for reassociation.  One of the things
841a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // that we want to do to achieve this is to push a negation as deep into an
842a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // expression chain as possible, to expose the add instructions.  In practice,
843a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // this means that we turn this:
844a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  //   X = -(A+12+C+D)   into    X = -A + -12 + -C + -D = -12 + -A + -C + -D
845a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // so that later, a: Y = 12+X could get reassociated with the -12 to eliminate
846a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // the constants.  We assume that instcombine will clean up the mess later if
8479046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner  // we introduce tons of unnecessary negation instructions.
848a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  //
8490fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  if (BinaryOperator *I = isReassociableOp(V, Instruction::Add)) {
8500fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Push the negates through the add.
8510fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    I->setOperand(0, NegateValue(I->getOperand(0), BI));
8520fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    I->setOperand(1, NegateValue(I->getOperand(1), BI));
8530fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
8540fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // We must move the add instruction here, because the neg instructions do
8550fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // not dominate the old add instruction in general.  By moving it, we are
8560fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // assured that the neg instructions we just inserted dominate the
8570fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // instruction we are about to insert after them.
8580fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    //
8590fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    I->moveBefore(BI);
8600fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    I->setName(I->getName()+".neg");
8610fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    return I;
8620fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  }
863e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
86435239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner  // Okay, we need to materialize a negated version of V with an instruction.
86535239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner  // Scan the use lists of V to see if we have one already.
86635239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
867110b75aa7572d3b59b308da7ec1d759e86788f97Gabor Greif    User *U = *UI;
868110b75aa7572d3b59b308da7ec1d759e86788f97Gabor Greif    if (!BinaryOperator::isNeg(U)) continue;
86935239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner
87035239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    // We found one!  Now we have to make sure that the definition dominates
87135239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    // this use.  We do this by moving it to the entry block (if it is a
87235239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    // non-instruction value) or right after the definition.  These negates will
87335239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    // be zapped by reassociate later, so we don't need much finesse here.
874110b75aa7572d3b59b308da7ec1d759e86788f97Gabor Greif    BinaryOperator *TheNeg = cast<BinaryOperator>(U);
8751c91fae649734abe6f8271862fe3ba917e191279Chris Lattner
8761c91fae649734abe6f8271862fe3ba917e191279Chris Lattner    // Verify that the negate is in this function, V might be a constant expr.
8771c91fae649734abe6f8271862fe3ba917e191279Chris Lattner    if (TheNeg->getParent()->getParent() != BI->getParent()->getParent())
8781c91fae649734abe6f8271862fe3ba917e191279Chris Lattner      continue;
879e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
88035239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    BasicBlock::iterator InsertPt;
88135239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    if (Instruction *InstInput = dyn_cast<Instruction>(V)) {
88235239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner      if (InvokeInst *II = dyn_cast<InvokeInst>(InstInput)) {
88335239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner        InsertPt = II->getNormalDest()->begin();
88435239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner      } else {
88535239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner        InsertPt = InstInput;
88635239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner        ++InsertPt;
88735239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner      }
88835239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner      while (isa<PHINode>(InsertPt)) ++InsertPt;
88935239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    } else {
89035239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner      InsertPt = TheNeg->getParent()->getParent()->getEntryBlock().begin();
89135239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    }
89235239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    TheNeg->moveBefore(InsertPt);
89335239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner    return TheNeg;
89435239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner  }
895a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner
896a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // Insert a 'neg' instruction that subtracts the value from zero to get the
897a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner  // negation.
8984ae5126d041768ab9665cf2f11c024becd76c41fDan Gohman  return BinaryOperator::CreateNeg(V, V->getName() + ".neg", BI);
899a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner}
900a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner
9019bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner/// ShouldBreakUpSubtract - Return true if we should break up this subtract of
9029bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner/// X-Y into (X + -Y).
903e79fddedcae1ee8fe7d8571db58447bc722f75dcNick Lewyckystatic bool ShouldBreakUpSubtract(Instruction *Sub) {
9049bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner  // If this is a negation, we can't split it up!
905fa82b6eba4e1584d7dba291c28fe908272e1e002Owen Anderson  if (BinaryOperator::isNeg(Sub))
9069bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner    return false;
907e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
9089bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner  // Don't bother to break this up unless either the LHS is an associable add or
9090b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner  // subtract or if this is only used by one.
9100b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner  if (isReassociableOp(Sub->getOperand(0), Instruction::Add) ||
9110b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner      isReassociableOp(Sub->getOperand(0), Instruction::Sub))
9129bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner    return true;
9130b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner  if (isReassociableOp(Sub->getOperand(1), Instruction::Add) ||
9145329bb22e9b6374d62919981c1ef8775b42945ebChris Lattner      isReassociableOp(Sub->getOperand(1), Instruction::Sub))
9159bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner    return true;
916e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling  if (Sub->hasOneUse() &&
9170b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner      (isReassociableOp(Sub->use_back(), Instruction::Add) ||
9180b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner       isReassociableOp(Sub->use_back(), Instruction::Sub)))
9199bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner    return true;
920e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
9219bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner  return false;
9229bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner}
9239bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner
92408b43921e18f314c4fd38049291d323830934c36Chris Lattner/// BreakUpSubtract - If we have (X-Y), and if either X is an add, or if this is
92508b43921e18f314c4fd38049291d323830934c36Chris Lattner/// only used by an add, transform this into (X+(0-Y)) to promote better
92608b43921e18f314c4fd38049291d323830934c36Chris Lattner/// reassociation.
927841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsstatic BinaryOperator *BreakUpSubtract(Instruction *Sub) {
9289046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner  // Convert a subtract into an add and a neg instruction. This allows sub
9299046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner  // instructions to be commuted with other add instructions.
93008b43921e18f314c4fd38049291d323830934c36Chris Lattner  //
9319046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner  // Calculate the negative value of Operand 1 of the sub instruction,
9329046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner  // and set it as the RHS of the add instruction we just made.
93308b43921e18f314c4fd38049291d323830934c36Chris Lattner  //
934e79fddedcae1ee8fe7d8571db58447bc722f75dcNick Lewycky  Value *NegVal = NegateValue(Sub->getOperand(1), Sub);
935841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  BinaryOperator *New =
9367cbd8a3e92221437048b484d5ef9c0a22d0f8c58Gabor Greif    BinaryOperator::CreateAdd(Sub->getOperand(0), NegVal, "", Sub);
937841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Sub->setOperand(0, Constant::getNullValue(Sub->getType())); // Drop use of op.
938841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Sub->setOperand(1, Constant::getNullValue(Sub->getType())); // Drop use of op.
9396934a04a8c15e9971cd1ea4d5c8df2d7afdd5be5Chris Lattner  New->takeName(Sub);
94008b43921e18f314c4fd38049291d323830934c36Chris Lattner
94108b43921e18f314c4fd38049291d323830934c36Chris Lattner  // Everyone now refers to the add instruction.
94208b43921e18f314c4fd38049291d323830934c36Chris Lattner  Sub->replaceAllUsesWith(New);
9435367b23f76e75ebb680956575346fa8c3d56780fDevang Patel  New->setDebugLoc(Sub->getDebugLoc());
94400b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen
945a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene  DEBUG(dbgs() << "Negated: " << *New << '\n');
94608b43921e18f314c4fd38049291d323830934c36Chris Lattner  return New;
94708b43921e18f314c4fd38049291d323830934c36Chris Lattner}
94808b43921e18f314c4fd38049291d323830934c36Chris Lattner
9490975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner/// ConvertShiftToMul - If this is a shift of a reassociable multiply or is used
9500975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner/// by one, change this into a multiply by a constant to assist with further
9510975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner/// reassociation.
952841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsstatic BinaryOperator *ConvertShiftToMul(Instruction *Shl) {
953841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Constant *MulCst = ConstantInt::get(Shl->getType(), 1);
954841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  MulCst = ConstantExpr::getShl(MulCst, cast<Constant>(Shl->getOperand(1)));
955841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands
956841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  BinaryOperator *Mul =
957841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    BinaryOperator::CreateMul(Shl->getOperand(0), MulCst, "", Shl);
958841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Shl->setOperand(0, UndefValue::get(Shl->getType())); // Drop use of op.
959841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Mul->takeName(Shl);
960841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Shl->replaceAllUsesWith(Mul);
961841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  Mul->setDebugLoc(Shl->getDebugLoc());
962841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  return Mul;
9630975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner}
9640975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner
965e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// FindInOperandList - Scan backwards and forwards among values with the same
966e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// rank as element i to see if X exists.  If X does not exist, return i.  This
967e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// is useful when scanning for 'x' when we see '-x' because they both get the
968e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// same rank.
9699f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattnerstatic unsigned FindInOperandList(SmallVectorImpl<ValueEntry> &Ops, unsigned i,
970109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner                                  Value *X) {
971109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  unsigned XRank = Ops[i].Rank;
972109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  unsigned e = Ops.size();
973109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  for (unsigned j = i+1; j != e && Ops[j].Rank == XRank; ++j)
974109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner    if (Ops[j].Op == X)
975109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner      return j;
9769506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  // Scan backwards.
977109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  for (unsigned j = i-1; j != ~0U && Ops[j].Rank == XRank; --j)
978109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner    if (Ops[j].Op == X)
979109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner      return j;
980109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  return i;
981109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner}
982109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner
983e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// EmitAddTreeOfValues - Emit a tree of add instructions, summing Ops together
984e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// and returning the result.  Insert the tree before I.
98555e7098bbc363473c01229517097d2a04e04e9b0Bill Wendlingstatic Value *EmitAddTreeOfValues(Instruction *I,
98655e7098bbc363473c01229517097d2a04e04e9b0Bill Wendling                                  SmallVectorImpl<WeakVH> &Ops){
987e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  if (Ops.size() == 1) return Ops.back();
988e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
989e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  Value *V1 = Ops.back();
990e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  Ops.pop_back();
991e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  Value *V2 = EmitAddTreeOfValues(I, Ops);
9927cbd8a3e92221437048b484d5ef9c0a22d0f8c58Gabor Greif  return BinaryOperator::CreateAdd(V2, V1, "tmp", I);
993e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner}
994e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner
995e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// RemoveFactorFromExpression - If V is an expression tree that is a
996e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// multiplication sequence, and if this sequence contains a multiply by Factor,
997e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// remove Factor from the tree and return the new tree.
998e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris LattnerValue *Reassociate::RemoveFactorFromExpression(Value *V, Value *Factor) {
999e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  BinaryOperator *BO = isReassociableOp(V, Instruction::Mul);
1000e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  if (!BO) return 0;
1001e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1002c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  SmallVector<RepeatedValue, 8> Tree;
1003c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  MadeChange |= LinearizeExprTree(BO, Tree);
10049f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner  SmallVector<ValueEntry, 8> Factors;
1005c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  Factors.reserve(Tree.size());
1006c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  for (unsigned i = 0, e = Tree.size(); i != e; ++i) {
1007c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    RepeatedValue E = Tree[i];
1008c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    Factors.append(E.second.getZExtValue(),
1009c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands                   ValueEntry(getRank(E.first), E.first));
1010c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
1011e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner
1012e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  bool FoundFactor = false;
10139506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  bool NeedsNegate = false;
10149506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  for (unsigned i = 0, e = Factors.size(); i != e; ++i) {
1015e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner    if (Factors[i].Op == Factor) {
1016e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner      FoundFactor = true;
1017e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner      Factors.erase(Factors.begin()+i);
1018e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner      break;
1019e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner    }
1020e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
10219506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    // If this is a negative version of this factor, remove it.
10229506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    if (ConstantInt *FC1 = dyn_cast<ConstantInt>(Factor))
10239506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      if (ConstantInt *FC2 = dyn_cast<ConstantInt>(Factors[i].Op))
10249506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner        if (FC1->getValue() == -FC2->getValue()) {
10259506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          FoundFactor = NeedsNegate = true;
10269506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          Factors.erase(Factors.begin()+i);
10279506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          break;
10289506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner        }
10299506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  }
1030e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1031e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner  if (!FoundFactor) {
1032e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner    // Make sure to restore the operands to the expression tree.
1033e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner    RewriteExprTree(BO, Factors);
1034e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner    return 0;
1035e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner  }
1036e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
10379506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  BasicBlock::iterator InsertPt = BO; ++InsertPt;
1038e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
10391e7558b65689999089f53ce40ff07564cf498c68Chris Lattner  // If this was just a single multiply, remove the multiply and return the only
10401e7558b65689999089f53ce40ff07564cf498c68Chris Lattner  // remaining operand.
10411e7558b65689999089f53ce40ff07564cf498c68Chris Lattner  if (Factors.size() == 1) {
1042841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    RedoInsts.insert(BO);
10439506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    V = Factors[0].Op;
10449506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  } else {
10459506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    RewriteExprTree(BO, Factors);
10469506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    V = BO;
10471e7558b65689999089f53ce40ff07564cf498c68Chris Lattner  }
1048e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
10499506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  if (NeedsNegate)
10509506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    V = BinaryOperator::CreateNeg(V, "neg", InsertPt);
1051e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
10529506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  return V;
1053e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner}
1054e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner
1055e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner/// FindSingleUseMultiplyFactors - If V is a single-use multiply, recursively
1056e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner/// add its operands as factors, otherwise add V to the list of factors.
1057893075f46e9d07e3fe94e2b0e0f3ff8ae4061549Chris Lattner///
1058893075f46e9d07e3fe94e2b0e0f3ff8ae4061549Chris Lattner/// Ops is the top-level list of add operands we're trying to factor.
1059e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattnerstatic void FindSingleUseMultiplyFactors(Value *V,
1060893075f46e9d07e3fe94e2b0e0f3ff8ae4061549Chris Lattner                                         SmallVectorImpl<Value*> &Factors,
10610fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands                                       const SmallVectorImpl<ValueEntry> &Ops) {
10620fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  BinaryOperator *BO = isReassociableOp(V, Instruction::Mul);
10630fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  if (!BO) {
1064e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner    Factors.push_back(V);
1065e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner    return;
1066e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner  }
1067e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1068e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner  // Otherwise, add the LHS and RHS to the list of factors.
10690fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  FindSingleUseMultiplyFactors(BO->getOperand(1), Factors, Ops);
10700fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  FindSingleUseMultiplyFactors(BO->getOperand(0), Factors, Ops);
1071e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner}
1072e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner
1073f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// OptimizeAndOrXor - Optimize a series of operands to an 'and', 'or', or 'xor'
1074f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// instruction.  This optimizes based on identities.  If it can be reduced to
1075f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// a single Value, it is returned, otherwise the Ops list is mutated as
1076f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// necessary.
10779f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattnerstatic Value *OptimizeAndOrXor(unsigned Opcode,
10789f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner                               SmallVectorImpl<ValueEntry> &Ops) {
1079f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  // Scan the operand lists looking for X and ~X pairs, along with X,X pairs.
1080f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  // If we find any, we can simplify the expression. X&~X == 0, X|~X == -1.
1081f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1082f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    // First, check for X and ~X in the operand list.
1083f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    assert(i < Ops.size());
1084f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    if (BinaryOperator::isNot(Ops[i].Op)) {    // Cannot occur for ^.
1085f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      Value *X = BinaryOperator::getNotArgument(Ops[i].Op);
1086f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      unsigned FoundX = FindInOperandList(Ops, i, X);
1087f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      if (FoundX != i) {
10889fdaefad580194353f34b6d72669591f8f9d811aChris Lattner        if (Opcode == Instruction::And)   // ...&X&~X = 0
1089f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner          return Constant::getNullValue(X->getType());
1090e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
10919fdaefad580194353f34b6d72669591f8f9d811aChris Lattner        if (Opcode == Instruction::Or)    // ...|X|~X = -1
1092f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner          return Constant::getAllOnesValue(X->getType());
1093f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      }
1094f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    }
1095e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1096f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    // Next, check for duplicate pairs of values, which we assume are next to
1097f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    // each other, due to our sorting criteria.
1098f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    assert(i < Ops.size());
1099f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    if (i+1 != Ops.size() && Ops[i+1].Op == Ops[i].Op) {
1100f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      if (Opcode == Instruction::And || Opcode == Instruction::Or) {
1101f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner        // Drop duplicate values for And and Or.
1102f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner        Ops.erase(Ops.begin()+i);
1103f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner        --i; --e;
1104f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner        ++NumAnnihil;
1105f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner        continue;
1106f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      }
1107e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1108f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      // Drop pairs of values for Xor.
1109f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      assert(Opcode == Instruction::Xor);
1110f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      if (e == 2)
1111f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner        return Constant::getNullValue(Ops[0].Op->getType());
1112e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
11139046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner      // Y ^ X^X -> Y
1114f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      Ops.erase(Ops.begin()+i, Ops.begin()+i+2);
1115f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      i -= 1; e -= 2;
1116f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      ++NumAnnihil;
1117f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    }
1118f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  }
1119f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  return 0;
1120f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner}
1121e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner
11222d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang/// Helper funciton of CombineXorOpnd(). It creates a bitwise-and
11232d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang/// instruction with the given two operands, and return the resulting
11242d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang/// instruction. There are two special cases: 1) if the constant operand is 0,
11252d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang/// it will return NULL. 2) if the constant is ~0, the symbolic operand will
11262d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang/// be returned.
11272d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yangstatic Value *createAndInstr(Instruction *InsertBefore, Value *Opnd,
11282d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang                             const APInt &ConstOpnd) {
11292d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  if (ConstOpnd != 0) {
11302d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (!ConstOpnd.isAllOnesValue()) {
11312d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      LLVMContext &Ctx = Opnd->getType()->getContext();
11322d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      Instruction *I;
11332d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      I = BinaryOperator::CreateAnd(Opnd, ConstantInt::get(Ctx, ConstOpnd),
11342d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang                                    "and.ra", InsertBefore);
11352d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      I->setDebugLoc(InsertBefore->getDebugLoc());
11362d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      return I;
11372d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    }
11382d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    return Opnd;
11392d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  }
11402d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  return 0;
11412d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang}
11422d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
11432d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang// Helper function of OptimizeXor(). It tries to simplify "Opnd1 ^ ConstOpnd"
11442d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang// into "R ^ C", where C would be 0, and R is a symbolic value.
11452d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang//
11462d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang// If it was successful, true is returned, and the "R" and "C" is returned
11472d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang// via "Res" and "ConstOpnd", respectively; otherwise, false is returned,
11482d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang// and both "Res" and "ConstOpnd" remain unchanged.
11492d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang//
11502d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yangbool Reassociate::CombineXorOpnd(Instruction *I, XorOpnd *Opnd1,
11512d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang                                 APInt &ConstOpnd, Value *&Res) {
11522d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  // Xor-Rule 1: (x | c1) ^ c2 = (x | c1) ^ (c1 ^ c1) ^ c2
11532d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  //                       = ((x | c1) ^ c1) ^ (c1 ^ c2)
11542d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  //                       = (x & ~c1) ^ (c1 ^ c2)
11552d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  // It is useful only when c1 == c2.
11562d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  if (Opnd1->isOrExpr() && Opnd1->getConstPart() != 0) {
11572d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (!Opnd1->getValue()->hasOneUse())
11582d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      return false;
11592d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
11602d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    const APInt &C1 = Opnd1->getConstPart();
11612d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (C1 != ConstOpnd)
11622d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      return false;
11632d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
11642d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Value *X = Opnd1->getSymbolicPart();
11652d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Res = createAndInstr(I, X, ~C1);
11662d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    // ConstOpnd was C2, now C1 ^ C2.
11672d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    ConstOpnd ^= C1;
11682d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
11692d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (Instruction *T = dyn_cast<Instruction>(Opnd1->getValue()))
11702d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      RedoInsts.insert(T);
11712d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    return true;
11722d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  }
11732d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  return false;
11742d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang}
11752d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
11762d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
11772d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang// Helper function of OptimizeXor(). It tries to simplify
11782d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang// "Opnd1 ^ Opnd2 ^ ConstOpnd" into "R ^ C", where C would be 0, and R is a
11792d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang// symbolic value.
11802d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang//
11812d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang// If it was successful, true is returned, and the "R" and "C" is returned
11822d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang// via "Res" and "ConstOpnd", respectively (If the entire expression is
11832d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang// evaluated to a constant, the Res is set to NULL); otherwise, false is
11842d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang// returned, and both "Res" and "ConstOpnd" remain unchanged.
11852d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yangbool Reassociate::CombineXorOpnd(Instruction *I, XorOpnd *Opnd1, XorOpnd *Opnd2,
11862d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang                                 APInt &ConstOpnd, Value *&Res) {
11872d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  Value *X = Opnd1->getSymbolicPart();
11882d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  if (X != Opnd2->getSymbolicPart())
11892d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    return false;
11902d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
11912d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  // This many instruction become dead.(At least "Opnd1 ^ Opnd2" will die.)
11922d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  int DeadInstNum = 1;
11932d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  if (Opnd1->getValue()->hasOneUse())
11942d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    DeadInstNum++;
11952d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  if (Opnd2->getValue()->hasOneUse())
11962d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    DeadInstNum++;
11972d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
11982d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  // Xor-Rule 2:
11992d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  //  (x | c1) ^ (x & c2)
12002d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  //   = (x|c1) ^ (x&c2) ^ (c1 ^ c1) = ((x|c1) ^ c1) ^ (x & c2) ^ c1
12012d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  //   = (x & ~c1) ^ (x & c2) ^ c1               // Xor-Rule 1
12022d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  //   = (x & c3) ^ c1, where c3 = ~c1 ^ c2      // Xor-rule 3
12032d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  //
12042d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  if (Opnd1->isOrExpr() != Opnd2->isOrExpr()) {
12052d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (Opnd2->isOrExpr())
12062d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      std::swap(Opnd1, Opnd2);
12072d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12084d4c54d29ff911f59fe2be1a31331dbcbb741f5fShuxin Yang    const APInt &C1 = Opnd1->getConstPart();
12094d4c54d29ff911f59fe2be1a31331dbcbb741f5fShuxin Yang    const APInt &C2 = Opnd2->getConstPart();
12102d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    APInt C3((~C1) ^ C2);
12112d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12122d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    // Do not increase code size!
12132d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (C3 != 0 && !C3.isAllOnesValue()) {
12142d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      int NewInstNum = ConstOpnd != 0 ? 1 : 2;
12152d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      if (NewInstNum > DeadInstNum)
12162d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang        return false;
12172d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    }
12182d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12192d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Res = createAndInstr(I, X, C3);
12202d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    ConstOpnd ^= C1;
12212d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12222d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  } else if (Opnd1->isOrExpr()) {
12232d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    // Xor-Rule 3: (x | c1) ^ (x | c2) = (x & c3) ^ c3 where c3 = c1 ^ c2
12242d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    //
12254d4c54d29ff911f59fe2be1a31331dbcbb741f5fShuxin Yang    const APInt &C1 = Opnd1->getConstPart();
12264d4c54d29ff911f59fe2be1a31331dbcbb741f5fShuxin Yang    const APInt &C2 = Opnd2->getConstPart();
12272d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    APInt C3 = C1 ^ C2;
12282d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12292d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    // Do not increase code size
12302d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (C3 != 0 && !C3.isAllOnesValue()) {
12312d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      int NewInstNum = ConstOpnd != 0 ? 1 : 2;
12322d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      if (NewInstNum > DeadInstNum)
12332d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang        return false;
12342d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    }
12352d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12362d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Res = createAndInstr(I, X, C3);
12372d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    ConstOpnd ^= C3;
12382d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  } else {
12392d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    // Xor-Rule 4: (x & c1) ^ (x & c2) = (x & (c1^c2))
12402d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    //
12414d4c54d29ff911f59fe2be1a31331dbcbb741f5fShuxin Yang    const APInt &C1 = Opnd1->getConstPart();
12424d4c54d29ff911f59fe2be1a31331dbcbb741f5fShuxin Yang    const APInt &C2 = Opnd2->getConstPart();
12432d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    APInt C3 = C1 ^ C2;
12442d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Res = createAndInstr(I, X, C3);
12452d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  }
12462d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12472d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  // Put the original operands in the Redo list; hope they will be deleted
12482d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  // as dead code.
12492d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  if (Instruction *T = dyn_cast<Instruction>(Opnd1->getValue()))
12502d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    RedoInsts.insert(T);
12512d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  if (Instruction *T = dyn_cast<Instruction>(Opnd2->getValue()))
12522d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    RedoInsts.insert(T);
12532d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12542d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  return true;
12552d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang}
12562d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12572d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang/// Optimize a series of operands to an 'xor' instruction. If it can be reduced
12582d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang/// to a single Value, it is returned, otherwise the Ops list is mutated as
12592d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang/// necessary.
12602d1001064989b7fa79507816fc17d467fc00a2f2Shuxin YangValue *Reassociate::OptimizeXor(Instruction *I,
12612d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang                                SmallVectorImpl<ValueEntry> &Ops) {
12622d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  if (Value *V = OptimizeAndOrXor(Instruction::Xor, Ops))
12632d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    return V;
12642d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12652d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  if (Ops.size() == 1)
12662d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    return 0;
12672d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12682d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  SmallVector<XorOpnd, 8> Opnds;
12694fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang  SmallVector<XorOpnd*, 8> OpndPtrs;
12702d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  Type *Ty = Ops[0].Op->getType();
12712d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  APInt ConstOpnd(Ty->getIntegerBitWidth(), 0);
12722d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12732d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  // Step 1: Convert ValueEntry to XorOpnd
12742d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
12752d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Value *V = Ops[i].Op;
12762d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (!isa<ConstantInt>(V)) {
12772d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      XorOpnd O(V);
12782d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      O.setSymbolicRank(getRank(O.getSymbolicPart()));
12792d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      Opnds.push_back(O);
12802d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    } else
12812d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      ConstOpnd ^= cast<ConstantInt>(V)->getValue();
12822d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  }
12832d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12844fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang  // NOTE: From this point on, do *NOT* add/delete element to/from "Opnds".
12854fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang  //  It would otherwise invalidate the "Opnds"'s iterator, and hence invalidate
12864fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang  //  the "OpndPtrs" as well. For the similar reason, do not fuse this loop
12874fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang  //  with the previous loop --- the iterator of the "Opnds" may be invalidated
12884fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang  //  when new elements are added to the vector.
12894fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang  for (unsigned i = 0, e = Opnds.size(); i != e; ++i)
12904fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang    OpndPtrs.push_back(&Opnds[i]);
12914fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang
12922d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  // Step 2: Sort the Xor-Operands in a way such that the operands containing
12932d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  //  the same symbolic value cluster together. For instance, the input operand
12942d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  //  sequence ("x | 123", "y & 456", "x & 789") will be sorted into:
12952d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  //  ("x | 123", "x & 789", "y & 456").
12964fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang  std::sort(OpndPtrs.begin(), OpndPtrs.end(), XorOpnd::PtrSortFunctor());
12972d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
12982d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  // Step 3: Combine adjacent operands
12992d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  XorOpnd *PrevOpnd = 0;
13002d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  bool Changed = false;
13012d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  for (unsigned i = 0, e = Opnds.size(); i < e; i++) {
13024fd00c55d0e31770505e6517afdfadfa3482d889Shuxin Yang    XorOpnd *CurrOpnd = OpndPtrs[i];
13032d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    // The combined value
13042d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Value *CV;
13052d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
13062d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    // Step 3.1: Try simplifying "CurrOpnd ^ ConstOpnd"
13072d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (ConstOpnd != 0 && CombineXorOpnd(I, CurrOpnd, ConstOpnd, CV)) {
13082d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      Changed = true;
13092d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      if (CV)
13102d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang        *CurrOpnd = XorOpnd(CV);
13112d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      else {
13122d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang        CurrOpnd->Invalidate();
13132d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang        continue;
13142d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      }
13152d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    }
13162d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
13172d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (!PrevOpnd || CurrOpnd->getSymbolicPart() != PrevOpnd->getSymbolicPart()) {
13182d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      PrevOpnd = CurrOpnd;
13192d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      continue;
13202d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    }
13212d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
13222d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    // step 3.2: When previous and current operands share the same symbolic
13232d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    //  value, try to simplify "PrevOpnd ^ CurrOpnd ^ ConstOpnd"
13242d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    //
13252d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (CombineXorOpnd(I, CurrOpnd, PrevOpnd, ConstOpnd, CV)) {
13262d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      // Remove previous operand
13272d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      PrevOpnd->Invalidate();
13282d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      if (CV) {
13292d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang        *CurrOpnd = XorOpnd(CV);
13302d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang        PrevOpnd = CurrOpnd;
13312d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      } else {
13322d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang        CurrOpnd->Invalidate();
13332d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang        PrevOpnd = 0;
13342d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      }
13352d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      Changed = true;
13362d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    }
13372d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  }
13382d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
13392d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  // Step 4: Reassemble the Ops
13402d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  if (Changed) {
13412d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    Ops.clear();
13422d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    for (unsigned int i = 0, e = Opnds.size(); i < e; i++) {
13432d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      XorOpnd &O = Opnds[i];
13442d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      if (O.isInvalid())
13452d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang        continue;
13462d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      ValueEntry VE(getRank(O.getValue()), O.getValue());
13472d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      Ops.push_back(VE);
13482d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    }
13492d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (ConstOpnd != 0) {
13502d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      Value *C = ConstantInt::get(Ty->getContext(), ConstOpnd);
13512d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      ValueEntry VE(getRank(C), C);
13522d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      Ops.push_back(VE);
13532d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    }
13542d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    int Sz = Ops.size();
13552d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (Sz == 1)
13562d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      return Ops.back().Op;
13572d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    else if (Sz == 0) {
13582d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      assert(ConstOpnd == 0);
13592d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      return ConstantInt::get(Ty->getContext(), ConstOpnd);
13602d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    }
13612d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  }
13622d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
13632d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  return 0;
13642d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang}
13652d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
1366f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// OptimizeAdd - Optimize a series of operands to an 'add' instruction.  This
1367f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// optimizes based on identities.  If it can be reduced to a single Value, it
1368f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// is returned, otherwise the Ops list is mutated as necessary.
13699f7b7089be854c323f8d9a4627d80e47adf496e6Chris LattnerValue *Reassociate::OptimizeAdd(Instruction *I,
13709f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner                                SmallVectorImpl<ValueEntry> &Ops) {
1371f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  // Scan the operand lists looking for X and -X pairs.  If we find any, we
137269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  // can simplify the expression. X+-X == 0.  While we're at it, scan for any
137369e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  // duplicates.  We want to canonicalize Y+Y+Y+Z -> 3*Y+Z.
13749506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  //
13759506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  // TODO: We could handle "X + ~X" -> "-1" if we wanted, since "-X = ~X+1".
13769506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner  //
1377f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
137869e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    Value *TheOp = Ops[i].Op;
137969e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    // Check to see if we've seen this operand before.  If so, we factor all
1380f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner    // instances of the operand together.  Due to our sorting criteria, we know
1381f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner    // that these need to be next to each other in the vector.
1382f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner    if (i+1 != Ops.size() && Ops[i+1].Op == TheOp) {
1383f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      // Rescan the list, remove all instances of this operand from the expr.
138469e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      unsigned NumFound = 0;
1385f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      do {
1386f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner        Ops.erase(Ops.begin()+i);
138769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner        ++NumFound;
1388f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      } while (i != Ops.size() && Ops[i].Op == TheOp);
1389e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1390f8a447de162a2896a8a044931fb63de713dbc6b9Chris Lattner      DEBUG(errs() << "\nFACTORING [" << NumFound << "]: " << *TheOp << '\n');
139169e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      ++NumFactor;
1392e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
139369e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // Insert a new multiply.
139469e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      Value *Mul = ConstantInt::get(cast<IntegerType>(I->getType()), NumFound);
139569e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      Mul = BinaryOperator::CreateMul(TheOp, Mul, "factor", I);
1396e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
139769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // Now that we have inserted a multiply, optimize it. This allows us to
139869e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // handle cases that require multiple factoring steps, such as this:
139969e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // (X*2) + (X*2) + (X*2) -> (X*2)*3 -> X*6
1400841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      RedoInsts.insert(cast<Instruction>(Mul));
1401e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
140269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // If every add operand was a duplicate, return the multiply.
140369e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      if (Ops.empty())
140469e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner        return Mul;
1405e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
140669e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // Otherwise, we had some input that didn't have the dupe, such as
140769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // "A + A + B" -> "A*2 + B".  Add the new multiply to the list of
140869e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      // things being added by this operation.
140969e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner      Ops.insert(Ops.begin(), ValueEntry(getRank(Mul), Mul));
1410e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1411f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      --i;
1412f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      e = Ops.size();
1413f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner      continue;
141469e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    }
1415e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1416f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    // Check for X and -X in the operand list.
141769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    if (!BinaryOperator::isNeg(TheOp))
1418f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      continue;
1419e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
142069e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    Value *X = BinaryOperator::getNegArgument(TheOp);
1421f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    unsigned FoundX = FindInOperandList(Ops, i, X);
1422f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    if (FoundX == i)
1423f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      continue;
1424e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1425f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    // Remove X and -X from the operand list.
14269fdaefad580194353f34b6d72669591f8f9d811aChris Lattner    if (Ops.size() == 2)
1427f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      return Constant::getNullValue(X->getType());
1428e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1429f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    Ops.erase(Ops.begin()+i);
1430f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    if (i < FoundX)
1431f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      --FoundX;
1432f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    else
1433f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      --i;   // Need to back up an extra one.
1434f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    Ops.erase(Ops.begin()+FoundX);
1435f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    ++NumAnnihil;
1436f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    --i;     // Revisit element.
1437f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    e -= 2;  // Removed two elements.
1438f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  }
1439e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
144094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // Scan the operand list, checking to see if there are any common factors
144194285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // between operands.  Consider something like A*A+A*B*C+D.  We would like to
144294285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // reassociate this to A*(A+B*C)+D, which reduces the number of multiplies.
144394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // To efficiently find this, we count the number of times a factor occurs
144494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // for any ADD operands that are MULs.
144594285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  DenseMap<Value*, unsigned> FactorOccurrences;
1446e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
144794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // Keep track of each multiply we see, to avoid triggering on (X*4)+(X*4)
144894285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // where they are actually the same multiply.
144994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  unsigned MaxOcc = 0;
145094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  Value *MaxOccVal = 0;
145194285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
14520fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    BinaryOperator *BOp = isReassociableOp(Ops[i].Op, Instruction::Mul);
14530fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    if (!BOp)
145494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner      continue;
1455e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
145694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // Compute all of the factors of this added value.
145794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    SmallVector<Value*, 8> Factors;
14580fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    FindSingleUseMultiplyFactors(BOp, Factors, Ops);
145994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    assert(Factors.size() > 1 && "Bad linearize!");
1460e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
146194285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // Add one to FactorOccurrences for each unique factor in this op.
14629506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    SmallPtrSet<Value*, 8> Duplicates;
14639506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner    for (unsigned i = 0, e = Factors.size(); i != e; ++i) {
14649506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      Value *Factor = Factors[i];
14659506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      if (!Duplicates.insert(Factor)) continue;
1466e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
14679506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      unsigned Occ = ++FactorOccurrences[Factor];
14689506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factor; }
1469e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
14709506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      // If Factor is a negative constant, add the negated value as a factor
14719506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      // because we can percolate the negate out.  Watch for minint, which
14729506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      // cannot be positivified.
14739506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner      if (ConstantInt *CI = dyn_cast<ConstantInt>(Factor))
1474c73b24db5f6226ed44ebc44ce1c25bb357206623Chris Lattner        if (CI->isNegative() && !CI->isMinValue(true)) {
14759506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          Factor = ConstantInt::get(CI->getContext(), -CI->getValue());
14769506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          assert(!Duplicates.count(Factor) &&
14779506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner                 "Shouldn't have two constant factors, missed a canonicalize");
1478e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
14799506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          unsigned Occ = ++FactorOccurrences[Factor];
14809506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner          if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factor; }
14819506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner        }
148294285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    }
148394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  }
1484e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
148594285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  // If any factor occurred more than one time, we can pull it out.
148694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  if (MaxOcc > 1) {
148769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    DEBUG(errs() << "\nFACTORING [" << MaxOcc << "]: " << *MaxOccVal << '\n');
148894285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    ++NumFactor;
148994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner
149094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // Create a new instruction that uses the MaxOccVal twice.  If we don't do
149194285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // this, we could otherwise run into situations where removing a factor
1492e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling    // from an expression will drop a use of maxocc, and this can cause
149394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // RemoveFactorFromExpression on successive values to behave differently.
149494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    Instruction *DummyInst = BinaryOperator::CreateAdd(MaxOccVal, MaxOccVal);
149555e7098bbc363473c01229517097d2a04e04e9b0Bill Wendling    SmallVector<WeakVH, 4> NewMulOps;
149637f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands    for (unsigned i = 0; i != Ops.size(); ++i) {
1497c2d1b6949c5141d21827cc94daea6ae4b1a9c750Chris Lattner      // Only try to remove factors from expressions we're allowed to.
14980fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      BinaryOperator *BOp = isReassociableOp(Ops[i].Op, Instruction::Mul);
14990fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      if (!BOp)
1500c2d1b6949c5141d21827cc94daea6ae4b1a9c750Chris Lattner        continue;
1501e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
150294285e620b845e09b18939e8d6448e01e692f3ceChris Lattner      if (Value *V = RemoveFactorFromExpression(Ops[i].Op, MaxOccVal)) {
150337f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands        // The factorized operand may occur several times.  Convert them all in
150437f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands        // one fell swoop.
150537f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands        for (unsigned j = Ops.size(); j != i;) {
150637f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands          --j;
150737f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands          if (Ops[j].Op == Ops[i].Op) {
150837f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands            NewMulOps.push_back(V);
150937f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands            Ops.erase(Ops.begin()+j);
151037f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands          }
151137f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands        }
151237f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands        --i;
151394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner      }
151494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    }
1515e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
151694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // No need for extra uses anymore.
151794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    delete DummyInst;
151854a57045ebcf8e31b1542098d1cd2bda9a718725Duncan Sands
151994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    unsigned NumAddedValues = NewMulOps.size();
152094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    Value *V = EmitAddTreeOfValues(I, NewMulOps);
152154a57045ebcf8e31b1542098d1cd2bda9a718725Duncan Sands
152269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    // Now that we have inserted the add tree, optimize it. This allows us to
152369e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    // handle cases that require multiple factoring steps, such as this:
152494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // A*A*B + A*A*C   -->   A*(A*B+A*C)   -->   A*(A*(B+C))
15259cd1bc4f8b3e98892a2b9856eccd2a2ec9afdf7fChris Lattner    assert(NumAddedValues > 1 && "Each occurrence should contribute a value");
152654a57045ebcf8e31b1542098d1cd2bda9a718725Duncan Sands    (void)NumAddedValues;
1527841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    if (Instruction *VI = dyn_cast<Instruction>(V))
1528841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      RedoInsts.insert(VI);
152969e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner
153069e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner    // Create the multiply.
1531841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    Instruction *V2 = BinaryOperator::CreateMul(V, MaxOccVal, "tmp", I);
153269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner
1533f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner    // Rerun associate on the multiply in case the inner expression turned into
1534f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner    // a multiply.  We want to make sure that we keep things in canonical form.
1535841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    RedoInsts.insert(V2);
1536e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
153794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // If every add operand included the factor (e.g. "A*B + A*C"), then the
153894285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // entire result expression is just the multiply "A*(B+C)".
153994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    if (Ops.empty())
154094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner      return V2;
1541e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
15429cd1bc4f8b3e98892a2b9856eccd2a2ec9afdf7fChris Lattner    // Otherwise, we had some input that didn't have the factor, such as
154394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    // "A*B + A*C + D" -> "A*(B+C) + D".  Add the new multiply to the list of
15449cd1bc4f8b3e98892a2b9856eccd2a2ec9afdf7fChris Lattner    // things being added by this operation.
154594285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    Ops.insert(Ops.begin(), ValueEntry(getRank(V2), V2));
154694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner  }
1547e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1548f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner  return 0;
1549f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner}
1550e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner
1551464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruthnamespace {
1552464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  /// \brief Predicate tests whether a ValueEntry's op is in a map.
1553464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  struct IsValueInMap {
1554464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    const DenseMap<Value *, unsigned> &Map;
1555464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1556464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    IsValueInMap(const DenseMap<Value *, unsigned> &Map) : Map(Map) {}
1557464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1558464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    bool operator()(const ValueEntry &Entry) {
1559464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      return Map.find(Entry.Op) != Map.end();
1560464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    }
1561464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  };
1562464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth}
1563464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1564464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// \brief Build up a vector of value/power pairs factoring a product.
1565464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///
1566464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// Given a series of multiplication operands, build a vector of factors and
1567464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// the powers each is raised to when forming the final product. Sort them in
1568464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// the order of descending power.
1569464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///
1570464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///      (x*x)          -> [(x, 2)]
1571464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///     ((x*x)*x)       -> [(x, 3)]
1572464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///   ((((x*y)*x)*y)*x) -> [(x, 3), (y, 2)]
1573464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///
1574464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// \returns Whether any factors have a power greater than one.
1575464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruthbool Reassociate::collectMultiplyFactors(SmallVectorImpl<ValueEntry> &Ops,
1576464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                                         SmallVectorImpl<Factor> &Factors) {
15770fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // FIXME: Have Ops be (ValueEntry, Multiplicity) pairs, simplifying this.
15780fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Compute the sum of powers of simplifiable factors.
1579464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  unsigned FactorPowerSum = 0;
15800fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  for (unsigned Idx = 1, Size = Ops.size(); Idx < Size; ++Idx) {
15810fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Value *Op = Ops[Idx-1].Op;
15820fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
15830fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Count the number of occurrences of this value.
15840fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    unsigned Count = 1;
15850fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    for (; Idx < Size && Ops[Idx].Op == Op; ++Idx)
15860fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      ++Count;
1587464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    // Track for simplification all factors which occur 2 or more times.
15880fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    if (Count > 1)
15890fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      FactorPowerSum += Count;
1590464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  }
15910fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
1592464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // We can only simplify factors if the sum of the powers of our simplifiable
1593464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // factors is 4 or higher. When that is the case, we will *always* have
1594464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // a simplification. This is an important invariant to prevent cyclicly
1595464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // trying to simplify already minimal formations.
1596464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (FactorPowerSum < 4)
1597464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    return false;
1598464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
15990fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // Now gather the simplifiable factors, removing them from Ops.
16000fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  FactorPowerSum = 0;
16010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  for (unsigned Idx = 1; Idx < Ops.size(); ++Idx) {
16020fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Value *Op = Ops[Idx-1].Op;
1603464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
16040fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    // Count the number of occurrences of this value.
16050fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    unsigned Count = 1;
16060fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    for (; Idx < Ops.size() && Ops[Idx].Op == Op; ++Idx)
16070fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      ++Count;
16080fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    if (Count == 1)
16090fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands      continue;
1610d9b0b025612992a0b724eeca8bdf10b1d7a5c355Benjamin Kramer    // Move an even number of occurrences to Factors.
16110fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Count &= ~1U;
16120fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Idx -= Count;
16130fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    FactorPowerSum += Count;
16140fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Factors.push_back(Factor(Op, Count));
16150fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands    Ops.erase(Ops.begin()+Idx, Ops.begin()+Idx+Count);
1616464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  }
16170fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
1618464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // None of the adjustments above should have reduced the sum of factor powers
1619464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // below our mininum of '4'.
1620464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  assert(FactorPowerSum >= 4);
1621464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1622464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  std::sort(Factors.begin(), Factors.end(), Factor::PowerDescendingSorter());
1623464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  return true;
1624464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth}
1625464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1626464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// \brief Build a tree of multiplies, computing the product of Ops.
1627464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruthstatic Value *buildMultiplyTree(IRBuilder<> &Builder,
1628464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                                SmallVectorImpl<Value*> &Ops) {
1629464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (Ops.size() == 1)
1630464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    return Ops.back();
1631464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1632464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  Value *LHS = Ops.pop_back_val();
1633464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  do {
1634464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    LHS = Builder.CreateMul(LHS, Ops.pop_back_val());
1635464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  } while (!Ops.empty());
1636464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1637464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  return LHS;
1638464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth}
1639464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1640464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// \brief Build a minimal multiplication DAG for (a^x)*(b^y)*(c^z)*...
1641464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth///
1642464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// Given a vector of values raised to various powers, where no two values are
1643464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// equal and the powers are sorted in decreasing order, compute the minimal
1644464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// DAG of multiplies to compute the final product, and return that product
1645464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// value.
1646464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler CarruthValue *Reassociate::buildMinimalMultiplyDAG(IRBuilder<> &Builder,
1647464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                                            SmallVectorImpl<Factor> &Factors) {
1648464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  assert(Factors[0].Power);
1649464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  SmallVector<Value *, 4> OuterProduct;
1650464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  for (unsigned LastIdx = 0, Idx = 1, Size = Factors.size();
1651464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth       Idx < Size && Factors[Idx].Power > 0; ++Idx) {
1652464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    if (Factors[Idx].Power != Factors[LastIdx].Power) {
1653464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      LastIdx = Idx;
1654464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      continue;
1655464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    }
1656464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1657464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    // We want to multiply across all the factors with the same power so that
1658464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    // we can raise them to that power as a single entity. Build a mini tree
1659464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    // for that.
1660464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    SmallVector<Value *, 4> InnerProduct;
1661464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    InnerProduct.push_back(Factors[LastIdx].Base);
1662464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    do {
1663464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      InnerProduct.push_back(Factors[Idx].Base);
1664464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      ++Idx;
1665464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    } while (Idx < Size && Factors[Idx].Power == Factors[LastIdx].Power);
1666464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1667464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    // Reset the base value of the first factor to the new expression tree.
1668464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    // We'll remove all the factors with the same power in a second pass.
1669841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    Value *M = Factors[LastIdx].Base = buildMultiplyTree(Builder, InnerProduct);
1670841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    if (Instruction *MI = dyn_cast<Instruction>(M))
1671841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      RedoInsts.insert(MI);
1672464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1673464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    LastIdx = Idx;
1674464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  }
1675464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // Unique factors with equal powers -- we've folded them into the first one's
1676464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // base.
1677464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  Factors.erase(std::unique(Factors.begin(), Factors.end(),
1678464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                            Factor::PowerEqual()),
1679464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                Factors.end());
1680464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1681464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // Iteratively collect the base of each factor with an add power into the
1682464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // outer product, and halve each power in preparation for squaring the
1683464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // expression.
1684464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  for (unsigned Idx = 0, Size = Factors.size(); Idx != Size; ++Idx) {
1685464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    if (Factors[Idx].Power & 1)
1686464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      OuterProduct.push_back(Factors[Idx].Base);
1687464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    Factors[Idx].Power >>= 1;
1688464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  }
1689464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (Factors[0].Power) {
1690464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    Value *SquareRoot = buildMinimalMultiplyDAG(Builder, Factors);
1691464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    OuterProduct.push_back(SquareRoot);
1692464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    OuterProduct.push_back(SquareRoot);
1693464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  }
1694464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (OuterProduct.size() == 1)
1695464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    return OuterProduct.front();
1696464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1697a33701098936ffba12326d96e98d388357f3e098Duncan Sands  Value *V = buildMultiplyTree(Builder, OuterProduct);
1698a33701098936ffba12326d96e98d388357f3e098Duncan Sands  return V;
1699464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth}
1700464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1701464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler CarruthValue *Reassociate::OptimizeMul(BinaryOperator *I,
1702464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth                                SmallVectorImpl<ValueEntry> &Ops) {
1703464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // We can only optimize the multiplies when there is a chain of more than
1704464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // three, such that a balanced tree might require fewer total multiplies.
1705464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (Ops.size() < 4)
1706464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    return 0;
1707464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1708464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // Try to turn linear trees of multiplies without other uses of the
1709464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // intermediate stages into minimal multiply DAGs with perfect sub-expression
1710464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  // re-use.
1711464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  SmallVector<Factor, 4> Factors;
1712464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (!collectMultiplyFactors(Ops, Factors))
1713464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    return 0; // All distinct factors, so nothing left for us to do.
1714464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1715464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  IRBuilder<> Builder(I);
1716464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  Value *V = buildMinimalMultiplyDAG(Builder, Factors);
1717464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  if (Ops.empty())
1718464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    return V;
1719464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1720464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  ValueEntry NewEntry = ValueEntry(getRank(V), V);
1721464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  Ops.insert(std::lower_bound(Ops.begin(), Ops.end(), NewEntry), NewEntry);
1722464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  return 0;
1723464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth}
1724464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth
1725e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris LattnerValue *Reassociate::OptimizeExpression(BinaryOperator *I,
17269f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner                                       SmallVectorImpl<ValueEntry> &Ops) {
1727469001000620df176decd093a300db84a06cc78bChris Lattner  // Now that we have the linearized expression tree, try to optimize it.
1728469001000620df176decd093a300db84a06cc78bChris Lattner  // Start by folding any constants that we found.
17297ecfcc163956a9e27845ac217f6c650658631030Duncan Sands  Constant *Cst = 0;
1730e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  unsigned Opcode = I->getOpcode();
17317ecfcc163956a9e27845ac217f6c650658631030Duncan Sands  while (!Ops.empty() && isa<Constant>(Ops.back().Op)) {
17327ecfcc163956a9e27845ac217f6c650658631030Duncan Sands    Constant *C = cast<Constant>(Ops.pop_back_val().Op);
17337ecfcc163956a9e27845ac217f6c650658631030Duncan Sands    Cst = Cst ? ConstantExpr::get(Opcode, C, Cst) : C;
17347ecfcc163956a9e27845ac217f6c650658631030Duncan Sands  }
17357ecfcc163956a9e27845ac217f6c650658631030Duncan Sands  // If there was nothing but constants then we are done.
17367ecfcc163956a9e27845ac217f6c650658631030Duncan Sands  if (Ops.empty())
17377ecfcc163956a9e27845ac217f6c650658631030Duncan Sands    return Cst;
17387ecfcc163956a9e27845ac217f6c650658631030Duncan Sands
17397ecfcc163956a9e27845ac217f6c650658631030Duncan Sands  // Put the combined constant back at the end of the operand list, except if
17407ecfcc163956a9e27845ac217f6c650658631030Duncan Sands  // there is no point.  For example, an add of 0 gets dropped here, while a
17417ecfcc163956a9e27845ac217f6c650658631030Duncan Sands  // multiplication by zero turns the whole expression into zero.
17427ecfcc163956a9e27845ac217f6c650658631030Duncan Sands  if (Cst && Cst != ConstantExpr::getBinOpIdentity(Opcode, I->getType())) {
17437ecfcc163956a9e27845ac217f6c650658631030Duncan Sands    if (Cst == ConstantExpr::getBinOpAbsorber(Opcode, I->getType()))
17447ecfcc163956a9e27845ac217f6c650658631030Duncan Sands      return Cst;
17457ecfcc163956a9e27845ac217f6c650658631030Duncan Sands    Ops.push_back(ValueEntry(0, Cst));
17467ecfcc163956a9e27845ac217f6c650658631030Duncan Sands  }
17477ecfcc163956a9e27845ac217f6c650658631030Duncan Sands
17487ecfcc163956a9e27845ac217f6c650658631030Duncan Sands  if (Ops.size() == 1) return Ops[0].Op;
1749e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1750ec531233a16605756a84d175178e1ee0fac4791cChris Lattner  // Handle destructive annihilation due to identities between elements in the
1751469001000620df176decd093a300db84a06cc78bChris Lattner  // argument list here.
1752464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  unsigned NumOps = Ops.size();
1753109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  switch (Opcode) {
1754109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  default: break;
1755109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  case Instruction::And:
1756109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  case Instruction::Or:
1757f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner    if (Value *Result = OptimizeAndOrXor(Opcode, Ops))
1758f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      return Result;
1759109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner    break;
1760109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner
17612d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang  case Instruction::Xor:
17622d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    if (Value *Result = OptimizeXor(I, Ops))
17632d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang      return Result;
17642d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang    break;
17652d1001064989b7fa79507816fc17d467fc00a2f2Shuxin Yang
1766464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  case Instruction::Add:
176794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner    if (Value *Result = OptimizeAdd(I, Ops))
1768f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner      return Result;
1769464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    break;
1770e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner
1771464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth  case Instruction::Mul:
1772464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth    if (Value *Result = OptimizeMul(I, Ops))
1773464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth      return Result;
1774109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner    break;
1775109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner  }
1776109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner
1777841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (Ops.size() != NumOps)
1778e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner    return OptimizeExpression(I, Ops);
1779e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner  return 0;
1780469001000620df176decd093a300db84a06cc78bChris Lattner}
1781469001000620df176decd093a300db84a06cc78bChris Lattner
17824df2826166f1339eb7ddf5c5c84565fccb794de8Shuxin Yang/// EraseInst - Zap the given instruction, adding interesting operands to the
17834df2826166f1339eb7ddf5c5c84565fccb794de8Shuxin Yang/// work list.
17844df2826166f1339eb7ddf5c5c84565fccb794de8Shuxin Yangvoid Reassociate::EraseInst(Instruction *I) {
1785841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  assert(isInstructionTriviallyDead(I) && "Trivially dead instructions only!");
1786841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
1787841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  // Erase the dead instruction.
1788841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  ValueRankMap.erase(I);
17894df2826166f1339eb7ddf5c5c84565fccb794de8Shuxin Yang  RedoInsts.remove(I);
1790841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  I->eraseFromParent();
1791841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  // Optimize its operands.
1792cd117f736c47947af5c6549734549e135e626c5cDuncan Sands  SmallPtrSet<Instruction *, 8> Visited; // Detect self-referential nodes.
1793841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1794841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    if (Instruction *Op = dyn_cast<Instruction>(Ops[i])) {
1795841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      // If this is a node in an expression tree, climb to the expression root
1796841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      // and add that since that's where optimization actually happens.
1797841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      unsigned Opcode = Op->getOpcode();
1798cd117f736c47947af5c6549734549e135e626c5cDuncan Sands      while (Op->hasOneUse() && Op->use_back()->getOpcode() == Opcode &&
1799cd117f736c47947af5c6549734549e135e626c5cDuncan Sands             Visited.insert(Op))
1800841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        Op = Op->use_back();
18014df2826166f1339eb7ddf5c5c84565fccb794de8Shuxin Yang      RedoInsts.insert(Op);
1802841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    }
1803841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands}
1804841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands
1805841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands/// OptimizeInst - Inspect and optimize the given instruction. Note that erasing
1806841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands/// instructions is not allowed.
1807841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsvoid Reassociate::OptimizeInst(Instruction *I) {
1808841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  // Only consider operations that we understand.
1809841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (!isa<BinaryOperator>(I))
1810841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    return;
1811841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands
1812841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (I->getOpcode() == Instruction::Shl &&
1813841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      isa<ConstantInt>(I->getOperand(1)))
1814841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    // If an operand of this shift is a reassociable multiply, or if the shift
1815841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    // is used by a reassociable multiply or add, turn into a multiply.
1816841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    if (isReassociableOp(I->getOperand(0), Instruction::Mul) ||
1817841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        (I->hasOneUse() &&
1818841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands         (isReassociableOp(I->use_back(), Instruction::Mul) ||
1819841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands          isReassociableOp(I->use_back(), Instruction::Add)))) {
1820841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      Instruction *NI = ConvertShiftToMul(I);
1821841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      RedoInsts.insert(I);
1822dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman      MadeChange = true;
1823841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      I = NI;
1824dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman    }
1825641f02f10f08c9a9add651c6f0169f5441eaeb49Chris Lattner
1826423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson  // Floating point binary operators are not associative, but we can still
1827423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson  // commute (some) of them, to canonicalize the order of their operands.
1828423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson  // This can potentially expose more CSE opportunities, and makes writing
1829423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson  // other transformations simpler.
1830841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if ((I->getType()->isFloatingPointTy() || I->getType()->isVectorTy())) {
1831423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    // FAdd and FMul can be commuted.
1832841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    if (I->getOpcode() != Instruction::FMul &&
1833841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        I->getOpcode() != Instruction::FAdd)
1834423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson      return;
1835423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson
1836841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    Value *LHS = I->getOperand(0);
1837841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    Value *RHS = I->getOperand(1);
1838423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    unsigned LHSRank = getRank(LHS);
1839423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    unsigned RHSRank = getRank(RHS);
1840423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson
1841423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    // Sort the operands by rank.
1842423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    if (RHSRank < LHSRank) {
1843841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      I->setOperand(0, RHS);
1844841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      I->setOperand(1, LHS);
1845423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    }
1846423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson
1847423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson    return;
1848423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson  }
1849423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson
1850dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // Do not reassociate boolean (i1) expressions.  We want to preserve the
1851dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // original order of evaluation for short-circuited comparisons that
1852dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // SimplifyCFG has folded to AND/OR expressions.  If the expression
1853dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // is not further optimized, it is likely to be transformed back to a
1854dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // short-circuited form for code gen, and the source order may have been
1855dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // optimized for the most likely conditions.
1856841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (I->getType()->isIntegerTy(1))
1857dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman    return;
1858fc375d22001d27ba6d22db67821da057e36f7f89Bob Wilson
1859dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // If this is a subtract instruction which is not already in negate form,
1860dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // see if we can convert it to X+-Y.
1861841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (I->getOpcode() == Instruction::Sub) {
1862841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    if (ShouldBreakUpSubtract(I)) {
1863841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      Instruction *NI = BreakUpSubtract(I);
1864841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      RedoInsts.insert(I);
1865dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman      MadeChange = true;
1866841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      I = NI;
1867841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    } else if (BinaryOperator::isNeg(I)) {
1868dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman      // Otherwise, this is a negation.  See if the operand is a multiply tree
1869dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman      // and if this is not an inner node of a multiply tree.
1870841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      if (isReassociableOp(I->getOperand(1), Instruction::Mul) &&
1871841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands          (!I->hasOneUse() ||
1872841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands           !isReassociableOp(I->use_back(), Instruction::Mul))) {
1873841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        Instruction *NI = LowerNegateToMultiply(I);
1874841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        RedoInsts.insert(I);
1875d5b8d92b9f4dfb216e4f2a52b4e801d7559574baChris Lattner        MadeChange = true;
1876841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        I = NI;
187708b43921e18f314c4fd38049291d323830934c36Chris Lattner      }
1878f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner    }
1879dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  }
1880e4b730441dab4aff9a69aeddbdea98990e7703c4Chris Lattner
1881841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  // If this instruction is an associative binary operator, process it.
1882841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (!I->isAssociative()) return;
1883841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  BinaryOperator *BO = cast<BinaryOperator>(I);
188400b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen
1885dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // If this is an interior node of a reassociable tree, ignore it until we
1886dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // get to the root of the tree, to avoid N^2 analysis.
1887c1deb67d78ac987577e9caa22d60435239ad0e12Nadav Rotem  unsigned Opcode = BO->getOpcode();
1888c1deb67d78ac987577e9caa22d60435239ad0e12Nadav Rotem  if (BO->hasOneUse() && BO->use_back()->getOpcode() == Opcode)
1889dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman    return;
1890c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner
1891e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling  // If this is an add tree that is used by a sub instruction, ignore it
1892dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman  // until we process the subtract.
1893841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  if (BO->hasOneUse() && BO->getOpcode() == Instruction::Add &&
1894841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      cast<Instruction>(BO->use_back())->getOpcode() == Instruction::Sub)
1895dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman    return;
18967b4ad94282b94e1827be29b4db73fdf6e241f748Chris Lattner
1897841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  ReassociateExpression(BO);
1898895b392269cad07c34d59110d68dc86708c53adbChris Lattner}
1899c9fd097a01383323f166c14c17d3984620cad766Chris Lattner
1900cd117f736c47947af5c6549734549e135e626c5cDuncan Sandsvoid Reassociate::ReassociateExpression(BinaryOperator *I) {
1901e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
190269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  // First, walk the expression tree, linearizing the tree, collecting the
190369e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  // operand information.
1904c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  SmallVector<RepeatedValue, 8> Tree;
1905c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  MadeChange |= LinearizeExprTree(I, Tree);
19069f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner  SmallVector<ValueEntry, 8> Ops;
1907c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  Ops.reserve(Tree.size());
1908c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  for (unsigned i = 0, e = Tree.size(); i != e; ++i) {
1909c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    RepeatedValue E = Tree[i];
1910c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands    Ops.append(E.second.getZExtValue(),
1911c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands               ValueEntry(getRank(E.first), E.first));
1912c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands  }
1913e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
191424dfa52fa20eee39440e5dec4d23359e5a6773c7Duncan Sands  DEBUG(dbgs() << "RAIn:\t"; PrintOps(I, Ops); dbgs() << '\n');
191524dfa52fa20eee39440e5dec4d23359e5a6773c7Duncan Sands
1916895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // Now that we have linearized the tree to a list and have gathered all of
1917895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // the operands and their ranks, sort the operands by their rank.  Use a
1918895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // stable_sort so that values with equal ranks will have their relative
1919895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // positions maintained (and so the compiler is deterministic).  Note that
1920895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // this sorts so that the highest ranking values end up at the beginning of
1921895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // the vector.
1922895b392269cad07c34d59110d68dc86708c53adbChris Lattner  std::stable_sort(Ops.begin(), Ops.end());
1923e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1924895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // OptimizeExpression - Now that we have the expression tree in a convenient
1925895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // sorted form, optimize it globally if possible.
1926895b392269cad07c34d59110d68dc86708c53adbChris Lattner  if (Value *V = OptimizeExpression(I, Ops)) {
1927cd117f736c47947af5c6549734549e135e626c5cDuncan Sands    if (V == I)
1928cd117f736c47947af5c6549734549e135e626c5cDuncan Sands      // Self-referential expression in unreachable code.
1929cd117f736c47947af5c6549734549e135e626c5cDuncan Sands      return;
1930895b392269cad07c34d59110d68dc86708c53adbChris Lattner    // This expression tree simplified to something that isn't a tree,
1931895b392269cad07c34d59110d68dc86708c53adbChris Lattner    // eliminate it.
1932a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene    DEBUG(dbgs() << "Reassoc to scalar: " << *V << '\n');
1933895b392269cad07c34d59110d68dc86708c53adbChris Lattner    I->replaceAllUsesWith(V);
19345367b23f76e75ebb680956575346fa8c3d56780fDevang Patel    if (Instruction *VI = dyn_cast<Instruction>(V))
19355367b23f76e75ebb680956575346fa8c3d56780fDevang Patel      VI->setDebugLoc(I->getDebugLoc());
1936841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    RedoInsts.insert(I);
19379fdaefad580194353f34b6d72669591f8f9d811aChris Lattner    ++NumAnnihil;
1938cd117f736c47947af5c6549734549e135e626c5cDuncan Sands    return;
1939895b392269cad07c34d59110d68dc86708c53adbChris Lattner  }
1940e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1941895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // We want to sink immediates as deeply as possible except in the case where
1942895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // this is a multiply tree used only by an add, and the immediate is a -1.
1943895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // In this case we reassociate to put the negation on the outside so that we
1944895b392269cad07c34d59110d68dc86708c53adbChris Lattner  // can fold the negation into the add: (-X)*Y + Z -> Z-X*Y
1945895b392269cad07c34d59110d68dc86708c53adbChris Lattner  if (I->getOpcode() == Instruction::Mul && I->hasOneUse() &&
1946895b392269cad07c34d59110d68dc86708c53adbChris Lattner      cast<Instruction>(I->use_back())->getOpcode() == Instruction::Add &&
1947895b392269cad07c34d59110d68dc86708c53adbChris Lattner      isa<ConstantInt>(Ops.back().Op) &&
1948895b392269cad07c34d59110d68dc86708c53adbChris Lattner      cast<ConstantInt>(Ops.back().Op)->isAllOnesValue()) {
19499f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner    ValueEntry Tmp = Ops.pop_back_val();
19509f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner    Ops.insert(Ops.begin(), Tmp);
1951895b392269cad07c34d59110d68dc86708c53adbChris Lattner  }
1952e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1953a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene  DEBUG(dbgs() << "RAOut:\t"; PrintOps(I, Ops); dbgs() << '\n');
1954e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
1955895b392269cad07c34d59110d68dc86708c53adbChris Lattner  if (Ops.size() == 1) {
1956cd117f736c47947af5c6549734549e135e626c5cDuncan Sands    if (Ops[0].Op == I)
1957cd117f736c47947af5c6549734549e135e626c5cDuncan Sands      // Self-referential expression in unreachable code.
1958cd117f736c47947af5c6549734549e135e626c5cDuncan Sands      return;
1959cd117f736c47947af5c6549734549e135e626c5cDuncan Sands
1960895b392269cad07c34d59110d68dc86708c53adbChris Lattner    // This expression tree simplified to something that isn't a tree,
1961895b392269cad07c34d59110d68dc86708c53adbChris Lattner    // eliminate it.
1962895b392269cad07c34d59110d68dc86708c53adbChris Lattner    I->replaceAllUsesWith(Ops[0].Op);
19635367b23f76e75ebb680956575346fa8c3d56780fDevang Patel    if (Instruction *OI = dyn_cast<Instruction>(Ops[0].Op))
19645367b23f76e75ebb680956575346fa8c3d56780fDevang Patel      OI->setDebugLoc(I->getDebugLoc());
1965841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    RedoInsts.insert(I);
1966cd117f736c47947af5c6549734549e135e626c5cDuncan Sands    return;
19674fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner  }
1968e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling
196969e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  // Now that we ordered and optimized the expressions, splat them back into
197069e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  // the expression tree, removing any unneeded nodes.
197169e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner  RewriteExprTree(I, Ops);
19724fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner}
19734fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
19747e70829632f82de15db187845666aaca6e04b792Chris Lattnerbool Reassociate::runOnFunction(Function &F) {
1975841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  // Calculate the rank map for F
19764fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner  BuildRankMap(F);
19774fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
1978c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner  MadeChange = false;
1979841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
1980841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    // Optimize every instruction in the basic block.
1981841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE; )
1982841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      if (isInstructionTriviallyDead(II)) {
1983841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        EraseInst(II++);
1984841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      } else {
1985841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        OptimizeInst(II);
1986841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        assert(II->getParent() == BI && "Moved to a different block!");
1987841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        ++II;
1988841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      }
1989841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands
1990841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    // If this produced extra instructions to optimize, handle them now.
1991841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands    while (!RedoInsts.empty()) {
19924df2826166f1339eb7ddf5c5c84565fccb794de8Shuxin Yang      Instruction *I = RedoInsts.pop_back_val();
1993841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      if (isInstructionTriviallyDead(I))
1994841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        EraseInst(I);
1995841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands      else
1996841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands        OptimizeInst(I);
1997dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman    }
1998841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands  }
19994fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner
20000fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  // We are done with the rank map.
20010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  RankMap.clear();
20020fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands  ValueRankMap.clear();
20030fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands
2004c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner  return MadeChange;
20054fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner}
2006