1 //===- ScalarEvolutionNormalization.cpp - See below -------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements utilities for working with "normalized" expressions. 11 // See the comments at the top of ScalarEvolutionNormalization.h for details. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Analysis/Dominators.h" 16 #include "llvm/Analysis/LoopInfo.h" 17 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 18 #include "llvm/Analysis/ScalarEvolutionNormalization.h" 19 using namespace llvm; 20 21 /// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression 22 /// and now we need to decide whether the user should use the preinc or post-inc 23 /// value. If this user should use the post-inc version of the IV, return true. 24 /// 25 /// Choosing wrong here can break dominance properties (if we choose to use the 26 /// post-inc value when we cannot) or it can end up adding extra live-ranges to 27 /// the loop, resulting in reg-reg copies (if we use the pre-inc value when we 28 /// should use the post-inc value). 29 static bool IVUseShouldUsePostIncValue(Instruction *User, Instruction *IV, 30 const Loop *L, DominatorTree *DT) { 31 // If the user is in the loop, use the preinc value. 32 if (L->contains(User)) return false; 33 34 BasicBlock *LatchBlock = L->getLoopLatch(); 35 if (!LatchBlock) 36 return false; 37 38 // Ok, the user is outside of the loop. If it is dominated by the latch 39 // block, use the post-inc value. 40 if (DT->dominates(LatchBlock, User->getParent())) 41 return true; 42 43 // There is one case we have to be careful of: PHI nodes. These little guys 44 // can live in blocks that are not dominated by the latch block, but (since 45 // their uses occur in the predecessor block, not the block the PHI lives in) 46 // should still use the post-inc value. Check for this case now. 47 PHINode *PN = dyn_cast<PHINode>(User); 48 if (!PN) return false; // not a phi, not dominated by latch block. 49 50 // Look at all of the uses of IV by the PHI node. If any use corresponds to 51 // a block that is not dominated by the latch block, give up and use the 52 // preincremented value. 53 unsigned NumUses = 0; 54 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 55 if (PN->getIncomingValue(i) == IV) { 56 ++NumUses; 57 if (!DT->dominates(LatchBlock, PN->getIncomingBlock(i))) 58 return false; 59 } 60 61 // Okay, all uses of IV by PN are in predecessor blocks that really are 62 // dominated by the latch block. Use the post-incremented value. 63 return true; 64 } 65 66 const SCEV *llvm::TransformForPostIncUse(TransformKind Kind, 67 const SCEV *S, 68 Instruction *User, 69 Value *OperandValToReplace, 70 PostIncLoopSet &Loops, 71 ScalarEvolution &SE, 72 DominatorTree &DT) { 73 if (isa<SCEVConstant>(S) || isa<SCEVUnknown>(S)) 74 return S; 75 if (const SCEVCastExpr *X = dyn_cast<SCEVCastExpr>(S)) { 76 const SCEV *O = X->getOperand(); 77 const SCEV *N = TransformForPostIncUse(Kind, O, User, OperandValToReplace, 78 Loops, SE, DT); 79 if (O != N) 80 switch (S->getSCEVType()) { 81 case scZeroExtend: return SE.getZeroExtendExpr(N, S->getType()); 82 case scSignExtend: return SE.getSignExtendExpr(N, S->getType()); 83 case scTruncate: return SE.getTruncateExpr(N, S->getType()); 84 default: llvm_unreachable("Unexpected SCEVCastExpr kind!"); 85 } 86 return S; 87 } 88 if (const SCEVNAryExpr *X = dyn_cast<SCEVNAryExpr>(S)) { 89 SmallVector<const SCEV *, 8> Operands; 90 bool Changed = false; 91 for (SCEVNAryExpr::op_iterator I = X->op_begin(), E = X->op_end(); 92 I != E; ++I) { 93 const SCEV *O = *I; 94 const SCEV *N = TransformForPostIncUse(Kind, O, User, OperandValToReplace, 95 Loops, SE, DT); 96 Changed |= N != O; 97 Operands.push_back(N); 98 } 99 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { 100 // An addrec. This is the interesting part. 101 const Loop *L = AR->getLoop(); 102 const SCEV *Result = SE.getAddRecExpr(Operands, L); 103 switch (Kind) { 104 default: llvm_unreachable("Unexpected transform name!"); 105 case NormalizeAutodetect: 106 if (Instruction *OI = dyn_cast<Instruction>(OperandValToReplace)) 107 if (IVUseShouldUsePostIncValue(User, OI, L, &DT)) { 108 const SCEV *TransformedStep = 109 TransformForPostIncUse(Kind, AR->getStepRecurrence(SE), 110 User, OperandValToReplace, Loops, SE, DT); 111 Result = SE.getMinusSCEV(Result, TransformedStep); 112 Loops.insert(L); 113 } 114 break; 115 case Normalize: 116 if (Loops.count(L)) { 117 const SCEV *TransformedStep = 118 TransformForPostIncUse(Kind, AR->getStepRecurrence(SE), 119 User, OperandValToReplace, Loops, SE, DT); 120 Result = SE.getMinusSCEV(Result, TransformedStep); 121 } 122 break; 123 case Denormalize: 124 if (Loops.count(L)) 125 Result = SE.getAddExpr(Result, AR->getStepRecurrence(SE)); 126 break; 127 } 128 return Result; 129 } 130 if (Changed) 131 switch (S->getSCEVType()) { 132 case scAddExpr: return SE.getAddExpr(Operands); 133 case scMulExpr: return SE.getMulExpr(Operands); 134 case scSMaxExpr: return SE.getSMaxExpr(Operands); 135 case scUMaxExpr: return SE.getUMaxExpr(Operands); 136 default: llvm_unreachable("Unexpected SCEVNAryExpr kind!"); 137 } 138 return S; 139 } 140 if (const SCEVUDivExpr *X = dyn_cast<SCEVUDivExpr>(S)) { 141 const SCEV *LO = X->getLHS(); 142 const SCEV *RO = X->getRHS(); 143 const SCEV *LN = TransformForPostIncUse(Kind, LO, User, OperandValToReplace, 144 Loops, SE, DT); 145 const SCEV *RN = TransformForPostIncUse(Kind, RO, User, OperandValToReplace, 146 Loops, SE, DT); 147 if (LO != LN || RO != RN) 148 return SE.getUDivExpr(LN, RN); 149 return S; 150 } 151 llvm_unreachable("Unexpected SCEV kind!"); 152 return 0; 153 } 154