13ca95b02SDimitry Andric //===- LoopUnrollAnalyzer.cpp - Unrolling Effect Estimation -----*- C++ -*-===//
23ca95b02SDimitry Andric //
33ca95b02SDimitry Andric //                     The LLVM Compiler Infrastructure
43ca95b02SDimitry Andric //
53ca95b02SDimitry Andric // This file is distributed under the University of Illinois Open Source
63ca95b02SDimitry Andric // License. See LICENSE.TXT for details.
73ca95b02SDimitry Andric //
83ca95b02SDimitry Andric //===----------------------------------------------------------------------===//
93ca95b02SDimitry Andric //
103ca95b02SDimitry Andric // This file implements UnrolledInstAnalyzer class. It's used for predicting
113ca95b02SDimitry Andric // potential effects that loop unrolling might have, such as enabling constant
123ca95b02SDimitry Andric // propagation and other optimizations.
133ca95b02SDimitry Andric //
143ca95b02SDimitry Andric //===----------------------------------------------------------------------===//
153ca95b02SDimitry Andric 
163ca95b02SDimitry Andric #include "llvm/Analysis/LoopUnrollAnalyzer.h"
173ca95b02SDimitry Andric 
183ca95b02SDimitry Andric using namespace llvm;
193ca95b02SDimitry Andric 
20*4ba319b5SDimitry Andric /// Try to simplify instruction \param I using its SCEV expression.
213ca95b02SDimitry Andric ///
223ca95b02SDimitry Andric /// The idea is that some AddRec expressions become constants, which then
233ca95b02SDimitry Andric /// could trigger folding of other instructions. However, that only happens
243ca95b02SDimitry Andric /// for expressions whose start value is also constant, which isn't always the
253ca95b02SDimitry Andric /// case. In another common and important case the start value is just some
263ca95b02SDimitry Andric /// address (i.e. SCEVUnknown) - in this case we compute the offset and save
273ca95b02SDimitry Andric /// it along with the base address instead.
simplifyInstWithSCEV(Instruction * I)283ca95b02SDimitry Andric bool UnrolledInstAnalyzer::simplifyInstWithSCEV(Instruction *I) {
293ca95b02SDimitry Andric   if (!SE.isSCEVable(I->getType()))
303ca95b02SDimitry Andric     return false;
313ca95b02SDimitry Andric 
323ca95b02SDimitry Andric   const SCEV *S = SE.getSCEV(I);
333ca95b02SDimitry Andric   if (auto *SC = dyn_cast<SCEVConstant>(S)) {
343ca95b02SDimitry Andric     SimplifiedValues[I] = SC->getValue();
353ca95b02SDimitry Andric     return true;
363ca95b02SDimitry Andric   }
373ca95b02SDimitry Andric 
383ca95b02SDimitry Andric   auto *AR = dyn_cast<SCEVAddRecExpr>(S);
393ca95b02SDimitry Andric   if (!AR || AR->getLoop() != L)
403ca95b02SDimitry Andric     return false;
413ca95b02SDimitry Andric 
423ca95b02SDimitry Andric   const SCEV *ValueAtIteration = AR->evaluateAtIteration(IterationNumber, SE);
433ca95b02SDimitry Andric   // Check if the AddRec expression becomes a constant.
443ca95b02SDimitry Andric   if (auto *SC = dyn_cast<SCEVConstant>(ValueAtIteration)) {
453ca95b02SDimitry Andric     SimplifiedValues[I] = SC->getValue();
463ca95b02SDimitry Andric     return true;
473ca95b02SDimitry Andric   }
483ca95b02SDimitry Andric 
493ca95b02SDimitry Andric   // Check if the offset from the base address becomes a constant.
503ca95b02SDimitry Andric   auto *Base = dyn_cast<SCEVUnknown>(SE.getPointerBase(S));
513ca95b02SDimitry Andric   if (!Base)
523ca95b02SDimitry Andric     return false;
533ca95b02SDimitry Andric   auto *Offset =
543ca95b02SDimitry Andric       dyn_cast<SCEVConstant>(SE.getMinusSCEV(ValueAtIteration, Base));
553ca95b02SDimitry Andric   if (!Offset)
563ca95b02SDimitry Andric     return false;
573ca95b02SDimitry Andric   SimplifiedAddress Address;
583ca95b02SDimitry Andric   Address.Base = Base->getValue();
593ca95b02SDimitry Andric   Address.Offset = Offset->getValue();
603ca95b02SDimitry Andric   SimplifiedAddresses[I] = Address;
613ca95b02SDimitry Andric   return false;
623ca95b02SDimitry Andric }
633ca95b02SDimitry Andric 
643ca95b02SDimitry Andric /// Try to simplify binary operator I.
653ca95b02SDimitry Andric ///
663ca95b02SDimitry Andric /// TODO: Probably it's worth to hoist the code for estimating the
673ca95b02SDimitry Andric /// simplifications effects to a separate class, since we have a very similar
683ca95b02SDimitry Andric /// code in InlineCost already.
visitBinaryOperator(BinaryOperator & I)693ca95b02SDimitry Andric bool UnrolledInstAnalyzer::visitBinaryOperator(BinaryOperator &I) {
703ca95b02SDimitry Andric   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
713ca95b02SDimitry Andric   if (!isa<Constant>(LHS))
723ca95b02SDimitry Andric     if (Constant *SimpleLHS = SimplifiedValues.lookup(LHS))
733ca95b02SDimitry Andric       LHS = SimpleLHS;
743ca95b02SDimitry Andric   if (!isa<Constant>(RHS))
753ca95b02SDimitry Andric     if (Constant *SimpleRHS = SimplifiedValues.lookup(RHS))
763ca95b02SDimitry Andric       RHS = SimpleRHS;
773ca95b02SDimitry Andric 
783ca95b02SDimitry Andric   Value *SimpleV = nullptr;
793ca95b02SDimitry Andric   const DataLayout &DL = I.getModule()->getDataLayout();
803ca95b02SDimitry Andric   if (auto FI = dyn_cast<FPMathOperator>(&I))
813ca95b02SDimitry Andric     SimpleV =
823ca95b02SDimitry Andric         SimplifyFPBinOp(I.getOpcode(), LHS, RHS, FI->getFastMathFlags(), DL);
833ca95b02SDimitry Andric   else
843ca95b02SDimitry Andric     SimpleV = SimplifyBinOp(I.getOpcode(), LHS, RHS, DL);
853ca95b02SDimitry Andric 
863ca95b02SDimitry Andric   if (Constant *C = dyn_cast_or_null<Constant>(SimpleV))
873ca95b02SDimitry Andric     SimplifiedValues[&I] = C;
883ca95b02SDimitry Andric 
893ca95b02SDimitry Andric   if (SimpleV)
903ca95b02SDimitry Andric     return true;
913ca95b02SDimitry Andric   return Base::visitBinaryOperator(I);
923ca95b02SDimitry Andric }
933ca95b02SDimitry Andric 
943ca95b02SDimitry Andric /// Try to fold load I.
visitLoad(LoadInst & I)953ca95b02SDimitry Andric bool UnrolledInstAnalyzer::visitLoad(LoadInst &I) {
963ca95b02SDimitry Andric   Value *AddrOp = I.getPointerOperand();
973ca95b02SDimitry Andric 
983ca95b02SDimitry Andric   auto AddressIt = SimplifiedAddresses.find(AddrOp);
993ca95b02SDimitry Andric   if (AddressIt == SimplifiedAddresses.end())
1003ca95b02SDimitry Andric     return false;
1013ca95b02SDimitry Andric   ConstantInt *SimplifiedAddrOp = AddressIt->second.Offset;
1023ca95b02SDimitry Andric 
1033ca95b02SDimitry Andric   auto *GV = dyn_cast<GlobalVariable>(AddressIt->second.Base);
1043ca95b02SDimitry Andric   // We're only interested in loads that can be completely folded to a
1053ca95b02SDimitry Andric   // constant.
1063ca95b02SDimitry Andric   if (!GV || !GV->hasDefinitiveInitializer() || !GV->isConstant())
1073ca95b02SDimitry Andric     return false;
1083ca95b02SDimitry Andric 
1093ca95b02SDimitry Andric   ConstantDataSequential *CDS =
1103ca95b02SDimitry Andric       dyn_cast<ConstantDataSequential>(GV->getInitializer());
1113ca95b02SDimitry Andric   if (!CDS)
1123ca95b02SDimitry Andric     return false;
1133ca95b02SDimitry Andric 
1143ca95b02SDimitry Andric   // We might have a vector load from an array. FIXME: for now we just bail
1153ca95b02SDimitry Andric   // out in this case, but we should be able to resolve and simplify such
1163ca95b02SDimitry Andric   // loads.
1173ca95b02SDimitry Andric   if (CDS->getElementType() != I.getType())
1183ca95b02SDimitry Andric     return false;
1193ca95b02SDimitry Andric 
1206c4bc1bdSDimitry Andric   unsigned ElemSize = CDS->getElementType()->getPrimitiveSizeInBits() / 8U;
1216c4bc1bdSDimitry Andric   if (SimplifiedAddrOp->getValue().getActiveBits() > 64)
1223ca95b02SDimitry Andric     return false;
1236c4bc1bdSDimitry Andric   int64_t SimplifiedAddrOpV = SimplifiedAddrOp->getSExtValue();
1246c4bc1bdSDimitry Andric   if (SimplifiedAddrOpV < 0) {
1256c4bc1bdSDimitry Andric     // FIXME: For now we conservatively ignore out of bound accesses, but
1266c4bc1bdSDimitry Andric     // we're allowed to perform the optimization in this case.
1276c4bc1bdSDimitry Andric     return false;
1286c4bc1bdSDimitry Andric   }
1296c4bc1bdSDimitry Andric   uint64_t Index = static_cast<uint64_t>(SimplifiedAddrOpV) / ElemSize;
1303ca95b02SDimitry Andric   if (Index >= CDS->getNumElements()) {
1313ca95b02SDimitry Andric     // FIXME: For now we conservatively ignore out of bound accesses, but
1323ca95b02SDimitry Andric     // we're allowed to perform the optimization in this case.
1333ca95b02SDimitry Andric     return false;
1343ca95b02SDimitry Andric   }
1353ca95b02SDimitry Andric 
1363ca95b02SDimitry Andric   Constant *CV = CDS->getElementAsConstant(Index);
1373ca95b02SDimitry Andric   assert(CV && "Constant expected.");
1383ca95b02SDimitry Andric   SimplifiedValues[&I] = CV;
1393ca95b02SDimitry Andric 
1403ca95b02SDimitry Andric   return true;
1413ca95b02SDimitry Andric }
1423ca95b02SDimitry Andric 
1433ca95b02SDimitry Andric /// Try to simplify cast instruction.
visitCastInst(CastInst & I)1443ca95b02SDimitry Andric bool UnrolledInstAnalyzer::visitCastInst(CastInst &I) {
1453ca95b02SDimitry Andric   // Propagate constants through casts.
1463ca95b02SDimitry Andric   Constant *COp = dyn_cast<Constant>(I.getOperand(0));
1473ca95b02SDimitry Andric   if (!COp)
1483ca95b02SDimitry Andric     COp = SimplifiedValues.lookup(I.getOperand(0));
1493ca95b02SDimitry Andric 
1503ca95b02SDimitry Andric   // If we know a simplified value for this operand and cast is valid, save the
1513ca95b02SDimitry Andric   // result to SimplifiedValues.
1523ca95b02SDimitry Andric   // The cast can be invalid, because SimplifiedValues contains results of SCEV
1533ca95b02SDimitry Andric   // analysis, which operates on integers (and, e.g., might convert i8* null to
1543ca95b02SDimitry Andric   // i32 0).
1553ca95b02SDimitry Andric   if (COp && CastInst::castIsValid(I.getOpcode(), COp, I.getType())) {
1563ca95b02SDimitry Andric     if (Constant *C =
1573ca95b02SDimitry Andric             ConstantExpr::getCast(I.getOpcode(), COp, I.getType())) {
1583ca95b02SDimitry Andric       SimplifiedValues[&I] = C;
1593ca95b02SDimitry Andric       return true;
1603ca95b02SDimitry Andric     }
1613ca95b02SDimitry Andric   }
1623ca95b02SDimitry Andric 
1633ca95b02SDimitry Andric   return Base::visitCastInst(I);
1643ca95b02SDimitry Andric }
1653ca95b02SDimitry Andric 
1663ca95b02SDimitry Andric /// Try to simplify cmp instruction.
visitCmpInst(CmpInst & I)1673ca95b02SDimitry Andric bool UnrolledInstAnalyzer::visitCmpInst(CmpInst &I) {
1683ca95b02SDimitry Andric   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1693ca95b02SDimitry Andric 
1703ca95b02SDimitry Andric   // First try to handle simplified comparisons.
1713ca95b02SDimitry Andric   if (!isa<Constant>(LHS))
1723ca95b02SDimitry Andric     if (Constant *SimpleLHS = SimplifiedValues.lookup(LHS))
1733ca95b02SDimitry Andric       LHS = SimpleLHS;
1743ca95b02SDimitry Andric   if (!isa<Constant>(RHS))
1753ca95b02SDimitry Andric     if (Constant *SimpleRHS = SimplifiedValues.lookup(RHS))
1763ca95b02SDimitry Andric       RHS = SimpleRHS;
1773ca95b02SDimitry Andric 
1783ca95b02SDimitry Andric   if (!isa<Constant>(LHS) && !isa<Constant>(RHS)) {
1793ca95b02SDimitry Andric     auto SimplifiedLHS = SimplifiedAddresses.find(LHS);
1803ca95b02SDimitry Andric     if (SimplifiedLHS != SimplifiedAddresses.end()) {
1813ca95b02SDimitry Andric       auto SimplifiedRHS = SimplifiedAddresses.find(RHS);
1823ca95b02SDimitry Andric       if (SimplifiedRHS != SimplifiedAddresses.end()) {
1833ca95b02SDimitry Andric         SimplifiedAddress &LHSAddr = SimplifiedLHS->second;
1843ca95b02SDimitry Andric         SimplifiedAddress &RHSAddr = SimplifiedRHS->second;
1853ca95b02SDimitry Andric         if (LHSAddr.Base == RHSAddr.Base) {
1863ca95b02SDimitry Andric           LHS = LHSAddr.Offset;
1873ca95b02SDimitry Andric           RHS = RHSAddr.Offset;
1883ca95b02SDimitry Andric         }
1893ca95b02SDimitry Andric       }
1903ca95b02SDimitry Andric     }
1913ca95b02SDimitry Andric   }
1923ca95b02SDimitry Andric 
1933ca95b02SDimitry Andric   if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
1943ca95b02SDimitry Andric     if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
1953ca95b02SDimitry Andric       if (CLHS->getType() == CRHS->getType()) {
1963ca95b02SDimitry Andric         if (Constant *C = ConstantExpr::getCompare(I.getPredicate(), CLHS, CRHS)) {
1973ca95b02SDimitry Andric           SimplifiedValues[&I] = C;
1983ca95b02SDimitry Andric           return true;
1993ca95b02SDimitry Andric         }
2003ca95b02SDimitry Andric       }
2013ca95b02SDimitry Andric     }
2023ca95b02SDimitry Andric   }
2033ca95b02SDimitry Andric 
2043ca95b02SDimitry Andric   return Base::visitCmpInst(I);
2053ca95b02SDimitry Andric }
2063ca95b02SDimitry Andric 
visitPHINode(PHINode & PN)2073ca95b02SDimitry Andric bool UnrolledInstAnalyzer::visitPHINode(PHINode &PN) {
2083ca95b02SDimitry Andric   // Run base visitor first. This way we can gather some useful for later
2093ca95b02SDimitry Andric   // analysis information.
2103ca95b02SDimitry Andric   if (Base::visitPHINode(PN))
2113ca95b02SDimitry Andric     return true;
2123ca95b02SDimitry Andric 
2133ca95b02SDimitry Andric   // The loop induction PHI nodes are definitionally free.
2143ca95b02SDimitry Andric   return PN.getParent() == L->getHeader();
2153ca95b02SDimitry Andric }
216