Reassociate.cpp revision 917f99354fa558e50d17191f593f81155b4ab2c3
14fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner//===- Reassociate.cpp - Reassociate binary expressions -------------------===// 2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// The LLVM Compiler Infrastructure 4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// 54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source 64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details. 7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===// 94fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// 104fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// This pass reassociates commutative expressions in an order that is designed 119046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner// to promote better constant propagation, GCSE, LICM, PRE, etc. 124fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// 134fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// For example: 4 + (x + 5) -> x + (4 + 5) 144fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// 154fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// In the implementation of this algorithm, constants are assigned rank = 0, 164fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// function arguments are rank = 1, and other values are assigned ranks 174fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// corresponding to the reverse post order traversal of current function 184fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// (starting at 2), which effectively gives values in deep loops higher rank 194fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// than values not in loops. 204fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner// 214fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner//===----------------------------------------------------------------------===// 224fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner 2308b43921e18f314c4fd38049291d323830934c36Chris Lattner#define DEBUG_TYPE "reassociate" 244fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner#include "llvm/Transforms/Scalar.h" 25fa0e6facc793d0a67e89f873e18cd35a9d7c02e0Dan Gohman#include "llvm/Transforms/Utils/Local.h" 260975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner#include "llvm/Constants.h" 27ae74f555522298bef3be8a173163bf778d59adf9Chris Lattner#include "llvm/DerivedTypes.h" 284fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner#include "llvm/Function.h" 29d8e1eea678833cc2b15e4ea69a5a403ba9c3b013Misha Brukman#include "llvm/Instructions.h" 3003afd02ca2486aebb3b29edd2f77920d4e5020fdDale Johannesen#include "llvm/IntrinsicInst.h" 314fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner#include "llvm/Pass.h" 32c9fd097a01383323f166c14c17d3984620cad766Chris Lattner#include "llvm/Assembly/Writer.h" 334fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner#include "llvm/Support/CFG.h" 34464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth#include "llvm/Support/IRBuilder.h" 35551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h" 36d3c7b7359d4992b9ab9f8e12ccd0a9b7d2446566Chris Lattner#include "llvm/Support/ValueHandle.h" 37bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner#include "llvm/Support/raw_ostream.h" 380fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands#include "llvm/ADT/DenseMap.h" 39551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/PostOrderIterator.h" 40841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands#include "llvm/ADT/SetVector.h" 41464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth#include "llvm/ADT/STLExtras.h" 42551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h" 43c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner#include <algorithm> 44d7456026629fc1760a45e6e955e9834246493147Chris Lattnerusing namespace llvm; 45d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 460e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumChanged, "Number of insts reassociated"); 470e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumAnnihil, "Number of expr tree annihilated"); 480e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumFactor , "Number of multiplies factored"); 49a92f696b74a99325026ebbdbffd2a44317e0c10bChris Lattner 500e5f499638c8d277b9dc4a4385712177c53b5681Chris Lattnernamespace { 513e8b6631e67e01e4960a7ba4668a50c596607473Chris Lattner struct ValueEntry { 52c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner unsigned Rank; 53c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner Value *Op; 54c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner ValueEntry(unsigned R, Value *O) : Rank(R), Op(O) {} 55c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner }; 56c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner inline bool operator<(const ValueEntry &LHS, const ValueEntry &RHS) { 57c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner return LHS.Rank > RHS.Rank; // Sort so that highest rank goes to start. 58c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner } 59e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner} 60c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner 6150cacb2a520b93530e79220a307c907163b9e370Devang Patel#ifndef NDEBUG 62e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// PrintOps - Print out the expression identified in the Ops list. 63e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// 649f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattnerstatic void PrintOps(Instruction *I, const SmallVectorImpl<ValueEntry> &Ops) { 65e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner Module *M = I->getParent()->getParent()->getParent(); 66a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene dbgs() << Instruction::getOpcodeName(I->getOpcode()) << " " 671befe643b2a030f5e2433ce0034a27fb65b5f26bChris Lattner << *Ops[0].Op->getType() << '\t'; 687de3b5db26bb3c8dcca5348fb7c0be4f9bd1bcb7Chris Lattner for (unsigned i = 0, e = Ops.size(); i != e; ++i) { 69a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene dbgs() << "[ "; 70a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene WriteAsOperand(dbgs(), Ops[i].Op, false, M); 71a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene dbgs() << ", #" << Ops[i].Rank << "] "; 727de3b5db26bb3c8dcca5348fb7c0be4f9bd1bcb7Chris Lattner } 73e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner} 7459500c8f9a76b3386329b6f837255c16f4e8b61bDevang Patel#endif 75e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 76844731a7f1909f55935e3514c9e713a62d67662eDan Gohmannamespace { 77464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth /// \brief Utility class representing a base and exponent pair which form one 78464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth /// factor of some product. 79464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth struct Factor { 80464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth Value *Base; 81464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth unsigned Power; 82464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 83464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth Factor(Value *Base, unsigned Power) : Base(Base), Power(Power) {} 84464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 85464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth /// \brief Sort factors by their Base. 86464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth struct BaseSorter { 87464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth bool operator()(const Factor &LHS, const Factor &RHS) { 88464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return LHS.Base < RHS.Base; 89464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth } 90464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth }; 91464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 92464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth /// \brief Compare factors for equal bases. 93464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth struct BaseEqual { 94464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth bool operator()(const Factor &LHS, const Factor &RHS) { 95464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return LHS.Base == RHS.Base; 96464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth } 97464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth }; 98464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 99464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth /// \brief Sort factors in descending order by their power. 100464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth struct PowerDescendingSorter { 101464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth bool operator()(const Factor &LHS, const Factor &RHS) { 102464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return LHS.Power > RHS.Power; 103464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth } 104464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth }; 105464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 106464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth /// \brief Compare factors for equal powers. 107464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth struct PowerEqual { 108464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth bool operator()(const Factor &LHS, const Factor &RHS) { 109464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return LHS.Power == RHS.Power; 110464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth } 111464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth }; 112464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth }; 113464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth} 114464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 115464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruthnamespace { 1163e8b6631e67e01e4960a7ba4668a50c596607473Chris Lattner class Reassociate : public FunctionPass { 117f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner DenseMap<BasicBlock*, unsigned> RankMap; 118f1d0f7781e766df878bec4e7977fa3204374f394Craig Topper DenseMap<AssertingVH<Value>, unsigned> ValueRankMap; 119841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands SetVector<AssertingVH<Instruction> > RedoInsts; 120c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner bool MadeChange; 1214fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner public: 122ecd94c804a563f2a86572dcf1d2e81f397e19daaNick Lewycky static char ID; // Pass identification, replacement for typeid 123081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson Reassociate() : FunctionPass(ID) { 124081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson initializeReassociatePass(*PassRegistry::getPassRegistry()); 125081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson } 126794fd75c67a2cdc128d67342c6d88a504d186896Devang Patel 1277e70829632f82de15db187845666aaca6e04b792Chris Lattner bool runOnFunction(Function &F); 1284fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner 1294fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner virtual void getAnalysisUsage(AnalysisUsage &AU) const { 130cb2610ea037a17115ef3a01a6bdaab4e3cfdca27Chris Lattner AU.setPreservesCFG(); 1314fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner } 1324fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner private: 1337e70829632f82de15db187845666aaca6e04b792Chris Lattner void BuildRankMap(Function &F); 1344fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner unsigned getRank(Value *V); 135cd117f736c47947af5c6549734549e135e626c5cDuncan Sands void ReassociateExpression(BinaryOperator *I); 1360fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands void RewriteExprTree(BinaryOperator *I, SmallVectorImpl<ValueEntry> &Ops); 1379f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner Value *OptimizeExpression(BinaryOperator *I, 1389f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner SmallVectorImpl<ValueEntry> &Ops); 1399f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner Value *OptimizeAdd(Instruction *I, SmallVectorImpl<ValueEntry> &Ops); 140464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth bool collectMultiplyFactors(SmallVectorImpl<ValueEntry> &Ops, 141464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth SmallVectorImpl<Factor> &Factors); 142464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth Value *buildMinimalMultiplyDAG(IRBuilder<> &Builder, 143464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth SmallVectorImpl<Factor> &Factors); 144464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth Value *OptimizeMul(BinaryOperator *I, SmallVectorImpl<ValueEntry> &Ops); 145e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner Value *RemoveFactorFromExpression(Value *V, Value *Factor); 146841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands void EraseInst(Instruction *I); 147841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands void OptimizeInst(Instruction *I); 1484fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner }; 1494fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner} 1504fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner 151844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar Reassociate::ID = 0; 152d13db2c59cc94162d6cf0a04187d408bfef6d4a7Owen AndersonINITIALIZE_PASS(Reassociate, "reassociate", 153ce665bd2e2b581ab0858d1afe359192bac96b868Owen Anderson "Reassociate expressions", false, false) 154844731a7f1909f55935e3514c9e713a62d67662eDan Gohman 155d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke// Public interface to the Reassociate pass 156d7456026629fc1760a45e6e955e9834246493147Chris LattnerFunctionPass *llvm::createReassociatePass() { return new Reassociate(); } 1574fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner 1580fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// isReassociableOp - Return true if V is an instruction of the specified 1590fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// opcode and if it only has one use. 1600fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sandsstatic BinaryOperator *isReassociableOp(Value *V, unsigned Opcode) { 1610fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (V->hasOneUse() && isa<Instruction>(V) && 1620fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands cast<Instruction>(V)->getOpcode() == Opcode) 1630fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands return cast<BinaryOperator>(V); 1640fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands return 0; 1650fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands} 1660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 1679c723199384b16899831937e2800d52f4f953569Chris Lattnerstatic bool isUnmovableInstruction(Instruction *I) { 1689c723199384b16899831937e2800d52f4f953569Chris Lattner if (I->getOpcode() == Instruction::PHI || 16998bda3dfefcb08e6ce81fa9545b05eb433cd5b87Bill Wendling I->getOpcode() == Instruction::LandingPad || 1709c723199384b16899831937e2800d52f4f953569Chris Lattner I->getOpcode() == Instruction::Alloca || 1719c723199384b16899831937e2800d52f4f953569Chris Lattner I->getOpcode() == Instruction::Load || 1729c723199384b16899831937e2800d52f4f953569Chris Lattner I->getOpcode() == Instruction::Invoke || 17303afd02ca2486aebb3b29edd2f77920d4e5020fdDale Johannesen (I->getOpcode() == Instruction::Call && 17403afd02ca2486aebb3b29edd2f77920d4e5020fdDale Johannesen !isa<DbgInfoIntrinsic>(I)) || 175e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling I->getOpcode() == Instruction::UDiv || 1761628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer I->getOpcode() == Instruction::SDiv || 1771628cec4d7fce310d9cde0bcc73997e5a71692c4Reid Spencer I->getOpcode() == Instruction::FDiv || 1780a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer I->getOpcode() == Instruction::URem || 1790a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer I->getOpcode() == Instruction::SRem || 1800a783f783ca05c961234385f5b269d4cf03dbbdbReid Spencer I->getOpcode() == Instruction::FRem) 1819c723199384b16899831937e2800d52f4f953569Chris Lattner return true; 1829c723199384b16899831937e2800d52f4f953569Chris Lattner return false; 1839c723199384b16899831937e2800d52f4f953569Chris Lattner} 1849c723199384b16899831937e2800d52f4f953569Chris Lattner 1857e70829632f82de15db187845666aaca6e04b792Chris Lattnervoid Reassociate::BuildRankMap(Function &F) { 1866007cb6c4d923e2dee4a1133fb6d1bb00a37062dChris Lattner unsigned i = 2; 187fb5be090f59997deb7a2e89c92bac19528ba6755Chris Lattner 188fb5be090f59997deb7a2e89c92bac19528ba6755Chris Lattner // Assign distinct ranks to function arguments 189e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) 190d3c7b7359d4992b9ab9f8e12ccd0a9b7d2446566Chris Lattner ValueRankMap[&*I] = ++i; 191fb5be090f59997deb7a2e89c92bac19528ba6755Chris Lattner 1927e70829632f82de15db187845666aaca6e04b792Chris Lattner ReversePostOrderTraversal<Function*> RPOT(&F); 1934fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner for (ReversePostOrderTraversal<Function*>::rpo_iterator I = RPOT.begin(), 1949c723199384b16899831937e2800d52f4f953569Chris Lattner E = RPOT.end(); I != E; ++I) { 1959c723199384b16899831937e2800d52f4f953569Chris Lattner BasicBlock *BB = *I; 1969c723199384b16899831937e2800d52f4f953569Chris Lattner unsigned BBRank = RankMap[BB] = ++i << 16; 1979c723199384b16899831937e2800d52f4f953569Chris Lattner 1989c723199384b16899831937e2800d52f4f953569Chris Lattner // Walk the basic block, adding precomputed ranks for any instructions that 1999c723199384b16899831937e2800d52f4f953569Chris Lattner // we cannot move. This ensures that the ranks for these instructions are 2009c723199384b16899831937e2800d52f4f953569Chris Lattner // all different in the block. 2019c723199384b16899831937e2800d52f4f953569Chris Lattner for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 2029c723199384b16899831937e2800d52f4f953569Chris Lattner if (isUnmovableInstruction(I)) 203d3c7b7359d4992b9ab9f8e12ccd0a9b7d2446566Chris Lattner ValueRankMap[&*I] = ++BBRank; 2049c723199384b16899831937e2800d52f4f953569Chris Lattner } 2054fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner} 2064fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner 2074fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattnerunsigned Reassociate::getRank(Value *V) { 20808b43921e18f314c4fd38049291d323830934c36Chris Lattner Instruction *I = dyn_cast<Instruction>(V); 209f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner if (I == 0) { 210f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner if (isa<Argument>(V)) return ValueRankMap[V]; // Function argument. 211f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner return 0; // Otherwise it's a global or constant, rank 0. 212f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner } 2134fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner 214f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner if (unsigned Rank = ValueRankMap[I]) 215f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner return Rank; // Rank already known? 21600b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen 21708b43921e18f314c4fd38049291d323830934c36Chris Lattner // If this is an expression, return the 1+MAX(rank(LHS), rank(RHS)) so that 21808b43921e18f314c4fd38049291d323830934c36Chris Lattner // we can reassociate expressions for code motion! Since we do not recurse 21908b43921e18f314c4fd38049291d323830934c36Chris Lattner // for PHI nodes, we cannot have infinite recursion here, because there 22008b43921e18f314c4fd38049291d323830934c36Chris Lattner // cannot be loops in the value graph that do not go through PHI nodes. 22108b43921e18f314c4fd38049291d323830934c36Chris Lattner unsigned Rank = 0, MaxRank = RankMap[I->getParent()]; 22208b43921e18f314c4fd38049291d323830934c36Chris Lattner for (unsigned i = 0, e = I->getNumOperands(); 22308b43921e18f314c4fd38049291d323830934c36Chris Lattner i != e && Rank != MaxRank; ++i) 22408b43921e18f314c4fd38049291d323830934c36Chris Lattner Rank = std::max(Rank, getRank(I->getOperand(i))); 22500b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen 226cc8a2b98f28c10d93f45489b8c6f0c8b8205bb3bChris Lattner // If this is a not or neg instruction, do not count it for rank. This 227cc8a2b98f28c10d93f45489b8c6f0c8b8205bb3bChris Lattner // assures us that X and ~X will have the same rank. 228b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands if (!I->getType()->isIntegerTy() || 229fa82b6eba4e1584d7dba291c28fe908272e1e002Owen Anderson (!BinaryOperator::isNot(I) && !BinaryOperator::isNeg(I))) 230cc8a2b98f28c10d93f45489b8c6f0c8b8205bb3bChris Lattner ++Rank; 231cc8a2b98f28c10d93f45489b8c6f0c8b8205bb3bChris Lattner 232a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene //DEBUG(dbgs() << "Calculated Rank[" << V->getName() << "] = " 233bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner // << Rank << "\n"); 23400b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen 235f55e7f54b1877aa6a58b368084cc25acbaa30967Chris Lattner return ValueRankMap[I] = Rank; 2364fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner} 2374fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner 238f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner/// LowerNegateToMultiply - Replace 0-X with X*-1. 239f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner/// 240841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsstatic BinaryOperator *LowerNegateToMultiply(Instruction *Neg) { 241a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson Constant *Cst = Constant::getAllOnesValue(Neg->getType()); 242f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner 2430fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands BinaryOperator *Res = 2440fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands BinaryOperator::CreateMul(Neg->getOperand(1), Cst, "",Neg); 245841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Neg->setOperand(1, Constant::getNullValue(Neg->getType())); // Drop use of op. 2466934a04a8c15e9971cd1ea4d5c8df2d7afdd5be5Chris Lattner Res->takeName(Neg); 247f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner Neg->replaceAllUsesWith(Res); 2485367b23f76e75ebb680956575346fa8c3d56780fDevang Patel Res->setDebugLoc(Neg->getDebugLoc()); 249f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner return Res; 250f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner} 251f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner 252c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// CarmichaelShift - Returns k such that lambda(2^Bitwidth) = 2^k, where lambda 253c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// is the Carmichael function. This means that x^(2^k) === 1 mod 2^Bitwidth for 254c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// every odd x, i.e. x^(2^k) = 1 for every odd x in Bitwidth-bit arithmetic. 255c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// Note that 0 <= k < Bitwidth, and if Bitwidth > 3 then x^(2^k) = 0 for every 256c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// even x in Bitwidth-bit arithmetic. 257c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandsstatic unsigned CarmichaelShift(unsigned Bitwidth) { 258c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands if (Bitwidth < 3) 259c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands return Bitwidth - 1; 260c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands return Bitwidth - 2; 261c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands} 262c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands 263c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// IncorporateWeight - Add the extra weight 'RHS' to the existing weight 'LHS', 264c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// reducing the combined weight using any special properties of the operation. 265c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// The existing weight LHS represents the computation X op X op ... op X where 266c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// X occurs LHS times. The combined weight represents X op X op ... op X with 267c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// X occurring LHS + RHS times. If op is "Xor" for example then the combined 268c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// operation is equivalent to X if LHS + RHS is odd, or 0 if LHS + RHS is even; 269c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// the routine returns 1 in LHS in the first case, and 0 in LHS in the second. 270c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandsstatic void IncorporateWeight(APInt &LHS, const APInt &RHS, unsigned Opcode) { 271c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // If we were working with infinite precision arithmetic then the combined 272c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // weight would be LHS + RHS. But we are using finite precision arithmetic, 273c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // and the APInt sum LHS + RHS may not be correct if it wraps (it is correct 274c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // for nilpotent operations and addition, but not for idempotent operations 275c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // and multiplication), so it is important to correctly reduce the combined 276c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // weight back into range if wrapping would be wrong. 277c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands 278c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // If RHS is zero then the weight didn't change. 279c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands if (RHS.isMinValue()) 280c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands return; 281c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // If LHS is zero then the combined weight is RHS. 282c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands if (LHS.isMinValue()) { 283c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands LHS = RHS; 284c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands return; 285c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands } 286c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // From this point on we know that neither LHS nor RHS is zero. 287c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands 288c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands if (Instruction::isIdempotent(Opcode)) { 289c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // Idempotent means X op X === X, so any non-zero weight is equivalent to a 290c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // weight of 1. Keeping weights at zero or one also means that wrapping is 291c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // not a problem. 292c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands assert(LHS == 1 && RHS == 1 && "Weights not reduced!"); 293c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands return; // Return a weight of 1. 294c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands } 295c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands if (Instruction::isNilpotent(Opcode)) { 296c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // Nilpotent means X op X === 0, so reduce weights modulo 2. 297c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands assert(LHS == 1 && RHS == 1 && "Weights not reduced!"); 298c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands LHS = 0; // 1 + 1 === 0 modulo 2. 299c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands return; 300c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands } 301c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands if (Opcode == Instruction::Add) { 302c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // TODO: Reduce the weight by exploiting nsw/nuw? 303c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands LHS += RHS; 304c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands return; 305c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands } 306c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands 307c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands assert(Opcode == Instruction::Mul && "Unknown associative operation!"); 308c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands unsigned Bitwidth = LHS.getBitWidth(); 309c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // If CM is the Carmichael number then a weight W satisfying W >= CM+Bitwidth 310c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // can be replaced with W-CM. That's because x^W=x^(W-CM) for every Bitwidth 311c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // bit number x, since either x is odd in which case x^CM = 1, or x is even in 312c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // which case both x^W and x^(W - CM) are zero. By subtracting off multiples 313c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // of CM like this weights can always be reduced to the range [0, CM+Bitwidth) 314c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // which by a happy accident means that they can always be represented using 315c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // Bitwidth bits. 316c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // TODO: Reduce the weight by exploiting nsw/nuw? (Could do much better than 317c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // the Carmichael number). 318c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands if (Bitwidth > 3) { 319c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands /// CM - The value of Carmichael's lambda function. 320c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands APInt CM = APInt::getOneBitSet(Bitwidth, CarmichaelShift(Bitwidth)); 321c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // Any weight W >= Threshold can be replaced with W - CM. 322c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands APInt Threshold = CM + Bitwidth; 323c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands assert(LHS.ult(Threshold) && RHS.ult(Threshold) && "Weights not reduced!"); 324c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // For Bitwidth 4 or more the following sum does not overflow. 325c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands LHS += RHS; 326c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands while (LHS.uge(Threshold)) 327c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands LHS -= CM; 328c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands } else { 329c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // To avoid problems with overflow do everything the same as above but using 330c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // a larger type. 331c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands unsigned CM = 1U << CarmichaelShift(Bitwidth); 332c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands unsigned Threshold = CM + Bitwidth; 333c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands assert(LHS.getZExtValue() < Threshold && RHS.getZExtValue() < Threshold && 334c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands "Weights not reduced!"); 335c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands unsigned Total = LHS.getZExtValue() + RHS.getZExtValue(); 336c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands while (Total >= Threshold) 337c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Total -= CM; 338c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands LHS = Total; 339c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands } 340c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands} 341c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands 342c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// EvaluateRepeatedConstant - Compute C op C op ... op C where the constant C 343c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// is repeated Weight times. 344c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandsstatic Constant *EvaluateRepeatedConstant(unsigned Opcode, Constant *C, 345c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands APInt Weight) { 346c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // For addition the result can be efficiently computed as the product of the 347c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // constant and the weight. 348c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands if (Opcode == Instruction::Add) 349c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands return ConstantExpr::getMul(C, ConstantInt::get(C->getContext(), Weight)); 350c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands 351c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // The weight might be huge, so compute by repeated squaring to ensure that 352c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // compile time is proportional to the logarithm of the weight. 353c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Constant *Result = 0; 354c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Constant *Power = C; // Successively C, C op C, (C op C) op (C op C) etc. 355c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // Visit the bits in Weight. 356c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands while (Weight != 0) { 357c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // If the current bit in Weight is non-zero do Result = Result op Power. 358c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands if (Weight[0]) 359c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Result = Result ? ConstantExpr::get(Opcode, Result, Power) : Power; 360c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // Move on to the next bit if any more are non-zero. 361c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Weight = Weight.lshr(1); 362c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands if (Weight.isMinValue()) 363c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands break; 364c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // Square the power. 365c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Power = ConstantExpr::get(Opcode, Power, Power); 366c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands } 367c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands 368c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands assert(Result && "Only positive weights supported!"); 369c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands return Result; 370c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands} 371c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands 372c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandstypedef std::pair<Value*, APInt> RepeatedValue; 373c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands 3740fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// LinearizeExprTree - Given an associative binary expression, return the leaf 375c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// nodes in Ops along with their weights (how many times the leaf occurs). The 376c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// original expression is the same as 377c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// (Ops[0].first op Ops[0].first op ... Ops[0].first) <- Ops[0].second times 378c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// op 379c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// (Ops[1].first op Ops[1].first op ... Ops[1].first) <- Ops[1].second times 380c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// op 381c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// ... 382c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// op 383c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// (Ops[N].first op Ops[N].first op ... Ops[N].first) <- Ops[N].second times 384c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// 385c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// Note that the values Ops[0].first, ..., Ops[N].first are all distinct, and 386c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// they are all non-constant except possibly for the last one, which if it is 387c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// constant will have weight one (Ops[N].second === 1). 388c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// 389c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// This routine may modify the function, in which case it returns 'true'. The 390c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// changes it makes may well be destructive, changing the value computed by 'I' 391c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// to something completely different. Thus if the routine returns 'true' then 392c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// you MUST either replace I with a new expression computed from the Ops array, 393c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// or use RewriteExprTree to put the values back in. 3940fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// 3950fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// A leaf node is either not a binary operation of the same kind as the root 3960fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// node 'I' (i.e. is not a binary operator at all, or is, but with a different 3970fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// opcode), or is the same kind of binary operator but has a use which either 3980fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// does not belong to the expression, or does belong to the expression but is 3990fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// a leaf node. Every leaf node has at least one use that is a non-leaf node 4000fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// of the expression, while for non-leaf nodes (except for the root 'I') every 4010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// use is a non-leaf node of the expression. 4020fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// 4030fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// For example: 4040fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// expression graph node names 4050fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// 4060fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// + | I 4070fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// / \ | 4080fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// + + | A, B 4090fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// / \ / \ | 4100fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// * + * | C, D, E 4110fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// / \ / \ / \ | 4120fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// + * | F, G 4130fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// 4140fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// The leaf nodes are C, E, F and G. The Ops array will contain (maybe not in 415c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// that order) (C, 1), (E, 1), (F, 2), (G, 2). 4160fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// 4170fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// The expression is maximal: if some instruction is a binary operator of the 4180fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// same kind as 'I', and all of its uses are non-leaf nodes of the expression, 4190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// then the instruction also belongs to the expression, is not a leaf node of 4200fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// it, and its operands also belong to the expression (but may be leaf nodes). 421c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner/// 4220fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// NOTE: This routine will set operands of non-leaf non-root nodes to undef in 4230fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// order to ensure that every non-root node in the expression has *exactly one* 4240fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// use by a non-leaf node of the expression. This destruction means that the 425eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands/// caller MUST either replace 'I' with a new expression or use something like 426c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// RewriteExprTree to put the values back in if the routine indicates that it 427c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands/// made a change by returning 'true'. 428e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner/// 4290fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// In the above example either the right operand of A or the left operand of B 4300fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// will be replaced by undef. If it is B's operand then this gives: 4310fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// 4320fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// + | I 4330fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// / \ | 4340fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// + + | A, B - operand of B replaced with undef 4350fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// / \ \ | 4360fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// * + * | C, D, E 4370fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// / \ / \ / \ | 4380fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// + * | F, G 4390fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// 440eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands/// Note that such undef operands can only be reached by passing through 'I'. 441eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands/// For example, if you visit operands recursively starting from a leaf node 442eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands/// then you will never see such an undef operand unless you get back to 'I', 4430fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// which requires passing through a phi node. 4440fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// 4450fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// Note that this routine may also mutate binary operators of the wrong type 4460fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// that have all uses inside the expression (i.e. only used by non-leaf nodes 4470fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// of the expression) if it can turn them into binary operators of the right 4480fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands/// type and thus make the expression bigger. 4490fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 450c038a7833565ecf92a699371d448135a097c9e2fDuncan Sandsstatic bool LinearizeExprTree(BinaryOperator *I, 451c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands SmallVectorImpl<RepeatedValue> &Ops) { 4520fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "LINEARIZE: " << *I << '\n'); 453c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands unsigned Bitwidth = I->getType()->getScalarType()->getPrimitiveSizeInBits(); 454c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands unsigned Opcode = I->getOpcode(); 455c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands assert(Instruction::isAssociative(Opcode) && 456c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Instruction::isCommutative(Opcode) && 457c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands "Expected an associative and commutative operation!"); 458ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands // If we see an absorbing element then the entire expression must be equal to 459ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands // it. For example, if this is a multiplication expression and zero occurs as 460ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands // an operand somewhere in it then the result of the expression must be zero. 461ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, I->getType()); 4620fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 4630fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Visit all operands of the expression, keeping track of their weight (the 4640fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // number of paths from the expression root to the operand, or if you like 4650fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // the number of times that operand occurs in the linearized expression). 4660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // For example, if I = X + A, where X = A + B, then I, X and B have weight 1 4670fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // while A has weight two. 4680fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 4690fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Worklist of non-leaf nodes (their operands are in the expression too) along 4700fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // with their weights, representing a certain number of paths to the operator. 4710fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // If an operator occurs in the worklist multiple times then we found multiple 4720fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // ways to get to it. 473c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands SmallVector<std::pair<BinaryOperator*, APInt>, 8> Worklist; // (Op, Weight) 474c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Worklist.push_back(std::make_pair(I, APInt(Bitwidth, 1))); 475c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands bool MadeChange = false; 476c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner 4770fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Leaves of the expression are values that either aren't the right kind of 4780fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // operation (eg: a constant, or a multiply in an add tree), or are, but have 4790fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // some uses that are not inside the expression. For example, in I = X + X, 4800fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // X = A + B, the value X has two uses (by I) that are in the expression. If 4810fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // X has any other uses, for example in a return instruction, then we consider 4820fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // X to be a leaf, and won't analyze it further. When we first visit a value, 4830fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // if it has more than one use then at first we conservatively consider it to 4840fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // be a leaf. Later, as the expression is explored, we may discover some more 4850fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // uses of the value from inside the expression. If all uses turn out to be 4860fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // from within the expression (and the value is a binary operator of the right 4870fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // kind) then the value is no longer considered to be a leaf, and its operands 4880fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // are explored. 4890fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 4900fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Leaves - Keeps track of the set of putative leaves as well as the number of 4910fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // paths to each leaf seen so far. 4925f9e4c1189ab4a8ea1b0000d9337060ac3cac26eDuncan Sands typedef DenseMap<Value*, APInt> LeafMap; 4930fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands LeafMap Leaves; // Leaf -> Total weight so far. 4940fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands SmallVector<Value*, 8> LeafOrder; // Ensure deterministic leaf output order. 495f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner 4960fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands#ifndef NDEBUG 4970fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands SmallPtrSet<Value*, 8> Visited; // For sanity checking the iteration scheme. 4980fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands#endif 4990fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands while (!Worklist.empty()) { 500c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands std::pair<BinaryOperator*, APInt> P = Worklist.pop_back_val(); 5010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands I = P.first; // We examine the operands of this binary operator. 5020fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 5030fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands for (unsigned OpIdx = 0; OpIdx < 2; ++OpIdx) { // Visit operands. 5040fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Value *Op = I->getOperand(OpIdx); 505c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands APInt Weight = P.second; // Number of paths to this operand. 5060fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "OPERAND: " << *Op << " (" << Weight << ")\n"); 5070fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands assert(!Op->use_empty() && "No uses, so how did we get to it?!"); 5080fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 509ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands // If the expression contains an absorbing element then there is no need 510ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands // to analyze it further: it must evaluate to the absorbing element. 511ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands if (Op == Absorber && !Weight.isMinValue()) { 512ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands Ops.push_back(std::make_pair(Absorber, APInt(Bitwidth, 1))); 513ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands return MadeChange; 514ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands } 515ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands 5160fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // If this is a binary operation of the right kind with only one use then 5170fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // add its operands to the expression. 5180fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (BinaryOperator *BO = isReassociableOp(Op, Opcode)) { 5190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands assert(Visited.insert(Op) && "Not first visit!"); 5200fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "DIRECT ADD: " << *Op << " (" << Weight << ")\n"); 5210fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Worklist.push_back(std::make_pair(BO, Weight)); 5220fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands continue; 5230fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 524e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 5250fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Appears to be a leaf. Is the operand already in the set of leaves? 5260fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands LeafMap::iterator It = Leaves.find(Op); 5270fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (It == Leaves.end()) { 5280fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Not in the leaf map. Must be the first time we saw this operand. 5290fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands assert(Visited.insert(Op) && "Not first visit!"); 5300fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (!Op->hasOneUse()) { 5310fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // This value has uses not accounted for by the expression, so it is 5320fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // not safe to modify. Mark it as being a leaf. 5330fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "ADD USES LEAF: " << *Op << " (" << Weight << ")\n"); 5340fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands LeafOrder.push_back(Op); 5350fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Leaves[Op] = Weight; 5360fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands continue; 5370fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 5380fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // No uses outside the expression, try morphing it. 5390fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } else if (It != Leaves.end()) { 5400fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Already in the leaf map. 5410fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands assert(Visited.count(Op) && "In leaf map but not visited!"); 5420fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 5430fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Update the number of paths to the leaf. 544c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands IncorporateWeight(It->second, Weight, Opcode); 5450fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 5460fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // The leaf already has one use from inside the expression. As we want 5470fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // exactly one such use, drop this new use of the leaf. 5480fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands assert(!Op->hasOneUse() && "Only one use, but we got here twice!"); 5490fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands I->setOperand(OpIdx, UndefValue::get(I->getType())); 5500fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands MadeChange = true; 551e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 5520fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // If the leaf is a binary operation of the right kind and we now see 5530fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // that its multiple original uses were in fact all by nodes belonging 5540fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // to the expression, then no longer consider it to be a leaf and add 5550fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // its operands to the expression. 5560fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (BinaryOperator *BO = isReassociableOp(Op, Opcode)) { 5570fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "UNLEAF: " << *Op << " (" << It->second << ")\n"); 5580fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Worklist.push_back(std::make_pair(BO, It->second)); 5590fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Leaves.erase(It); 5600fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands continue; 5610fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 562fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 5630fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // If we still have uses that are not accounted for by the expression 5640fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // then it is not safe to modify the value. 5650fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (!Op->hasOneUse()) 5660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands continue; 5674fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner 5680fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // No uses outside the expression, try morphing it. 5690fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Weight = It->second; 5700fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Leaves.erase(It); // Since the value may be morphed below. 5710fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 572c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner 5730fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // At this point we have a value which, first of all, is not a binary 5740fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // expression of the right kind, and secondly, is only used inside the 5750fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // expression. This means that it can safely be modified. See if we 5760fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // can usefully morph it into an expression of the right kind. 5770fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands assert((!isa<Instruction>(Op) || 5780fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands cast<Instruction>(Op)->getOpcode() != Opcode) && 5790fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands "Should have been handled above!"); 5800fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands assert(Op->hasOneUse() && "Has uses outside the expression tree!"); 5810fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 5820fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // If this is a multiply expression, turn any internal negations into 5830fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // multiplies by -1 so they can be reassociated. 5840fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands BinaryOperator *BO = dyn_cast<BinaryOperator>(Op); 5850fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (Opcode == Instruction::Mul && BO && BinaryOperator::isNeg(BO)) { 5860fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "MORPH LEAF: " << *Op << " (" << Weight << ") TO "); 587841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands BO = LowerNegateToMultiply(BO); 5880fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << *BO << 'n'); 5890fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Worklist.push_back(std::make_pair(BO, Weight)); 5900fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands MadeChange = true; 5910fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands continue; 5920fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 593c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner 5940fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Failed to morph into an expression of the right type. This really is 5950fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // a leaf. 5960fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "ADD LEAF: " << *Op << " (" << Weight << ")\n"); 5970fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands assert(!isReassociableOp(Op, Opcode) && "Value was morphed?"); 5980fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands LeafOrder.push_back(Op); 5990fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Leaves[Op] = Weight; 6000fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 6010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 602e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 6030fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // The leaves, repeated according to their weights, represent the linearized 6040fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // form of the expression. 605c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Constant *Cst = 0; // Accumulate constants here. 6060fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands for (unsigned i = 0, e = LeafOrder.size(); i != e; ++i) { 6070fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Value *V = LeafOrder[i]; 6080fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands LeafMap::iterator It = Leaves.find(V); 6090fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (It == Leaves.end()) 610c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // Node initially thought to be a leaf wasn't. 6110fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands continue; 6120fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands assert(!isReassociableOp(V, Opcode) && "Shouldn't be a leaf!"); 613c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands APInt Weight = It->second; 614c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands if (Weight.isMinValue()) 615c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // Leaf already output or weight reduction eliminated it. 616c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands continue; 6170fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Ensure the leaf is only output once. 618c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands It->second = 0; 619c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // Glob all constants together into Cst. 620c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands if (Constant *C = dyn_cast<Constant>(V)) { 621c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands C = EvaluateRepeatedConstant(Opcode, C, Weight); 622c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Cst = Cst ? ConstantExpr::get(Opcode, Cst, C) : C; 623c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands continue; 624c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands } 625c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // Add non-constant 626c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Ops.push_back(std::make_pair(V, Weight)); 627c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands } 628c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands 629c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // Add any constants back into Ops, all globbed together and reduced to having 630c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // weight 1 for the convenience of users. 631ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands Constant *Identity = ConstantExpr::getBinOpIdentity(Opcode, I->getType()); 632d34491f6751ae2f8daf3e857c84bcb5b06fba889Duncan Sands if (Cst && Cst != Identity) { 633d34491f6751ae2f8daf3e857c84bcb5b06fba889Duncan Sands // If combining multiple constants resulted in the absorber then the entire 634d34491f6751ae2f8daf3e857c84bcb5b06fba889Duncan Sands // expression must evaluate to the absorber. 635d34491f6751ae2f8daf3e857c84bcb5b06fba889Duncan Sands if (Cst == Absorber) 636d34491f6751ae2f8daf3e857c84bcb5b06fba889Duncan Sands Ops.clear(); 637c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Ops.push_back(std::make_pair(Cst, APInt(Bitwidth, 1))); 638d34491f6751ae2f8daf3e857c84bcb5b06fba889Duncan Sands } 639c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands 640c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // For nilpotent operations or addition there may be no operands, for example 641c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // because the expression was "X xor X" or consisted of 2^Bitwidth additions: 642c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands // in both cases the weight reduces to 0 causing the value to be skipped. 643c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands if (Ops.empty()) { 644ee5a094ccf1f04d3fcc92ac4d2fc8a2926cbb232Duncan Sands assert(Identity && "Associative operation without identity!"); 645c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Ops.push_back(std::make_pair(Identity, APInt(Bitwidth, 1))); 6460fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 647c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands 648c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands return MadeChange; 649c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner} 650c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner 651c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner// RewriteExprTree - Now that the operands for this expression tree are 6520fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands// linearized and optimized, emit them in-order. 653e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattnervoid Reassociate::RewriteExprTree(BinaryOperator *I, 6540fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands SmallVectorImpl<ValueEntry> &Ops) { 6550fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands assert(Ops.size() > 1 && "Single values should be used directly!"); 6560fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 6570fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Since our optimizations never increase the number of operations, the new 6580fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // expression can always be written by reusing the existing binary operators 6590fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // from the original expression tree, without creating any new instructions, 6600fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // though the rewritten expression may have a completely different topology. 6610fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // We take care to not change anything if the new expression will be the same 6620fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // as the original. If more than trivial changes (like commuting operands) 6630fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // were made then we are obliged to clear out any optional subclass data like 6640fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // nsw flags. 6650fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 6660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands /// NodesToRewrite - Nodes from the original expression available for writing 6670fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands /// the new expression into. 6680fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands SmallVector<BinaryOperator*, 8> NodesToRewrite; 6690fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands unsigned Opcode = I->getOpcode(); 6700fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands NodesToRewrite.push_back(I); 6710fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 672eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands // ExpressionChanged - Non-null if the rewritten expression differs from the 673eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands // original in some non-trivial way, requiring the clearing of optional flags. 674eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands // Flags are cleared from the operator in ExpressionChanged up to I inclusive. 675eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands BinaryOperator *ExpressionChanged = 0; 6760fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands BinaryOperator *Previous; 6770fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands BinaryOperator *Op = 0; 6780fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands for (unsigned i = 0, e = Ops.size(); i != e; ++i) { 6790fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands assert(!NodesToRewrite.empty() && 6800fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands "Optimized expressions has more nodes than original!"); 6810fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Previous = Op; Op = NodesToRewrite.pop_back_val(); 682eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands if (ExpressionChanged) 683eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands // Compactify the tree instructions together with each other to guarantee 684eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands // that the expression tree is dominated by all of Ops. 6850fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Op->moveBefore(Previous); 6860fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 6870fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // The last operation (which comes earliest in the IR) is special as both 6880fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // operands will come from Ops, rather than just one with the other being 6890fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // a subexpression. 6900fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (i+2 == Ops.size()) { 6910fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Value *NewLHS = Ops[i].Op; 6920fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Value *NewRHS = Ops[i+1].Op; 6930fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Value *OldLHS = Op->getOperand(0); 6940fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Value *OldRHS = Op->getOperand(1); 6950fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 6960fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (NewLHS == OldLHS && NewRHS == OldRHS) 6970fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Nothing changed, leave it alone. 6980fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands break; 6990fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 7000fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (NewLHS == OldRHS && NewRHS == OldLHS) { 7010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // The order of the operands was reversed. Swap them. 7020fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "RA: " << *Op << '\n'); 7030fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Op->swapOperands(); 7040fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "TO: " << *Op << '\n'); 7050fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands MadeChange = true; 7060fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands ++NumChanged; 7070fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands break; 7080fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 7090fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 7100fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // The new operation differs non-trivially from the original. Overwrite 7110fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // the old operands with the new ones. 7120fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "RA: " << *Op << '\n'); 7130fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (NewLHS != OldLHS) { 7140fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (BinaryOperator *BO = isReassociableOp(OldLHS, Opcode)) 7150fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands NodesToRewrite.push_back(BO); 7160fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Op->setOperand(0, NewLHS); 7170fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 7180fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (NewRHS != OldRHS) { 7190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (BinaryOperator *BO = isReassociableOp(OldRHS, Opcode)) 7200fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands NodesToRewrite.push_back(BO); 7210fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Op->setOperand(1, NewRHS); 7220fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 7230fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "TO: " << *Op << '\n'); 7240fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 725eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands ExpressionChanged = Op; 726c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner MadeChange = true; 727c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner ++NumChanged; 728e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 7290fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands break; 730c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner } 731c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner 7320fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Not the last operation. The left-hand side will be a sub-expression 7330fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // while the right-hand side will be the current element of Ops. 7340fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Value *NewRHS = Ops[i].Op; 7350fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (NewRHS != Op->getOperand(1)) { 7360fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "RA: " << *Op << '\n'); 7370fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (NewRHS == Op->getOperand(0)) { 7380fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // The new right-hand side was already present as the left operand. If 7390fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // we are lucky then swapping the operands will sort out both of them. 7400fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Op->swapOperands(); 7410fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } else { 7420fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Overwrite with the new right-hand side. 7430fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (BinaryOperator *BO = isReassociableOp(Op->getOperand(1), Opcode)) 7440fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands NodesToRewrite.push_back(BO); 7450fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Op->setOperand(1, NewRHS); 746eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands ExpressionChanged = Op; 7470fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 7480fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "TO: " << *Op << '\n'); 7490fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands MadeChange = true; 7500fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands ++NumChanged; 7510fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 75246985a14409486293b689ca07dd07d7482734795Dan Gohman 7530fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Now deal with the left-hand side. If this is already an operation node 7540fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // from the original expression then just rewrite the rest of the expression 7550fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // into it. 7560fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (BinaryOperator *BO = isReassociableOp(Op->getOperand(0), Opcode)) { 7570fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands NodesToRewrite.push_back(BO); 7580fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands continue; 7590fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 76046985a14409486293b689ca07dd07d7482734795Dan Gohman 7610fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Otherwise, grab a spare node from the original expression and use that as 7620fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // the left-hand side. 7630fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands assert(!NodesToRewrite.empty() && 7640fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands "Optimized expressions has more nodes than original!"); 7650fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "RA: " << *Op << '\n'); 7660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Op->setOperand(0, NodesToRewrite.back()); 7670fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands DEBUG(dbgs() << "TO: " << *Op << '\n'); 768eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands ExpressionChanged = Op; 769c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner MadeChange = true; 770c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner ++NumChanged; 771c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner } 772e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 773eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands // If the expression changed non-trivially then clear out all subclass data 774eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands // starting from the operator specified in ExpressionChanged. 7750fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (ExpressionChanged) { 7760fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands do { 777eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands ExpressionChanged->clearSubclassOptionalData(); 778eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands if (ExpressionChanged == I) 7790fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands break; 780eacc31acf515c79338e8c94ce8c7c26dd7b2d32aDuncan Sands ExpressionChanged = cast<BinaryOperator>(*ExpressionChanged->use_begin()); 7810fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } while (1); 7820fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 783e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 7840fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Throw away any left over nodes from the original expression. 7850fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands for (unsigned i = 0, e = NodesToRewrite.size(); i != e; ++i) 786841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands RedoInsts.insert(NodesToRewrite[i]); 7874fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner} 7884fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner 789e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// NegateValue - Insert instructions before the instruction pointed to by BI, 790e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// that computes the negative version of the value specified. The negative 791e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// version of the value is returned, and BI is left pointing at the instruction 792e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// that should be processed next by the reassociation pass. 793e79fddedcae1ee8fe7d8571db58447bc722f75dcNick Lewyckystatic Value *NegateValue(Value *V, Instruction *BI) { 79435239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner if (Constant *C = dyn_cast<Constant>(V)) 79535239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner return ConstantExpr::getNeg(C); 796e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 797a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner // We are trying to expose opportunity for reassociation. One of the things 798a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner // that we want to do to achieve this is to push a negation as deep into an 799a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner // expression chain as possible, to expose the add instructions. In practice, 800a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner // this means that we turn this: 801a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner // X = -(A+12+C+D) into X = -A + -12 + -C + -D = -12 + -A + -C + -D 802a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner // so that later, a: Y = 12+X could get reassociated with the -12 to eliminate 803a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner // the constants. We assume that instcombine will clean up the mess later if 8049046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner // we introduce tons of unnecessary negation instructions. 805a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner // 8060fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (BinaryOperator *I = isReassociableOp(V, Instruction::Add)) { 8070fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Push the negates through the add. 8080fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands I->setOperand(0, NegateValue(I->getOperand(0), BI)); 8090fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands I->setOperand(1, NegateValue(I->getOperand(1), BI)); 8100fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 8110fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // We must move the add instruction here, because the neg instructions do 8120fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // not dominate the old add instruction in general. By moving it, we are 8130fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // assured that the neg instructions we just inserted dominate the 8140fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // instruction we are about to insert after them. 8150fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // 8160fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands I->moveBefore(BI); 8170fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands I->setName(I->getName()+".neg"); 8180fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands return I; 8190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands } 820e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 82135239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner // Okay, we need to materialize a negated version of V with an instruction. 82235239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner // Scan the use lists of V to see if we have one already. 82335239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){ 824110b75aa7572d3b59b308da7ec1d759e86788f97Gabor Greif User *U = *UI; 825110b75aa7572d3b59b308da7ec1d759e86788f97Gabor Greif if (!BinaryOperator::isNeg(U)) continue; 82635239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner 82735239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner // We found one! Now we have to make sure that the definition dominates 82835239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner // this use. We do this by moving it to the entry block (if it is a 82935239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner // non-instruction value) or right after the definition. These negates will 83035239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner // be zapped by reassociate later, so we don't need much finesse here. 831110b75aa7572d3b59b308da7ec1d759e86788f97Gabor Greif BinaryOperator *TheNeg = cast<BinaryOperator>(U); 8321c91fae649734abe6f8271862fe3ba917e191279Chris Lattner 8331c91fae649734abe6f8271862fe3ba917e191279Chris Lattner // Verify that the negate is in this function, V might be a constant expr. 8341c91fae649734abe6f8271862fe3ba917e191279Chris Lattner if (TheNeg->getParent()->getParent() != BI->getParent()->getParent()) 8351c91fae649734abe6f8271862fe3ba917e191279Chris Lattner continue; 836e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 83735239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner BasicBlock::iterator InsertPt; 83835239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner if (Instruction *InstInput = dyn_cast<Instruction>(V)) { 83935239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(InstInput)) { 84035239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner InsertPt = II->getNormalDest()->begin(); 84135239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner } else { 84235239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner InsertPt = InstInput; 84335239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner ++InsertPt; 84435239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner } 84535239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner while (isa<PHINode>(InsertPt)) ++InsertPt; 84635239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner } else { 84735239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner InsertPt = TheNeg->getParent()->getParent()->getEntryBlock().begin(); 84835239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner } 84935239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner TheNeg->moveBefore(InsertPt); 85035239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner return TheNeg; 85135239934517c6fcd52e3e965f40e03f74aa4d11dChris Lattner } 852a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner 853a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner // Insert a 'neg' instruction that subtracts the value from zero to get the 854a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner // negation. 8554ae5126d041768ab9665cf2f11c024becd76c41fDan Gohman return BinaryOperator::CreateNeg(V, V->getName() + ".neg", BI); 856a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner} 857a36e6c8cd58c2876decd2d0402064ac349bbec71Chris Lattner 8589bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner/// ShouldBreakUpSubtract - Return true if we should break up this subtract of 8599bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner/// X-Y into (X + -Y). 860e79fddedcae1ee8fe7d8571db58447bc722f75dcNick Lewyckystatic bool ShouldBreakUpSubtract(Instruction *Sub) { 8619bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner // If this is a negation, we can't split it up! 862fa82b6eba4e1584d7dba291c28fe908272e1e002Owen Anderson if (BinaryOperator::isNeg(Sub)) 8639bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner return false; 864e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 8659bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner // Don't bother to break this up unless either the LHS is an associable add or 8660b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner // subtract or if this is only used by one. 8670b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner if (isReassociableOp(Sub->getOperand(0), Instruction::Add) || 8680b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner isReassociableOp(Sub->getOperand(0), Instruction::Sub)) 8699bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner return true; 8700b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner if (isReassociableOp(Sub->getOperand(1), Instruction::Add) || 8715329bb22e9b6374d62919981c1ef8775b42945ebChris Lattner isReassociableOp(Sub->getOperand(1), Instruction::Sub)) 8729bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner return true; 873e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling if (Sub->hasOneUse() && 8740b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner (isReassociableOp(Sub->use_back(), Instruction::Add) || 8750b0803ae1508ff514dd7b471a2a3bcd1e83cb0efChris Lattner isReassociableOp(Sub->use_back(), Instruction::Sub))) 8769bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner return true; 877e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 8789bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner return false; 8799bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner} 8809bc5ed78c860694ccb4ea63c96c2c9212a8b245bChris Lattner 88108b43921e18f314c4fd38049291d323830934c36Chris Lattner/// BreakUpSubtract - If we have (X-Y), and if either X is an add, or if this is 88208b43921e18f314c4fd38049291d323830934c36Chris Lattner/// only used by an add, transform this into (X+(0-Y)) to promote better 88308b43921e18f314c4fd38049291d323830934c36Chris Lattner/// reassociation. 884841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsstatic BinaryOperator *BreakUpSubtract(Instruction *Sub) { 8859046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner // Convert a subtract into an add and a neg instruction. This allows sub 8869046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner // instructions to be commuted with other add instructions. 88708b43921e18f314c4fd38049291d323830934c36Chris Lattner // 8889046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner // Calculate the negative value of Operand 1 of the sub instruction, 8899046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner // and set it as the RHS of the add instruction we just made. 89008b43921e18f314c4fd38049291d323830934c36Chris Lattner // 891e79fddedcae1ee8fe7d8571db58447bc722f75dcNick Lewycky Value *NegVal = NegateValue(Sub->getOperand(1), Sub); 892841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands BinaryOperator *New = 8937cbd8a3e92221437048b484d5ef9c0a22d0f8c58Gabor Greif BinaryOperator::CreateAdd(Sub->getOperand(0), NegVal, "", Sub); 894841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Sub->setOperand(0, Constant::getNullValue(Sub->getType())); // Drop use of op. 895841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Sub->setOperand(1, Constant::getNullValue(Sub->getType())); // Drop use of op. 8966934a04a8c15e9971cd1ea4d5c8df2d7afdd5be5Chris Lattner New->takeName(Sub); 89708b43921e18f314c4fd38049291d323830934c36Chris Lattner 89808b43921e18f314c4fd38049291d323830934c36Chris Lattner // Everyone now refers to the add instruction. 89908b43921e18f314c4fd38049291d323830934c36Chris Lattner Sub->replaceAllUsesWith(New); 9005367b23f76e75ebb680956575346fa8c3d56780fDevang Patel New->setDebugLoc(Sub->getDebugLoc()); 90100b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen 902a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene DEBUG(dbgs() << "Negated: " << *New << '\n'); 90308b43921e18f314c4fd38049291d323830934c36Chris Lattner return New; 90408b43921e18f314c4fd38049291d323830934c36Chris Lattner} 90508b43921e18f314c4fd38049291d323830934c36Chris Lattner 9060975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner/// ConvertShiftToMul - If this is a shift of a reassociable multiply or is used 9070975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner/// by one, change this into a multiply by a constant to assist with further 9080975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner/// reassociation. 909841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsstatic BinaryOperator *ConvertShiftToMul(Instruction *Shl) { 910841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Constant *MulCst = ConstantInt::get(Shl->getType(), 1); 911841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands MulCst = ConstantExpr::getShl(MulCst, cast<Constant>(Shl->getOperand(1))); 912841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands 913841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands BinaryOperator *Mul = 914841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands BinaryOperator::CreateMul(Shl->getOperand(0), MulCst, "", Shl); 915841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Shl->setOperand(0, UndefValue::get(Shl->getType())); // Drop use of op. 916841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Mul->takeName(Shl); 917841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Shl->replaceAllUsesWith(Mul); 918841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Mul->setDebugLoc(Shl->getDebugLoc()); 919841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands return Mul; 9200975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner} 9210975ed5f4ef7264b45995241717055f8a116bb27Chris Lattner 922e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// FindInOperandList - Scan backwards and forwards among values with the same 923e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// rank as element i to see if X exists. If X does not exist, return i. This 924e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// is useful when scanning for 'x' when we see '-x' because they both get the 925e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// same rank. 9269f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattnerstatic unsigned FindInOperandList(SmallVectorImpl<ValueEntry> &Ops, unsigned i, 927109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner Value *X) { 928109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner unsigned XRank = Ops[i].Rank; 929109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner unsigned e = Ops.size(); 930109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner for (unsigned j = i+1; j != e && Ops[j].Rank == XRank; ++j) 931109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner if (Ops[j].Op == X) 932109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner return j; 9339506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner // Scan backwards. 934109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner for (unsigned j = i-1; j != ~0U && Ops[j].Rank == XRank; --j) 935109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner if (Ops[j].Op == X) 936109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner return j; 937109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner return i; 938109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner} 939109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner 940e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// EmitAddTreeOfValues - Emit a tree of add instructions, summing Ops together 941e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// and returning the result. Insert the tree before I. 94255e7098bbc363473c01229517097d2a04e04e9b0Bill Wendlingstatic Value *EmitAddTreeOfValues(Instruction *I, 94355e7098bbc363473c01229517097d2a04e04e9b0Bill Wendling SmallVectorImpl<WeakVH> &Ops){ 944e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner if (Ops.size() == 1) return Ops.back(); 945e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 946e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner Value *V1 = Ops.back(); 947e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner Ops.pop_back(); 948e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner Value *V2 = EmitAddTreeOfValues(I, Ops); 9497cbd8a3e92221437048b484d5ef9c0a22d0f8c58Gabor Greif return BinaryOperator::CreateAdd(V2, V1, "tmp", I); 950e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner} 951e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner 952e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling/// RemoveFactorFromExpression - If V is an expression tree that is a 953e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// multiplication sequence, and if this sequence contains a multiply by Factor, 954e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner/// remove Factor from the tree and return the new tree. 955e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris LattnerValue *Reassociate::RemoveFactorFromExpression(Value *V, Value *Factor) { 956e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner BinaryOperator *BO = isReassociableOp(V, Instruction::Mul); 957e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner if (!BO) return 0; 958e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 959c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands SmallVector<RepeatedValue, 8> Tree; 960c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands MadeChange |= LinearizeExprTree(BO, Tree); 9619f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner SmallVector<ValueEntry, 8> Factors; 962c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Factors.reserve(Tree.size()); 963c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands for (unsigned i = 0, e = Tree.size(); i != e; ++i) { 964c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands RepeatedValue E = Tree[i]; 965c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Factors.append(E.second.getZExtValue(), 966c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands ValueEntry(getRank(E.first), E.first)); 967c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands } 968e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner 969e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner bool FoundFactor = false; 9709506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner bool NeedsNegate = false; 9719506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner for (unsigned i = 0, e = Factors.size(); i != e; ++i) { 972e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner if (Factors[i].Op == Factor) { 973e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner FoundFactor = true; 974e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner Factors.erase(Factors.begin()+i); 975e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner break; 976e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner } 977e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 9789506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner // If this is a negative version of this factor, remove it. 9799506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner if (ConstantInt *FC1 = dyn_cast<ConstantInt>(Factor)) 9809506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner if (ConstantInt *FC2 = dyn_cast<ConstantInt>(Factors[i].Op)) 9819506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner if (FC1->getValue() == -FC2->getValue()) { 9829506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner FoundFactor = NeedsNegate = true; 9839506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner Factors.erase(Factors.begin()+i); 9849506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner break; 9859506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner } 9869506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner } 987e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 988e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner if (!FoundFactor) { 989e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner // Make sure to restore the operands to the expression tree. 990e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner RewriteExprTree(BO, Factors); 991e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner return 0; 992e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner } 993e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 9949506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner BasicBlock::iterator InsertPt = BO; ++InsertPt; 995e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 9961e7558b65689999089f53ce40ff07564cf498c68Chris Lattner // If this was just a single multiply, remove the multiply and return the only 9971e7558b65689999089f53ce40ff07564cf498c68Chris Lattner // remaining operand. 9981e7558b65689999089f53ce40ff07564cf498c68Chris Lattner if (Factors.size() == 1) { 999841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands RedoInsts.insert(BO); 10009506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner V = Factors[0].Op; 10019506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner } else { 10029506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner RewriteExprTree(BO, Factors); 10039506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner V = BO; 10041e7558b65689999089f53ce40ff07564cf498c68Chris Lattner } 1005e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 10069506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner if (NeedsNegate) 10079506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner V = BinaryOperator::CreateNeg(V, "neg", InsertPt); 1008e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 10099506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner return V; 1010e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner} 1011e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner 1012e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner/// FindSingleUseMultiplyFactors - If V is a single-use multiply, recursively 1013e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner/// add its operands as factors, otherwise add V to the list of factors. 1014893075f46e9d07e3fe94e2b0e0f3ff8ae4061549Chris Lattner/// 1015893075f46e9d07e3fe94e2b0e0f3ff8ae4061549Chris Lattner/// Ops is the top-level list of add operands we're trying to factor. 1016e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattnerstatic void FindSingleUseMultiplyFactors(Value *V, 1017893075f46e9d07e3fe94e2b0e0f3ff8ae4061549Chris Lattner SmallVectorImpl<Value*> &Factors, 10180fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands const SmallVectorImpl<ValueEntry> &Ops) { 10190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands BinaryOperator *BO = isReassociableOp(V, Instruction::Mul); 10200fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (!BO) { 1021e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner Factors.push_back(V); 1022e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner return; 1023e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner } 1024e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1025e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner // Otherwise, add the LHS and RHS to the list of factors. 10260fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands FindSingleUseMultiplyFactors(BO->getOperand(1), Factors, Ops); 10270fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands FindSingleUseMultiplyFactors(BO->getOperand(0), Factors, Ops); 1028e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner} 1029e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner 1030f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// OptimizeAndOrXor - Optimize a series of operands to an 'and', 'or', or 'xor' 1031f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// instruction. This optimizes based on identities. If it can be reduced to 1032f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// a single Value, it is returned, otherwise the Ops list is mutated as 1033f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// necessary. 10349f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattnerstatic Value *OptimizeAndOrXor(unsigned Opcode, 10359f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner SmallVectorImpl<ValueEntry> &Ops) { 1036f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner // Scan the operand lists looking for X and ~X pairs, along with X,X pairs. 1037f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner // If we find any, we can simplify the expression. X&~X == 0, X|~X == -1. 1038f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner for (unsigned i = 0, e = Ops.size(); i != e; ++i) { 1039f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner // First, check for X and ~X in the operand list. 1040f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner assert(i < Ops.size()); 1041f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner if (BinaryOperator::isNot(Ops[i].Op)) { // Cannot occur for ^. 1042f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner Value *X = BinaryOperator::getNotArgument(Ops[i].Op); 1043f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner unsigned FoundX = FindInOperandList(Ops, i, X); 1044f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner if (FoundX != i) { 10459fdaefad580194353f34b6d72669591f8f9d811aChris Lattner if (Opcode == Instruction::And) // ...&X&~X = 0 1046f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner return Constant::getNullValue(X->getType()); 1047e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 10489fdaefad580194353f34b6d72669591f8f9d811aChris Lattner if (Opcode == Instruction::Or) // ...|X|~X = -1 1049f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner return Constant::getAllOnesValue(X->getType()); 1050f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner } 1051f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner } 1052e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1053f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner // Next, check for duplicate pairs of values, which we assume are next to 1054f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner // each other, due to our sorting criteria. 1055f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner assert(i < Ops.size()); 1056f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner if (i+1 != Ops.size() && Ops[i+1].Op == Ops[i].Op) { 1057f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner if (Opcode == Instruction::And || Opcode == Instruction::Or) { 1058f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner // Drop duplicate values for And and Or. 1059f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner Ops.erase(Ops.begin()+i); 1060f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner --i; --e; 1061f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner ++NumAnnihil; 1062f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner continue; 1063f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner } 1064e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1065f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner // Drop pairs of values for Xor. 1066f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner assert(Opcode == Instruction::Xor); 1067f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner if (e == 2) 1068f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner return Constant::getNullValue(Ops[0].Op->getType()); 1069e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 10709046193e557d559f45dc50df5e20b1fccc90b2acChris Lattner // Y ^ X^X -> Y 1071f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner Ops.erase(Ops.begin()+i, Ops.begin()+i+2); 1072f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner i -= 1; e -= 2; 1073f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner ++NumAnnihil; 1074f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner } 1075f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner } 1076f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner return 0; 1077f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner} 1078e9efecbf470100696355f32ea8b6ab942183ac6cChris Lattner 1079f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// OptimizeAdd - Optimize a series of operands to an 'add' instruction. This 1080f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// optimizes based on identities. If it can be reduced to a single Value, it 1081f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner/// is returned, otherwise the Ops list is mutated as necessary. 10829f7b7089be854c323f8d9a4627d80e47adf496e6Chris LattnerValue *Reassociate::OptimizeAdd(Instruction *I, 10839f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner SmallVectorImpl<ValueEntry> &Ops) { 1084f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner // Scan the operand lists looking for X and -X pairs. If we find any, we 108569e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // can simplify the expression. X+-X == 0. While we're at it, scan for any 108669e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // duplicates. We want to canonicalize Y+Y+Y+Z -> 3*Y+Z. 10879506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner // 10889506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner // TODO: We could handle "X + ~X" -> "-1" if we wanted, since "-X = ~X+1". 10899506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner // 1090f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner for (unsigned i = 0, e = Ops.size(); i != e; ++i) { 109169e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner Value *TheOp = Ops[i].Op; 109269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // Check to see if we've seen this operand before. If so, we factor all 1093f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner // instances of the operand together. Due to our sorting criteria, we know 1094f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner // that these need to be next to each other in the vector. 1095f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner if (i+1 != Ops.size() && Ops[i+1].Op == TheOp) { 1096f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner // Rescan the list, remove all instances of this operand from the expr. 109769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner unsigned NumFound = 0; 1098f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner do { 1099f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner Ops.erase(Ops.begin()+i); 110069e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner ++NumFound; 1101f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner } while (i != Ops.size() && Ops[i].Op == TheOp); 1102e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1103f8a447de162a2896a8a044931fb63de713dbc6b9Chris Lattner DEBUG(errs() << "\nFACTORING [" << NumFound << "]: " << *TheOp << '\n'); 110469e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner ++NumFactor; 1105e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 110669e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // Insert a new multiply. 110769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner Value *Mul = ConstantInt::get(cast<IntegerType>(I->getType()), NumFound); 110869e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner Mul = BinaryOperator::CreateMul(TheOp, Mul, "factor", I); 1109e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 111069e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // Now that we have inserted a multiply, optimize it. This allows us to 111169e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // handle cases that require multiple factoring steps, such as this: 111269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // (X*2) + (X*2) + (X*2) -> (X*2)*3 -> X*6 1113841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands RedoInsts.insert(cast<Instruction>(Mul)); 1114e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 111569e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // If every add operand was a duplicate, return the multiply. 111669e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner if (Ops.empty()) 111769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner return Mul; 1118e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 111969e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // Otherwise, we had some input that didn't have the dupe, such as 112069e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // "A + A + B" -> "A*2 + B". Add the new multiply to the list of 112169e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // things being added by this operation. 112269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner Ops.insert(Ops.begin(), ValueEntry(getRank(Mul), Mul)); 1123e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1124f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner --i; 1125f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner e = Ops.size(); 1126f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner continue; 112769e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner } 1128e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1129f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner // Check for X and -X in the operand list. 113069e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner if (!BinaryOperator::isNeg(TheOp)) 1131f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner continue; 1132e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 113369e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner Value *X = BinaryOperator::getNegArgument(TheOp); 1134f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner unsigned FoundX = FindInOperandList(Ops, i, X); 1135f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner if (FoundX == i) 1136f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner continue; 1137e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1138f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner // Remove X and -X from the operand list. 11399fdaefad580194353f34b6d72669591f8f9d811aChris Lattner if (Ops.size() == 2) 1140f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner return Constant::getNullValue(X->getType()); 1141e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1142f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner Ops.erase(Ops.begin()+i); 1143f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner if (i < FoundX) 1144f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner --FoundX; 1145f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner else 1146f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner --i; // Need to back up an extra one. 1147f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner Ops.erase(Ops.begin()+FoundX); 1148f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner ++NumAnnihil; 1149f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner --i; // Revisit element. 1150f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner e -= 2; // Removed two elements. 1151f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner } 1152e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 115394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // Scan the operand list, checking to see if there are any common factors 115494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // between operands. Consider something like A*A+A*B*C+D. We would like to 115594285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // reassociate this to A*(A+B*C)+D, which reduces the number of multiplies. 115694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // To efficiently find this, we count the number of times a factor occurs 115794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // for any ADD operands that are MULs. 115894285e620b845e09b18939e8d6448e01e692f3ceChris Lattner DenseMap<Value*, unsigned> FactorOccurrences; 1159e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 116094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // Keep track of each multiply we see, to avoid triggering on (X*4)+(X*4) 116194285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // where they are actually the same multiply. 116294285e620b845e09b18939e8d6448e01e692f3ceChris Lattner unsigned MaxOcc = 0; 116394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner Value *MaxOccVal = 0; 116494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner for (unsigned i = 0, e = Ops.size(); i != e; ++i) { 11650fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands BinaryOperator *BOp = isReassociableOp(Ops[i].Op, Instruction::Mul); 11660fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (!BOp) 116794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner continue; 1168e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 116994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // Compute all of the factors of this added value. 117094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner SmallVector<Value*, 8> Factors; 11710fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands FindSingleUseMultiplyFactors(BOp, Factors, Ops); 117294285e620b845e09b18939e8d6448e01e692f3ceChris Lattner assert(Factors.size() > 1 && "Bad linearize!"); 1173e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 117494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // Add one to FactorOccurrences for each unique factor in this op. 11759506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner SmallPtrSet<Value*, 8> Duplicates; 11769506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner for (unsigned i = 0, e = Factors.size(); i != e; ++i) { 11779506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner Value *Factor = Factors[i]; 11789506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner if (!Duplicates.insert(Factor)) continue; 1179e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 11809506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner unsigned Occ = ++FactorOccurrences[Factor]; 11819506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factor; } 1182e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 11839506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner // If Factor is a negative constant, add the negated value as a factor 11849506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner // because we can percolate the negate out. Watch for minint, which 11859506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner // cannot be positivified. 11869506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner if (ConstantInt *CI = dyn_cast<ConstantInt>(Factor)) 1187c73b24db5f6226ed44ebc44ce1c25bb357206623Chris Lattner if (CI->isNegative() && !CI->isMinValue(true)) { 11889506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner Factor = ConstantInt::get(CI->getContext(), -CI->getValue()); 11899506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner assert(!Duplicates.count(Factor) && 11909506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner "Shouldn't have two constant factors, missed a canonicalize"); 1191e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 11929506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner unsigned Occ = ++FactorOccurrences[Factor]; 11939506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factor; } 11949506c930aa1f7c5fbf1e0e1e6bfae71f4a61ee15Chris Lattner } 119594285e620b845e09b18939e8d6448e01e692f3ceChris Lattner } 119694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner } 1197e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 119894285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // If any factor occurred more than one time, we can pull it out. 119994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner if (MaxOcc > 1) { 120069e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner DEBUG(errs() << "\nFACTORING [" << MaxOcc << "]: " << *MaxOccVal << '\n'); 120194285e620b845e09b18939e8d6448e01e692f3ceChris Lattner ++NumFactor; 120294285e620b845e09b18939e8d6448e01e692f3ceChris Lattner 120394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // Create a new instruction that uses the MaxOccVal twice. If we don't do 120494285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // this, we could otherwise run into situations where removing a factor 1205e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling // from an expression will drop a use of maxocc, and this can cause 120694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // RemoveFactorFromExpression on successive values to behave differently. 120794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner Instruction *DummyInst = BinaryOperator::CreateAdd(MaxOccVal, MaxOccVal); 120855e7098bbc363473c01229517097d2a04e04e9b0Bill Wendling SmallVector<WeakVH, 4> NewMulOps; 120937f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands for (unsigned i = 0; i != Ops.size(); ++i) { 1210c2d1b6949c5141d21827cc94daea6ae4b1a9c750Chris Lattner // Only try to remove factors from expressions we're allowed to. 12110fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands BinaryOperator *BOp = isReassociableOp(Ops[i].Op, Instruction::Mul); 12120fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (!BOp) 1213c2d1b6949c5141d21827cc94daea6ae4b1a9c750Chris Lattner continue; 1214e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 121594285e620b845e09b18939e8d6448e01e692f3ceChris Lattner if (Value *V = RemoveFactorFromExpression(Ops[i].Op, MaxOccVal)) { 121637f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands // The factorized operand may occur several times. Convert them all in 121737f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands // one fell swoop. 121837f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands for (unsigned j = Ops.size(); j != i;) { 121937f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands --j; 122037f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands if (Ops[j].Op == Ops[i].Op) { 122137f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands NewMulOps.push_back(V); 122237f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands Ops.erase(Ops.begin()+j); 122337f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands } 122437f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands } 122537f87c7aa914fba1362bb187ce5a386abfe94e39Duncan Sands --i; 122694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner } 122794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner } 1228e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 122994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // No need for extra uses anymore. 123094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner delete DummyInst; 123154a57045ebcf8e31b1542098d1cd2bda9a718725Duncan Sands 123294285e620b845e09b18939e8d6448e01e692f3ceChris Lattner unsigned NumAddedValues = NewMulOps.size(); 123394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner Value *V = EmitAddTreeOfValues(I, NewMulOps); 123454a57045ebcf8e31b1542098d1cd2bda9a718725Duncan Sands 123569e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // Now that we have inserted the add tree, optimize it. This allows us to 123669e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // handle cases that require multiple factoring steps, such as this: 123794285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // A*A*B + A*A*C --> A*(A*B+A*C) --> A*(A*(B+C)) 12389cd1bc4f8b3e98892a2b9856eccd2a2ec9afdf7fChris Lattner assert(NumAddedValues > 1 && "Each occurrence should contribute a value"); 123954a57045ebcf8e31b1542098d1cd2bda9a718725Duncan Sands (void)NumAddedValues; 1240841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (Instruction *VI = dyn_cast<Instruction>(V)) 1241841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands RedoInsts.insert(VI); 124269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner 124369e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // Create the multiply. 1244841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Instruction *V2 = BinaryOperator::CreateMul(V, MaxOccVal, "tmp", I); 124569e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner 1246f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner // Rerun associate on the multiply in case the inner expression turned into 1247f31e2e92a801c5053dc9b3b484cdec73ad89e567Chris Lattner // a multiply. We want to make sure that we keep things in canonical form. 1248841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands RedoInsts.insert(V2); 1249e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 125094285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // If every add operand included the factor (e.g. "A*B + A*C"), then the 125194285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // entire result expression is just the multiply "A*(B+C)". 125294285e620b845e09b18939e8d6448e01e692f3ceChris Lattner if (Ops.empty()) 125394285e620b845e09b18939e8d6448e01e692f3ceChris Lattner return V2; 1254e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 12559cd1bc4f8b3e98892a2b9856eccd2a2ec9afdf7fChris Lattner // Otherwise, we had some input that didn't have the factor, such as 125694285e620b845e09b18939e8d6448e01e692f3ceChris Lattner // "A*B + A*C + D" -> "A*(B+C) + D". Add the new multiply to the list of 12579cd1bc4f8b3e98892a2b9856eccd2a2ec9afdf7fChris Lattner // things being added by this operation. 125894285e620b845e09b18939e8d6448e01e692f3ceChris Lattner Ops.insert(Ops.begin(), ValueEntry(getRank(V2), V2)); 125994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner } 1260e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1261f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner return 0; 1262f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner} 1263e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner 1264464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruthnamespace { 1265464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth /// \brief Predicate tests whether a ValueEntry's op is in a map. 1266464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth struct IsValueInMap { 1267464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth const DenseMap<Value *, unsigned> ⤅ 1268464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1269464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth IsValueInMap(const DenseMap<Value *, unsigned> &Map) : Map(Map) {} 1270464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1271464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth bool operator()(const ValueEntry &Entry) { 1272464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return Map.find(Entry.Op) != Map.end(); 1273464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth } 1274464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth }; 1275464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth} 1276464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1277464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// \brief Build up a vector of value/power pairs factoring a product. 1278464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// 1279464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// Given a series of multiplication operands, build a vector of factors and 1280464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// the powers each is raised to when forming the final product. Sort them in 1281464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// the order of descending power. 1282464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// 1283464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// (x*x) -> [(x, 2)] 1284464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// ((x*x)*x) -> [(x, 3)] 1285464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// ((((x*y)*x)*y)*x) -> [(x, 3), (y, 2)] 1286464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// 1287464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// \returns Whether any factors have a power greater than one. 1288464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruthbool Reassociate::collectMultiplyFactors(SmallVectorImpl<ValueEntry> &Ops, 1289464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth SmallVectorImpl<Factor> &Factors) { 12900fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // FIXME: Have Ops be (ValueEntry, Multiplicity) pairs, simplifying this. 12910fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Compute the sum of powers of simplifiable factors. 1292464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth unsigned FactorPowerSum = 0; 12930fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands for (unsigned Idx = 1, Size = Ops.size(); Idx < Size; ++Idx) { 12940fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Value *Op = Ops[Idx-1].Op; 12950fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 12960fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Count the number of occurrences of this value. 12970fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands unsigned Count = 1; 12980fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands for (; Idx < Size && Ops[Idx].Op == Op; ++Idx) 12990fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands ++Count; 1300464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // Track for simplification all factors which occur 2 or more times. 13010fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (Count > 1) 13020fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands FactorPowerSum += Count; 1303464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth } 13040fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 1305464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // We can only simplify factors if the sum of the powers of our simplifiable 1306464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // factors is 4 or higher. When that is the case, we will *always* have 1307464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // a simplification. This is an important invariant to prevent cyclicly 1308464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // trying to simplify already minimal formations. 1309464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth if (FactorPowerSum < 4) 1310464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return false; 1311464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 13120fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Now gather the simplifiable factors, removing them from Ops. 13130fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands FactorPowerSum = 0; 13140fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands for (unsigned Idx = 1; Idx < Ops.size(); ++Idx) { 13150fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Value *Op = Ops[Idx-1].Op; 1316464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 13170fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // Count the number of occurrences of this value. 13180fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands unsigned Count = 1; 13190fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands for (; Idx < Ops.size() && Ops[Idx].Op == Op; ++Idx) 13200fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands ++Count; 13210fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands if (Count == 1) 13220fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands continue; 1323d9b0b025612992a0b724eeca8bdf10b1d7a5c355Benjamin Kramer // Move an even number of occurrences to Factors. 13240fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Count &= ~1U; 13250fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Idx -= Count; 13260fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands FactorPowerSum += Count; 13270fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Factors.push_back(Factor(Op, Count)); 13280fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands Ops.erase(Ops.begin()+Idx, Ops.begin()+Idx+Count); 1329464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth } 13300fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 1331464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // None of the adjustments above should have reduced the sum of factor powers 1332464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // below our mininum of '4'. 1333464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth assert(FactorPowerSum >= 4); 1334464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1335464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth std::sort(Factors.begin(), Factors.end(), Factor::PowerDescendingSorter()); 1336464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return true; 1337464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth} 1338464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1339464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// \brief Build a tree of multiplies, computing the product of Ops. 1340464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruthstatic Value *buildMultiplyTree(IRBuilder<> &Builder, 1341464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth SmallVectorImpl<Value*> &Ops) { 1342464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth if (Ops.size() == 1) 1343464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return Ops.back(); 1344464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1345464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth Value *LHS = Ops.pop_back_val(); 1346464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth do { 1347464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth LHS = Builder.CreateMul(LHS, Ops.pop_back_val()); 1348464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth } while (!Ops.empty()); 1349464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1350464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return LHS; 1351464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth} 1352464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1353464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// \brief Build a minimal multiplication DAG for (a^x)*(b^y)*(c^z)*... 1354464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// 1355464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// Given a vector of values raised to various powers, where no two values are 1356464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// equal and the powers are sorted in decreasing order, compute the minimal 1357464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// DAG of multiplies to compute the final product, and return that product 1358464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth/// value. 1359464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler CarruthValue *Reassociate::buildMinimalMultiplyDAG(IRBuilder<> &Builder, 1360464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth SmallVectorImpl<Factor> &Factors) { 1361464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth assert(Factors[0].Power); 1362464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth SmallVector<Value *, 4> OuterProduct; 1363464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth for (unsigned LastIdx = 0, Idx = 1, Size = Factors.size(); 1364464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth Idx < Size && Factors[Idx].Power > 0; ++Idx) { 1365464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth if (Factors[Idx].Power != Factors[LastIdx].Power) { 1366464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth LastIdx = Idx; 1367464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth continue; 1368464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth } 1369464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1370464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // We want to multiply across all the factors with the same power so that 1371464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // we can raise them to that power as a single entity. Build a mini tree 1372464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // for that. 1373464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth SmallVector<Value *, 4> InnerProduct; 1374464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth InnerProduct.push_back(Factors[LastIdx].Base); 1375464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth do { 1376464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth InnerProduct.push_back(Factors[Idx].Base); 1377464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth ++Idx; 1378464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth } while (Idx < Size && Factors[Idx].Power == Factors[LastIdx].Power); 1379464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1380464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // Reset the base value of the first factor to the new expression tree. 1381464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // We'll remove all the factors with the same power in a second pass. 1382841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Value *M = Factors[LastIdx].Base = buildMultiplyTree(Builder, InnerProduct); 1383841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (Instruction *MI = dyn_cast<Instruction>(M)) 1384841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands RedoInsts.insert(MI); 1385464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1386464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth LastIdx = Idx; 1387464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth } 1388464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // Unique factors with equal powers -- we've folded them into the first one's 1389464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // base. 1390464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth Factors.erase(std::unique(Factors.begin(), Factors.end(), 1391464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth Factor::PowerEqual()), 1392464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth Factors.end()); 1393464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1394464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // Iteratively collect the base of each factor with an add power into the 1395464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // outer product, and halve each power in preparation for squaring the 1396464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // expression. 1397464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth for (unsigned Idx = 0, Size = Factors.size(); Idx != Size; ++Idx) { 1398464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth if (Factors[Idx].Power & 1) 1399464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth OuterProduct.push_back(Factors[Idx].Base); 1400464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth Factors[Idx].Power >>= 1; 1401464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth } 1402464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth if (Factors[0].Power) { 1403464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth Value *SquareRoot = buildMinimalMultiplyDAG(Builder, Factors); 1404464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth OuterProduct.push_back(SquareRoot); 1405464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth OuterProduct.push_back(SquareRoot); 1406464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth } 1407464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth if (OuterProduct.size() == 1) 1408464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return OuterProduct.front(); 1409464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1410a33701098936ffba12326d96e98d388357f3e098Duncan Sands Value *V = buildMultiplyTree(Builder, OuterProduct); 1411a33701098936ffba12326d96e98d388357f3e098Duncan Sands return V; 1412464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth} 1413464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1414464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler CarruthValue *Reassociate::OptimizeMul(BinaryOperator *I, 1415464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth SmallVectorImpl<ValueEntry> &Ops) { 1416464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // We can only optimize the multiplies when there is a chain of more than 1417464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // three, such that a balanced tree might require fewer total multiplies. 1418464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth if (Ops.size() < 4) 1419464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return 0; 1420464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1421464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // Try to turn linear trees of multiplies without other uses of the 1422464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // intermediate stages into minimal multiply DAGs with perfect sub-expression 1423464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth // re-use. 1424464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth SmallVector<Factor, 4> Factors; 1425464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth if (!collectMultiplyFactors(Ops, Factors)) 1426464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return 0; // All distinct factors, so nothing left for us to do. 1427464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1428464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth IRBuilder<> Builder(I); 1429464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth Value *V = buildMinimalMultiplyDAG(Builder, Factors); 1430464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth if (Ops.empty()) 1431464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return V; 1432464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1433464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth ValueEntry NewEntry = ValueEntry(getRank(V), V); 1434464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth Ops.insert(std::lower_bound(Ops.begin(), Ops.end(), NewEntry), NewEntry); 1435464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return 0; 1436464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth} 1437464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth 1438e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris LattnerValue *Reassociate::OptimizeExpression(BinaryOperator *I, 14399f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner SmallVectorImpl<ValueEntry> &Ops) { 1440469001000620df176decd093a300db84a06cc78bChris Lattner // Now that we have the linearized expression tree, try to optimize it. 1441469001000620df176decd093a300db84a06cc78bChris Lattner // Start by folding any constants that we found. 1442e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner if (Ops.size() == 1) return Ops[0].Op; 1443469001000620df176decd093a300db84a06cc78bChris Lattner 1444e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner unsigned Opcode = I->getOpcode(); 1445e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1446ec531233a16605756a84d175178e1ee0fac4791cChris Lattner // Handle destructive annihilation due to identities between elements in the 1447469001000620df176decd093a300db84a06cc78bChris Lattner // argument list here. 1448464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth unsigned NumOps = Ops.size(); 1449109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner switch (Opcode) { 1450109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner default: break; 1451109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner case Instruction::And: 1452109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner case Instruction::Or: 1453464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth case Instruction::Xor: 1454f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner if (Value *Result = OptimizeAndOrXor(Opcode, Ops)) 1455f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner return Result; 1456109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner break; 1457109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner 1458464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth case Instruction::Add: 145994285e620b845e09b18939e8d6448e01e692f3ceChris Lattner if (Value *Result = OptimizeAdd(I, Ops)) 1460f3f55a9bc1ce62fad7faecff7bd83565d569dee8Chris Lattner return Result; 1461464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth break; 1462e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner 1463464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth case Instruction::Mul: 1464464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth if (Value *Result = OptimizeMul(I, Ops)) 1465464bda3a167bb761eb3c9c178db3fa8ed26fe825Chandler Carruth return Result; 1466109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner break; 1467109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner } 1468109d34d6ff51a0fdd39d7b3b373a83fcca6c67a3Chris Lattner 1469841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (Ops.size() != NumOps) 1470e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner return OptimizeExpression(I, Ops); 1471e5022fe4cd83eef91f5c3a21c943ca9b65507ab8Chris Lattner return 0; 1472469001000620df176decd093a300db84a06cc78bChris Lattner} 1473469001000620df176decd093a300db84a06cc78bChris Lattner 1474841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands/// EraseInst - Zap the given instruction, adding interesting operands to the 1475841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands/// work list. 1476841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsvoid Reassociate::EraseInst(Instruction *I) { 1477841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands assert(isInstructionTriviallyDead(I) && "Trivially dead instructions only!"); 1478841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); 1479841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands // Erase the dead instruction. 1480841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands ValueRankMap.erase(I); 1481917f99354fa558e50d17191f593f81155b4ab2c3Nick Lewycky RedoInsts.remove(I); 1482841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands I->eraseFromParent(); 1483841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands // Optimize its operands. 1484cd117f736c47947af5c6549734549e135e626c5cDuncan Sands SmallPtrSet<Instruction *, 8> Visited; // Detect self-referential nodes. 1485841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands for (unsigned i = 0, e = Ops.size(); i != e; ++i) 1486841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (Instruction *Op = dyn_cast<Instruction>(Ops[i])) { 1487841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands // If this is a node in an expression tree, climb to the expression root 1488841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands // and add that since that's where optimization actually happens. 1489841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands unsigned Opcode = Op->getOpcode(); 1490cd117f736c47947af5c6549734549e135e626c5cDuncan Sands while (Op->hasOneUse() && Op->use_back()->getOpcode() == Opcode && 1491cd117f736c47947af5c6549734549e135e626c5cDuncan Sands Visited.insert(Op)) 1492841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Op = Op->use_back(); 1493841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands RedoInsts.insert(Op); 1494841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands } 1495841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands} 1496841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands 1497841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands/// OptimizeInst - Inspect and optimize the given instruction. Note that erasing 1498841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands/// instructions is not allowed. 1499841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sandsvoid Reassociate::OptimizeInst(Instruction *I) { 1500841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands // Only consider operations that we understand. 1501841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (!isa<BinaryOperator>(I)) 1502841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands return; 1503841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands 1504841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (I->getOpcode() == Instruction::Shl && 1505841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands isa<ConstantInt>(I->getOperand(1))) 1506841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands // If an operand of this shift is a reassociable multiply, or if the shift 1507841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands // is used by a reassociable multiply or add, turn into a multiply. 1508841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (isReassociableOp(I->getOperand(0), Instruction::Mul) || 1509841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands (I->hasOneUse() && 1510841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands (isReassociableOp(I->use_back(), Instruction::Mul) || 1511841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands isReassociableOp(I->use_back(), Instruction::Add)))) { 1512841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Instruction *NI = ConvertShiftToMul(I); 1513841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands RedoInsts.insert(I); 1514dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman MadeChange = true; 1515841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands I = NI; 1516dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman } 1517641f02f10f08c9a9add651c6f0169f5441eaeb49Chris Lattner 1518423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson // Floating point binary operators are not associative, but we can still 1519423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson // commute (some) of them, to canonicalize the order of their operands. 1520423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson // This can potentially expose more CSE opportunities, and makes writing 1521423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson // other transformations simpler. 1522841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if ((I->getType()->isFloatingPointTy() || I->getType()->isVectorTy())) { 1523423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson // FAdd and FMul can be commuted. 1524841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (I->getOpcode() != Instruction::FMul && 1525841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands I->getOpcode() != Instruction::FAdd) 1526423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson return; 1527423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson 1528841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Value *LHS = I->getOperand(0); 1529841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Value *RHS = I->getOperand(1); 1530423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson unsigned LHSRank = getRank(LHS); 1531423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson unsigned RHSRank = getRank(RHS); 1532423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson 1533423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson // Sort the operands by rank. 1534423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson if (RHSRank < LHSRank) { 1535841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands I->setOperand(0, RHS); 1536841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands I->setOperand(1, LHS); 1537423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson } 1538423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson 1539423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson return; 1540423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson } 1541423f19f2dadee9325766008b63c1faf3c644043bOwen Anderson 1542dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman // Do not reassociate boolean (i1) expressions. We want to preserve the 1543dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman // original order of evaluation for short-circuited comparisons that 1544dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman // SimplifyCFG has folded to AND/OR expressions. If the expression 1545dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman // is not further optimized, it is likely to be transformed back to a 1546dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman // short-circuited form for code gen, and the source order may have been 1547dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman // optimized for the most likely conditions. 1548841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (I->getType()->isIntegerTy(1)) 1549dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman return; 1550fc375d22001d27ba6d22db67821da057e36f7f89Bob Wilson 1551dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman // If this is a subtract instruction which is not already in negate form, 1552dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman // see if we can convert it to X+-Y. 1553841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (I->getOpcode() == Instruction::Sub) { 1554841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (ShouldBreakUpSubtract(I)) { 1555841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Instruction *NI = BreakUpSubtract(I); 1556841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands RedoInsts.insert(I); 1557dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman MadeChange = true; 1558841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands I = NI; 1559841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands } else if (BinaryOperator::isNeg(I)) { 1560dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman // Otherwise, this is a negation. See if the operand is a multiply tree 1561dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman // and if this is not an inner node of a multiply tree. 1562841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (isReassociableOp(I->getOperand(1), Instruction::Mul) && 1563841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands (!I->hasOneUse() || 1564841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands !isReassociableOp(I->use_back(), Instruction::Mul))) { 1565841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Instruction *NI = LowerNegateToMultiply(I); 1566841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands RedoInsts.insert(I); 1567d5b8d92b9f4dfb216e4f2a52b4e801d7559574baChris Lattner MadeChange = true; 1568841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands I = NI; 156908b43921e18f314c4fd38049291d323830934c36Chris Lattner } 1570f33151aff008c40eec6435ddb7a5c9017b6acef9Chris Lattner } 1571dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman } 1572e4b730441dab4aff9a69aeddbdea98990e7703c4Chris Lattner 1573841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands // If this instruction is an associative binary operator, process it. 1574841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (!I->isAssociative()) return; 1575841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands BinaryOperator *BO = cast<BinaryOperator>(I); 157600b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen 1577dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman // If this is an interior node of a reassociable tree, ignore it until we 1578dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman // get to the root of the tree, to avoid N^2 analysis. 1579841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (BO->hasOneUse() && BO->use_back()->getOpcode() == BO->getOpcode()) 1580dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman return; 1581c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner 1582e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling // If this is an add tree that is used by a sub instruction, ignore it 1583dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman // until we process the subtract. 1584841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (BO->hasOneUse() && BO->getOpcode() == Instruction::Add && 1585841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands cast<Instruction>(BO->use_back())->getOpcode() == Instruction::Sub) 1586dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman return; 15877b4ad94282b94e1827be29b4db73fdf6e241f748Chris Lattner 1588841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands ReassociateExpression(BO); 1589895b392269cad07c34d59110d68dc86708c53adbChris Lattner} 1590c9fd097a01383323f166c14c17d3984620cad766Chris Lattner 1591cd117f736c47947af5c6549734549e135e626c5cDuncan Sandsvoid Reassociate::ReassociateExpression(BinaryOperator *I) { 1592e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 159369e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // First, walk the expression tree, linearizing the tree, collecting the 159469e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // operand information. 1595c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands SmallVector<RepeatedValue, 8> Tree; 1596c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands MadeChange |= LinearizeExprTree(I, Tree); 15979f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner SmallVector<ValueEntry, 8> Ops; 1598c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Ops.reserve(Tree.size()); 1599c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands for (unsigned i = 0, e = Tree.size(); i != e; ++i) { 1600c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands RepeatedValue E = Tree[i]; 1601c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands Ops.append(E.second.getZExtValue(), 1602c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands ValueEntry(getRank(E.first), E.first)); 1603c038a7833565ecf92a699371d448135a097c9e2fDuncan Sands } 1604e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 160524dfa52fa20eee39440e5dec4d23359e5a6773c7Duncan Sands DEBUG(dbgs() << "RAIn:\t"; PrintOps(I, Ops); dbgs() << '\n'); 160624dfa52fa20eee39440e5dec4d23359e5a6773c7Duncan Sands 1607895b392269cad07c34d59110d68dc86708c53adbChris Lattner // Now that we have linearized the tree to a list and have gathered all of 1608895b392269cad07c34d59110d68dc86708c53adbChris Lattner // the operands and their ranks, sort the operands by their rank. Use a 1609895b392269cad07c34d59110d68dc86708c53adbChris Lattner // stable_sort so that values with equal ranks will have their relative 1610895b392269cad07c34d59110d68dc86708c53adbChris Lattner // positions maintained (and so the compiler is deterministic). Note that 1611895b392269cad07c34d59110d68dc86708c53adbChris Lattner // this sorts so that the highest ranking values end up at the beginning of 1612895b392269cad07c34d59110d68dc86708c53adbChris Lattner // the vector. 1613895b392269cad07c34d59110d68dc86708c53adbChris Lattner std::stable_sort(Ops.begin(), Ops.end()); 1614e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1615895b392269cad07c34d59110d68dc86708c53adbChris Lattner // OptimizeExpression - Now that we have the expression tree in a convenient 1616895b392269cad07c34d59110d68dc86708c53adbChris Lattner // sorted form, optimize it globally if possible. 1617895b392269cad07c34d59110d68dc86708c53adbChris Lattner if (Value *V = OptimizeExpression(I, Ops)) { 1618cd117f736c47947af5c6549734549e135e626c5cDuncan Sands if (V == I) 1619cd117f736c47947af5c6549734549e135e626c5cDuncan Sands // Self-referential expression in unreachable code. 1620cd117f736c47947af5c6549734549e135e626c5cDuncan Sands return; 1621895b392269cad07c34d59110d68dc86708c53adbChris Lattner // This expression tree simplified to something that isn't a tree, 1622895b392269cad07c34d59110d68dc86708c53adbChris Lattner // eliminate it. 1623a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene DEBUG(dbgs() << "Reassoc to scalar: " << *V << '\n'); 1624895b392269cad07c34d59110d68dc86708c53adbChris Lattner I->replaceAllUsesWith(V); 16255367b23f76e75ebb680956575346fa8c3d56780fDevang Patel if (Instruction *VI = dyn_cast<Instruction>(V)) 16265367b23f76e75ebb680956575346fa8c3d56780fDevang Patel VI->setDebugLoc(I->getDebugLoc()); 1627841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands RedoInsts.insert(I); 16289fdaefad580194353f34b6d72669591f8f9d811aChris Lattner ++NumAnnihil; 1629cd117f736c47947af5c6549734549e135e626c5cDuncan Sands return; 1630895b392269cad07c34d59110d68dc86708c53adbChris Lattner } 1631e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1632895b392269cad07c34d59110d68dc86708c53adbChris Lattner // We want to sink immediates as deeply as possible except in the case where 1633895b392269cad07c34d59110d68dc86708c53adbChris Lattner // this is a multiply tree used only by an add, and the immediate is a -1. 1634895b392269cad07c34d59110d68dc86708c53adbChris Lattner // In this case we reassociate to put the negation on the outside so that we 1635895b392269cad07c34d59110d68dc86708c53adbChris Lattner // can fold the negation into the add: (-X)*Y + Z -> Z-X*Y 1636895b392269cad07c34d59110d68dc86708c53adbChris Lattner if (I->getOpcode() == Instruction::Mul && I->hasOneUse() && 1637895b392269cad07c34d59110d68dc86708c53adbChris Lattner cast<Instruction>(I->use_back())->getOpcode() == Instruction::Add && 1638895b392269cad07c34d59110d68dc86708c53adbChris Lattner isa<ConstantInt>(Ops.back().Op) && 1639895b392269cad07c34d59110d68dc86708c53adbChris Lattner cast<ConstantInt>(Ops.back().Op)->isAllOnesValue()) { 16409f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner ValueEntry Tmp = Ops.pop_back_val(); 16419f7b7089be854c323f8d9a4627d80e47adf496e6Chris Lattner Ops.insert(Ops.begin(), Tmp); 1642895b392269cad07c34d59110d68dc86708c53adbChris Lattner } 1643e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1644a1fa76cb5443e7b0fe7d36ee1118f80050e746f9David Greene DEBUG(dbgs() << "RAOut:\t"; PrintOps(I, Ops); dbgs() << '\n'); 1645e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 1646895b392269cad07c34d59110d68dc86708c53adbChris Lattner if (Ops.size() == 1) { 1647cd117f736c47947af5c6549734549e135e626c5cDuncan Sands if (Ops[0].Op == I) 1648cd117f736c47947af5c6549734549e135e626c5cDuncan Sands // Self-referential expression in unreachable code. 1649cd117f736c47947af5c6549734549e135e626c5cDuncan Sands return; 1650cd117f736c47947af5c6549734549e135e626c5cDuncan Sands 1651895b392269cad07c34d59110d68dc86708c53adbChris Lattner // This expression tree simplified to something that isn't a tree, 1652895b392269cad07c34d59110d68dc86708c53adbChris Lattner // eliminate it. 1653895b392269cad07c34d59110d68dc86708c53adbChris Lattner I->replaceAllUsesWith(Ops[0].Op); 16545367b23f76e75ebb680956575346fa8c3d56780fDevang Patel if (Instruction *OI = dyn_cast<Instruction>(Ops[0].Op)) 16555367b23f76e75ebb680956575346fa8c3d56780fDevang Patel OI->setDebugLoc(I->getDebugLoc()); 1656841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands RedoInsts.insert(I); 1657cd117f736c47947af5c6549734549e135e626c5cDuncan Sands return; 16584fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner } 1659e8cd3f249133406c9b2a8dc6b6dbd2752fc605b4Bill Wendling 166069e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // Now that we ordered and optimized the expressions, splat them back into 166169e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner // the expression tree, removing any unneeded nodes. 166269e98e2c0f7a1a1a8e3547b57e3e78e1142b8a64Chris Lattner RewriteExprTree(I, Ops); 16634fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner} 16644fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner 16657e70829632f82de15db187845666aaca6e04b792Chris Lattnerbool Reassociate::runOnFunction(Function &F) { 1666841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands // Calculate the rank map for F 16674fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner BuildRankMap(F); 16684fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner 1669c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner MadeChange = false; 1670841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) { 1671841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands // Optimize every instruction in the basic block. 1672841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE; ) 1673841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (isInstructionTriviallyDead(II)) { 1674841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands EraseInst(II++); 1675841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands } else { 1676841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands OptimizeInst(II); 1677841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands assert(II->getParent() == BI && "Moved to a different block!"); 1678841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands ++II; 1679841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands } 1680841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands 1681841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands // If this produced extra instructions to optimize, handle them now. 1682841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands while (!RedoInsts.empty()) { 1683841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands Instruction *I = RedoInsts.pop_back_val(); 1684841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands if (isInstructionTriviallyDead(I)) 1685841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands EraseInst(I); 1686841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands else 1687841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands OptimizeInst(I); 1688dac5dbadeb840ddded4665d144f31c5f88494d6eDan Gohman } 1689841f42617531ff947b2d957e7b0cb367a290aae4Duncan Sands } 16904fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner 16910fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands // We are done with the rank map. 16920fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands RankMap.clear(); 16930fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands ValueRankMap.clear(); 16940fd120b970fe9a036ae664ad1bfbf04e55b3b8a7Duncan Sands 1695c0649ac931d22b7118c1db292b887cd4eb52cd32Chris Lattner return MadeChange; 16964fd56003ab29e3662c909bb10e47daa97ceb55abChris Lattner} 1697