| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 1 | //===------ PPCLoopPreIncPrep.cpp - Loop Pre-Inc. AM Prep. Pass -----------===// |
| 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 a pass to prepare loops for pre-increment addressing |
| 11 | // modes. Additional PHIs are created for loop induction variables used by |
| 12 | // load/store instructions so that the pre-increment forms can be used. |
| 13 | // Generically, this means transforming loops like this: |
| 14 | // for (int i = 0; i < n; ++i) |
| 15 | // array[i] = c; |
| 16 | // to look like this: |
| 17 | // T *p = array[-1]; |
| 18 | // for (int i = 0; i < n; ++i) |
| 19 | // *++p = c; |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #define DEBUG_TYPE "ppc-loop-preinc-prep" |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 23 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 24 | #include "PPC.h" |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 25 | #include "PPCSubtarget.h" |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 26 | #include "PPCTargetMachine.h" |
| Hal Finkel | ffb460f | 2015-04-11 00:33:08 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/DepthFirstIterator.h" |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallPtrSet.h" |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallSet.h" |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallVector.h" |
| Stefan Pintilie | 9495f33 | 2017-08-21 13:36:18 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/Statistic.h" |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/LoopInfo.h" |
| 33 | #include "llvm/Analysis/ScalarEvolution.h" |
| 34 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
| 35 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| David Blaikie | 2be3922 | 2018-03-21 22:34:23 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/Utils/Local.h" |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 37 | #include "llvm/IR/BasicBlock.h" |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 38 | #include "llvm/IR/CFG.h" |
| 39 | #include "llvm/IR/Dominators.h" |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 40 | #include "llvm/IR/Instruction.h" |
| 41 | #include "llvm/IR/Instructions.h" |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 42 | #include "llvm/IR/IntrinsicInst.h" |
| Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 43 | #include "llvm/IR/Module.h" |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 44 | #include "llvm/IR/Type.h" |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 45 | #include "llvm/IR/Value.h" |
| 46 | #include "llvm/Pass.h" |
| 47 | #include "llvm/Support/Casting.h" |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 48 | #include "llvm/Support/CommandLine.h" |
| 49 | #include "llvm/Support/Debug.h" |
| Chandler Carruth | 71f308a | 2015-02-13 09:09:03 +0000 | [diff] [blame] | 50 | #include "llvm/Transforms/Scalar.h" |
| David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 51 | #include "llvm/Transforms/Utils.h" |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 52 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| Hal Finkel | 291cc7b | 2015-02-07 07:32:58 +0000 | [diff] [blame] | 53 | #include "llvm/Transforms/Utils/LoopUtils.h" |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 54 | #include <cassert> |
| 55 | #include <iterator> |
| 56 | #include <utility> |
| 57 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 58 | using namespace llvm; |
| 59 | |
| 60 | // By default, we limit this to creating 16 PHIs (which is a little over half |
| 61 | // of the allocatable register set). |
| 62 | static cl::opt<unsigned> MaxVars("ppc-preinc-prep-max-vars", |
| 63 | cl::Hidden, cl::init(16), |
| 64 | cl::desc("Potential PHI threshold for PPC preinc loop prep")); |
| 65 | |
| Stefan Pintilie | 9495f33 | 2017-08-21 13:36:18 +0000 | [diff] [blame] | 66 | STATISTIC(PHINodeAlreadyExists, "PHI node already in pre-increment form"); |
| 67 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 68 | namespace llvm { |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 69 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 70 | void initializePPCLoopPreIncPrepPass(PassRegistry&); |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 71 | |
| 72 | } // end namespace llvm |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 73 | |
| 74 | namespace { |
| 75 | |
| 76 | class PPCLoopPreIncPrep : public FunctionPass { |
| 77 | public: |
| 78 | static char ID; // Pass ID, replacement for typeid |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 79 | |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 80 | PPCLoopPreIncPrep() : FunctionPass(ID) { |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 81 | initializePPCLoopPreIncPrepPass(*PassRegistry::getPassRegistry()); |
| 82 | } |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 83 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 84 | PPCLoopPreIncPrep(PPCTargetMachine &TM) : FunctionPass(ID), TM(&TM) { |
| 85 | initializePPCLoopPreIncPrepPass(*PassRegistry::getPassRegistry()); |
| 86 | } |
| 87 | |
| 88 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 89 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| 90 | AU.addRequired<LoopInfoWrapperPass>(); |
| 91 | AU.addPreserved<LoopInfoWrapperPass>(); |
| Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 92 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| Stefan Pintilie | 9495f33 | 2017-08-21 13:36:18 +0000 | [diff] [blame] | 95 | bool alreadyPrepared(Loop *L, Instruction* MemI, |
| 96 | const SCEV *BasePtrStartSCEV, |
| 97 | const SCEVConstant *BasePtrIncSCEV); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 98 | bool runOnFunction(Function &F) override; |
| 99 | |
| 100 | bool runOnLoop(Loop *L); |
| 101 | void simplifyLoopLatch(Loop *L); |
| 102 | bool rotateLoop(Loop *L); |
| 103 | |
| 104 | private: |
| Eugene Zelenko | 8187c19 | 2017-01-13 00:58:58 +0000 | [diff] [blame] | 105 | PPCTargetMachine *TM = nullptr; |
| Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 106 | DominatorTree *DT; |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 107 | LoopInfo *LI; |
| 108 | ScalarEvolution *SE; |
| Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 109 | bool PreserveLCSSA; |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 110 | }; |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 111 | |
| 112 | } // end anonymous namespace |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 113 | |
| 114 | char PPCLoopPreIncPrep::ID = 0; |
| Benjamin Kramer | 970eac4 | 2015-02-06 17:51:54 +0000 | [diff] [blame] | 115 | static const char *name = "Prepare loop for pre-inc. addressing modes"; |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 116 | INITIALIZE_PASS_BEGIN(PPCLoopPreIncPrep, DEBUG_TYPE, name, false, false) |
| 117 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
| Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 118 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 119 | INITIALIZE_PASS_END(PPCLoopPreIncPrep, DEBUG_TYPE, name, false, false) |
| 120 | |
| 121 | FunctionPass *llvm::createPPCLoopPreIncPrepPass(PPCTargetMachine &TM) { |
| 122 | return new PPCLoopPreIncPrep(TM); |
| 123 | } |
| 124 | |
| 125 | namespace { |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 126 | |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 127 | struct BucketElement { |
| 128 | BucketElement(const SCEVConstant *O, Instruction *I) : Offset(O), Instr(I) {} |
| 129 | BucketElement(Instruction *I) : Offset(nullptr), Instr(I) {} |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 130 | |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 131 | const SCEVConstant *Offset; |
| 132 | Instruction *Instr; |
| 133 | }; |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 134 | |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 135 | struct Bucket { |
| 136 | Bucket(const SCEV *B, Instruction *I) : BaseSCEV(B), |
| 137 | Elements(1, BucketElement(I)) {} |
| 138 | |
| 139 | const SCEV *BaseSCEV; |
| 140 | SmallVector<BucketElement, 16> Elements; |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 141 | }; |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 142 | |
| 143 | } // end anonymous namespace |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 144 | |
| 145 | static bool IsPtrInBounds(Value *BasePtr) { |
| 146 | Value *StrippedBasePtr = BasePtr; |
| 147 | while (BitCastInst *BC = dyn_cast<BitCastInst>(StrippedBasePtr)) |
| 148 | StrippedBasePtr = BC->getOperand(0); |
| 149 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(StrippedBasePtr)) |
| 150 | return GEP->isInBounds(); |
| 151 | |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | static Value *GetPointerOperand(Value *MemI) { |
| 156 | if (LoadInst *LMemI = dyn_cast<LoadInst>(MemI)) { |
| 157 | return LMemI->getPointerOperand(); |
| 158 | } else if (StoreInst *SMemI = dyn_cast<StoreInst>(MemI)) { |
| 159 | return SMemI->getPointerOperand(); |
| 160 | } else if (IntrinsicInst *IMemI = dyn_cast<IntrinsicInst>(MemI)) { |
| 161 | if (IMemI->getIntrinsicID() == Intrinsic::prefetch) |
| 162 | return IMemI->getArgOperand(0); |
| 163 | } |
| 164 | |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 165 | return nullptr; |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | bool PPCLoopPreIncPrep::runOnFunction(Function &F) { |
| Andrew Kaylor | 289bd5f | 2016-04-27 19:39:32 +0000 | [diff] [blame] | 169 | if (skipFunction(F)) |
| 170 | return false; |
| 171 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 172 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 173 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 174 | auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 175 | DT = DTWP ? &DTWP->getDomTree() : nullptr; |
| 176 | PreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 177 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 178 | bool MadeChange = false; |
| 179 | |
| Hal Finkel | 5551f25 | 2015-04-12 17:18:56 +0000 | [diff] [blame] | 180 | for (auto I = LI->begin(), IE = LI->end(); I != IE; ++I) |
| 181 | for (auto L = df_begin(*I), LE = df_end(*I); L != LE; ++L) |
| 182 | MadeChange |= runOnLoop(*L); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 183 | |
| 184 | return MadeChange; |
| 185 | } |
| 186 | |
| Stefan Pintilie | 9495f33 | 2017-08-21 13:36:18 +0000 | [diff] [blame] | 187 | // In order to prepare for the pre-increment a PHI is added. |
| 188 | // This function will check to see if that PHI already exists and will return |
| 189 | // true if it found an existing PHI with the same start and increment as the |
| 190 | // one we wanted to create. |
| 191 | bool PPCLoopPreIncPrep::alreadyPrepared(Loop *L, Instruction* MemI, |
| 192 | const SCEV *BasePtrStartSCEV, |
| 193 | const SCEVConstant *BasePtrIncSCEV) { |
| 194 | BasicBlock *BB = MemI->getParent(); |
| 195 | if (!BB) |
| 196 | return false; |
| 197 | |
| 198 | BasicBlock *PredBB = L->getLoopPredecessor(); |
| 199 | BasicBlock *LatchBB = L->getLoopLatch(); |
| 200 | |
| 201 | if (!PredBB || !LatchBB) |
| 202 | return false; |
| 203 | |
| 204 | // Run through the PHIs and see if we have some that looks like a preparation |
| 205 | iterator_range<BasicBlock::phi_iterator> PHIIter = BB->phis(); |
| 206 | for (auto & CurrentPHI : PHIIter) { |
| 207 | PHINode *CurrentPHINode = dyn_cast<PHINode>(&CurrentPHI); |
| 208 | if (!CurrentPHINode) |
| 209 | continue; |
| 210 | |
| 211 | if (!SE->isSCEVable(CurrentPHINode->getType())) |
| 212 | continue; |
| 213 | |
| 214 | const SCEV *PHISCEV = SE->getSCEVAtScope(CurrentPHINode, L); |
| 215 | |
| 216 | const SCEVAddRecExpr *PHIBasePtrSCEV = dyn_cast<SCEVAddRecExpr>(PHISCEV); |
| 217 | if (!PHIBasePtrSCEV) |
| 218 | continue; |
| 219 | |
| 220 | const SCEVConstant *PHIBasePtrIncSCEV = |
| 221 | dyn_cast<SCEVConstant>(PHIBasePtrSCEV->getStepRecurrence(*SE)); |
| 222 | if (!PHIBasePtrIncSCEV) |
| 223 | continue; |
| 224 | |
| 225 | if (CurrentPHINode->getNumIncomingValues() == 2) { |
| 226 | if ( (CurrentPHINode->getIncomingBlock(0) == LatchBB && |
| 227 | CurrentPHINode->getIncomingBlock(1) == PredBB) || |
| 228 | (CurrentPHINode->getIncomingBlock(1) == LatchBB && |
| 229 | CurrentPHINode->getIncomingBlock(0) == PredBB) ) { |
| 230 | if (PHIBasePtrSCEV->getStart() == BasePtrStartSCEV && |
| 231 | PHIBasePtrIncSCEV == BasePtrIncSCEV) { |
| 232 | // The existing PHI (CurrentPHINode) has the same start and increment |
| 233 | // as the PHI that we wanted to create. |
| 234 | ++PHINodeAlreadyExists; |
| 235 | return true; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | return false; |
| 241 | } |
| 242 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 243 | bool PPCLoopPreIncPrep::runOnLoop(Loop *L) { |
| 244 | bool MadeChange = false; |
| 245 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 246 | // Only prep. the inner-most loop |
| 247 | if (!L->empty()) |
| 248 | return MadeChange; |
| 249 | |
| Hal Finkel | ffb460f | 2015-04-11 00:33:08 +0000 | [diff] [blame] | 250 | DEBUG(dbgs() << "PIP: Examining: " << *L << "\n"); |
| 251 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 252 | BasicBlock *Header = L->getHeader(); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 253 | |
| 254 | const PPCSubtarget *ST = |
| 255 | TM ? TM->getSubtargetImpl(*Header->getParent()) : nullptr; |
| 256 | |
| Hal Finkel | ffb460f | 2015-04-11 00:33:08 +0000 | [diff] [blame] | 257 | unsigned HeaderLoopPredCount = |
| 258 | std::distance(pred_begin(Header), pred_end(Header)); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 259 | |
| 260 | // Collect buckets of comparable addresses used by loads and stores. |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 261 | SmallVector<Bucket, 16> Buckets; |
| 262 | for (Loop::block_iterator I = L->block_begin(), IE = L->block_end(); |
| 263 | I != IE; ++I) { |
| 264 | for (BasicBlock::iterator J = (*I)->begin(), JE = (*I)->end(); |
| 265 | J != JE; ++J) { |
| 266 | Value *PtrValue; |
| 267 | Instruction *MemI; |
| 268 | |
| 269 | if (LoadInst *LMemI = dyn_cast<LoadInst>(J)) { |
| 270 | MemI = LMemI; |
| 271 | PtrValue = LMemI->getPointerOperand(); |
| 272 | } else if (StoreInst *SMemI = dyn_cast<StoreInst>(J)) { |
| 273 | MemI = SMemI; |
| 274 | PtrValue = SMemI->getPointerOperand(); |
| 275 | } else if (IntrinsicInst *IMemI = dyn_cast<IntrinsicInst>(J)) { |
| 276 | if (IMemI->getIntrinsicID() == Intrinsic::prefetch) { |
| 277 | MemI = IMemI; |
| 278 | PtrValue = IMemI->getArgOperand(0); |
| 279 | } else continue; |
| 280 | } else continue; |
| 281 | |
| 282 | unsigned PtrAddrSpace = PtrValue->getType()->getPointerAddressSpace(); |
| 283 | if (PtrAddrSpace) |
| 284 | continue; |
| 285 | |
| 286 | // There are no update forms for Altivec vector load/stores. |
| 287 | if (ST && ST->hasAltivec() && |
| 288 | PtrValue->getType()->getPointerElementType()->isVectorTy()) |
| 289 | continue; |
| 290 | |
| 291 | if (L->isLoopInvariant(PtrValue)) |
| 292 | continue; |
| 293 | |
| Hal Finkel | ffb460f | 2015-04-11 00:33:08 +0000 | [diff] [blame] | 294 | const SCEV *LSCEV = SE->getSCEVAtScope(PtrValue, L); |
| 295 | if (const SCEVAddRecExpr *LARSCEV = dyn_cast<SCEVAddRecExpr>(LSCEV)) { |
| 296 | if (LARSCEV->getLoop() != L) |
| 297 | continue; |
| 298 | } else { |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 299 | continue; |
| Hal Finkel | ffb460f | 2015-04-11 00:33:08 +0000 | [diff] [blame] | 300 | } |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 301 | |
| 302 | bool FoundBucket = false; |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 303 | for (auto &B : Buckets) { |
| 304 | const SCEV *Diff = SE->getMinusSCEV(LSCEV, B.BaseSCEV); |
| 305 | if (const auto *CDiff = dyn_cast<SCEVConstant>(Diff)) { |
| 306 | B.Elements.push_back(BucketElement(CDiff, MemI)); |
| 307 | FoundBucket = true; |
| 308 | break; |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 309 | } |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 310 | } |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 311 | |
| 312 | if (!FoundBucket) { |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 313 | if (Buckets.size() == MaxVars) |
| 314 | return MadeChange; |
| 315 | Buckets.push_back(Bucket(LSCEV, MemI)); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 320 | if (Buckets.empty()) |
| Hal Finkel | 291cc7b | 2015-02-07 07:32:58 +0000 | [diff] [blame] | 321 | return MadeChange; |
| 322 | |
| 323 | BasicBlock *LoopPredecessor = L->getLoopPredecessor(); |
| 324 | // If there is no loop predecessor, or the loop predecessor's terminator |
| 325 | // returns a value (which might contribute to determining the loop's |
| 326 | // iteration space), insert a new preheader for the loop. |
| 327 | if (!LoopPredecessor || |
| Hal Finkel | ffb460f | 2015-04-11 00:33:08 +0000 | [diff] [blame] | 328 | !LoopPredecessor->getTerminator()->getType()->isVoidTy()) { |
| Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 329 | LoopPredecessor = InsertPreheaderForLoop(L, DT, LI, PreserveLCSSA); |
| Hal Finkel | ffb460f | 2015-04-11 00:33:08 +0000 | [diff] [blame] | 330 | if (LoopPredecessor) |
| 331 | MadeChange = true; |
| 332 | } |
| Hal Finkel | 291cc7b | 2015-02-07 07:32:58 +0000 | [diff] [blame] | 333 | if (!LoopPredecessor) |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 334 | return MadeChange; |
| 335 | |
| Hal Finkel | ffb460f | 2015-04-11 00:33:08 +0000 | [diff] [blame] | 336 | DEBUG(dbgs() << "PIP: Found " << Buckets.size() << " buckets\n"); |
| 337 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 338 | SmallSet<BasicBlock *, 16> BBChanged; |
| 339 | for (unsigned i = 0, e = Buckets.size(); i != e; ++i) { |
| 340 | // The base address of each bucket is transformed into a phi and the others |
| 341 | // are rewritten as offsets of that variable. |
| 342 | |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 343 | // We have a choice now of which instruction's memory operand we use as the |
| 344 | // base for the generated PHI. Always picking the first instruction in each |
| 345 | // bucket does not work well, specifically because that instruction might |
| 346 | // be a prefetch (and there are no pre-increment dcbt variants). Otherwise, |
| 347 | // the choice is somewhat arbitrary, because the backend will happily |
| 348 | // generate direct offsets from both the pre-incremented and |
| 349 | // post-incremented pointer values. Thus, we'll pick the first non-prefetch |
| 350 | // instruction in each bucket, and adjust the recurrence and other offsets |
| 351 | // accordingly. |
| 352 | for (int j = 0, je = Buckets[i].Elements.size(); j != je; ++j) { |
| 353 | if (auto *II = dyn_cast<IntrinsicInst>(Buckets[i].Elements[j].Instr)) |
| 354 | if (II->getIntrinsicID() == Intrinsic::prefetch) |
| 355 | continue; |
| 356 | |
| 357 | // If we'd otherwise pick the first element anyway, there's nothing to do. |
| 358 | if (j == 0) |
| 359 | break; |
| 360 | |
| 361 | // If our chosen element has no offset from the base pointer, there's |
| 362 | // nothing to do. |
| 363 | if (!Buckets[i].Elements[j].Offset || |
| 364 | Buckets[i].Elements[j].Offset->isZero()) |
| 365 | break; |
| 366 | |
| 367 | const SCEV *Offset = Buckets[i].Elements[j].Offset; |
| 368 | Buckets[i].BaseSCEV = SE->getAddExpr(Buckets[i].BaseSCEV, Offset); |
| 369 | for (auto &E : Buckets[i].Elements) { |
| 370 | if (E.Offset) |
| 371 | E.Offset = cast<SCEVConstant>(SE->getMinusSCEV(E.Offset, Offset)); |
| 372 | else |
| 373 | E.Offset = cast<SCEVConstant>(SE->getNegativeSCEV(Offset)); |
| 374 | } |
| 375 | |
| 376 | std::swap(Buckets[i].Elements[j], Buckets[i].Elements[0]); |
| 377 | break; |
| 378 | } |
| 379 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 380 | const SCEVAddRecExpr *BasePtrSCEV = |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 381 | cast<SCEVAddRecExpr>(Buckets[i].BaseSCEV); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 382 | if (!BasePtrSCEV->isAffine()) |
| 383 | continue; |
| 384 | |
| Hal Finkel | ffb460f | 2015-04-11 00:33:08 +0000 | [diff] [blame] | 385 | DEBUG(dbgs() << "PIP: Transforming: " << *BasePtrSCEV << "\n"); |
| 386 | assert(BasePtrSCEV->getLoop() == L && |
| 387 | "AddRec for the wrong loop?"); |
| 388 | |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 389 | // The instruction corresponding to the Bucket's BaseSCEV must be the first |
| 390 | // in the vector of elements. |
| 391 | Instruction *MemI = Buckets[i].Elements.begin()->Instr; |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 392 | Value *BasePtr = GetPointerOperand(MemI); |
| 393 | assert(BasePtr && "No pointer operand"); |
| 394 | |
| David Blaikie | c95c3ae | 2015-03-14 21:20:51 +0000 | [diff] [blame] | 395 | Type *I8Ty = Type::getInt8Ty(MemI->getParent()->getContext()); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 396 | Type *I8PtrTy = Type::getInt8PtrTy(MemI->getParent()->getContext(), |
| 397 | BasePtr->getType()->getPointerAddressSpace()); |
| 398 | |
| 399 | const SCEV *BasePtrStartSCEV = BasePtrSCEV->getStart(); |
| 400 | if (!SE->isLoopInvariant(BasePtrStartSCEV, L)) |
| 401 | continue; |
| 402 | |
| 403 | const SCEVConstant *BasePtrIncSCEV = |
| 404 | dyn_cast<SCEVConstant>(BasePtrSCEV->getStepRecurrence(*SE)); |
| 405 | if (!BasePtrIncSCEV) |
| 406 | continue; |
| 407 | BasePtrStartSCEV = SE->getMinusSCEV(BasePtrStartSCEV, BasePtrIncSCEV); |
| 408 | if (!isSafeToExpand(BasePtrStartSCEV, *SE)) |
| 409 | continue; |
| 410 | |
| Hal Finkel | ffb460f | 2015-04-11 00:33:08 +0000 | [diff] [blame] | 411 | DEBUG(dbgs() << "PIP: New start is: " << *BasePtrStartSCEV << "\n"); |
| 412 | |
| Stefan Pintilie | 9495f33 | 2017-08-21 13:36:18 +0000 | [diff] [blame] | 413 | if (alreadyPrepared(L, MemI, BasePtrStartSCEV, BasePtrIncSCEV)) |
| 414 | continue; |
| 415 | |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 416 | PHINode *NewPHI = PHINode::Create(I8PtrTy, HeaderLoopPredCount, |
| 417 | MemI->hasName() ? MemI->getName() + ".phi" : "", |
| 418 | Header->getFirstNonPHI()); |
| 419 | |
| Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 420 | SCEVExpander SCEVE(*SE, Header->getModule()->getDataLayout(), "pistart"); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 421 | Value *BasePtrStart = SCEVE.expandCodeFor(BasePtrStartSCEV, I8PtrTy, |
| 422 | LoopPredecessor->getTerminator()); |
| 423 | |
| 424 | // Note that LoopPredecessor might occur in the predecessor list multiple |
| 425 | // times, and we need to add it the right number of times. |
| 426 | for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header); |
| 427 | PI != PE; ++PI) { |
| 428 | if (*PI != LoopPredecessor) |
| 429 | continue; |
| 430 | |
| 431 | NewPHI->addIncoming(BasePtrStart, LoopPredecessor); |
| 432 | } |
| 433 | |
| Duncan P. N. Exon Smith | ac65b4c | 2015-10-20 01:07:37 +0000 | [diff] [blame] | 434 | Instruction *InsPoint = &*Header->getFirstInsertionPt(); |
| David Blaikie | c95c3ae | 2015-03-14 21:20:51 +0000 | [diff] [blame] | 435 | GetElementPtrInst *PtrInc = GetElementPtrInst::Create( |
| 436 | I8Ty, NewPHI, BasePtrIncSCEV->getValue(), |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 437 | MemI->hasName() ? MemI->getName() + ".inc" : "", InsPoint); |
| 438 | PtrInc->setIsInBounds(IsPtrInBounds(BasePtr)); |
| 439 | for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header); |
| 440 | PI != PE; ++PI) { |
| 441 | if (*PI == LoopPredecessor) |
| 442 | continue; |
| 443 | |
| 444 | NewPHI->addIncoming(PtrInc, *PI); |
| 445 | } |
| 446 | |
| 447 | Instruction *NewBasePtr; |
| 448 | if (PtrInc->getType() != BasePtr->getType()) |
| 449 | NewBasePtr = new BitCastInst(PtrInc, BasePtr->getType(), |
| 450 | PtrInc->hasName() ? PtrInc->getName() + ".cast" : "", InsPoint); |
| 451 | else |
| 452 | NewBasePtr = PtrInc; |
| 453 | |
| 454 | if (Instruction *IDel = dyn_cast<Instruction>(BasePtr)) |
| 455 | BBChanged.insert(IDel->getParent()); |
| 456 | BasePtr->replaceAllUsesWith(NewBasePtr); |
| 457 | RecursivelyDeleteTriviallyDeadInstructions(BasePtr); |
| 458 | |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 459 | // Keep track of the replacement pointer values we've inserted so that we |
| 460 | // don't generate more pointer values than necessary. |
| 461 | SmallPtrSet<Value *, 16> NewPtrs; |
| 462 | NewPtrs.insert( NewBasePtr); |
| 463 | |
| 464 | for (auto I = std::next(Buckets[i].Elements.begin()), |
| 465 | IE = Buckets[i].Elements.end(); I != IE; ++I) { |
| 466 | Value *Ptr = GetPointerOperand(I->Instr); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 467 | assert(Ptr && "No pointer operand"); |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 468 | if (NewPtrs.count(Ptr)) |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 469 | continue; |
| 470 | |
| 471 | Instruction *RealNewPtr; |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 472 | if (!I->Offset || I->Offset->getValue()->isZero()) { |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 473 | RealNewPtr = NewBasePtr; |
| 474 | } else { |
| 475 | Instruction *PtrIP = dyn_cast<Instruction>(Ptr); |
| 476 | if (PtrIP && isa<Instruction>(NewBasePtr) && |
| 477 | cast<Instruction>(NewBasePtr)->getParent() == PtrIP->getParent()) |
| Eugene Zelenko | 2bc2f33 | 2016-12-09 22:06:55 +0000 | [diff] [blame] | 478 | PtrIP = nullptr; |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 479 | else if (isa<PHINode>(PtrIP)) |
| Duncan P. N. Exon Smith | ac65b4c | 2015-10-20 01:07:37 +0000 | [diff] [blame] | 480 | PtrIP = &*PtrIP->getParent()->getFirstInsertionPt(); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 481 | else if (!PtrIP) |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 482 | PtrIP = I->Instr; |
| David Blaikie | c95c3ae | 2015-03-14 21:20:51 +0000 | [diff] [blame] | 483 | |
| 484 | GetElementPtrInst *NewPtr = GetElementPtrInst::Create( |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 485 | I8Ty, PtrInc, I->Offset->getValue(), |
| 486 | I->Instr->hasName() ? I->Instr->getName() + ".off" : "", PtrIP); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 487 | if (!PtrIP) |
| 488 | NewPtr->insertAfter(cast<Instruction>(PtrInc)); |
| 489 | NewPtr->setIsInBounds(IsPtrInBounds(Ptr)); |
| 490 | RealNewPtr = NewPtr; |
| 491 | } |
| 492 | |
| 493 | if (Instruction *IDel = dyn_cast<Instruction>(Ptr)) |
| 494 | BBChanged.insert(IDel->getParent()); |
| 495 | |
| 496 | Instruction *ReplNewPtr; |
| 497 | if (Ptr->getType() != RealNewPtr->getType()) { |
| 498 | ReplNewPtr = new BitCastInst(RealNewPtr, Ptr->getType(), |
| 499 | Ptr->hasName() ? Ptr->getName() + ".cast" : ""); |
| 500 | ReplNewPtr->insertAfter(RealNewPtr); |
| 501 | } else |
| 502 | ReplNewPtr = RealNewPtr; |
| 503 | |
| 504 | Ptr->replaceAllUsesWith(ReplNewPtr); |
| 505 | RecursivelyDeleteTriviallyDeadInstructions(Ptr); |
| 506 | |
| Hal Finkel | f046f72 | 2015-11-08 08:04:40 +0000 | [diff] [blame] | 507 | NewPtrs.insert(RealNewPtr); |
| Hal Finkel | c9dd020 | 2015-02-05 18:43:00 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | MadeChange = true; |
| 511 | } |
| 512 | |
| 513 | for (Loop::block_iterator I = L->block_begin(), IE = L->block_end(); |
| 514 | I != IE; ++I) { |
| 515 | if (BBChanged.count(*I)) |
| 516 | DeleteDeadPHIs(*I); |
| 517 | } |
| 518 | |
| 519 | return MadeChange; |
| 520 | } |