PromoteMemoryToRegister.cpp revision 17e6e44298d8da4cd97ba0be66225cdad4670276
1//===- PromoteMemoryToRegister.cpp - Convert allocas to registers ---------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file promote memory references to be register references. It promotes 11// alloca instructions which only have loads and stores as uses. An alloca is 12// transformed by using dominator frontiers to place PHI nodes, then traversing 13// the function in depth-first order to rewrite loads and stores as appropriate. 14// This is just the standard SSA construction algorithm to construct "pruned" 15// SSA form. 16// 17//===----------------------------------------------------------------------===// 18 19#include "llvm/Transforms/Utils/PromoteMemToReg.h" 20#include "llvm/Constants.h" 21#include "llvm/DerivedTypes.h" 22#include "llvm/Function.h" 23#include "llvm/Instructions.h" 24#include "llvm/Analysis/Dominators.h" 25#include "llvm/Analysis/AliasSetTracker.h" 26#include "llvm/ADT/StringExtras.h" 27#include "llvm/Transforms/Utils/Local.h" 28#include "llvm/Support/CFG.h" 29#include "llvm/Support/StableBasicBlockNumbering.h" 30#include <algorithm> 31using namespace llvm; 32 33/// isAllocaPromotable - Return true if this alloca is legal for promotion. 34/// This is true if there are only loads and stores to the alloca. 35/// 36bool llvm::isAllocaPromotable(const AllocaInst *AI, const TargetData &TD) { 37 // FIXME: If the memory unit is of pointer or integer type, we can permit 38 // assignments to subsections of the memory unit. 39 40 // Only allow direct loads and stores... 41 for (Value::use_const_iterator UI = AI->use_begin(), UE = AI->use_end(); 42 UI != UE; ++UI) // Loop over all of the uses of the alloca 43 if (isa<LoadInst>(*UI)) { 44 // noop 45 } else if (const StoreInst *SI = dyn_cast<StoreInst>(*UI)) { 46 if (SI->getOperand(0) == AI) 47 return false; // Don't allow a store OF the AI, only INTO the AI. 48 } else { 49 return false; // Not a load or store. 50 } 51 52 return true; 53} 54 55namespace { 56 struct PromoteMem2Reg { 57 /// Allocas - The alloca instructions being promoted. 58 /// 59 std::vector<AllocaInst*> Allocas; 60 DominatorTree &DT; 61 DominanceFrontier &DF; 62 const TargetData &TD; 63 64 /// AST - An AliasSetTracker object to update. If null, don't update it. 65 /// 66 AliasSetTracker *AST; 67 68 /// AllocaLookup - Reverse mapping of Allocas. 69 /// 70 std::map<AllocaInst*, unsigned> AllocaLookup; 71 72 /// NewPhiNodes - The PhiNodes we're adding. 73 /// 74 std::map<BasicBlock*, std::vector<PHINode*> > NewPhiNodes; 75 76 /// PointerAllocaValues - If we are updating an AliasSetTracker, then for 77 /// each alloca that is of pointer type, we keep track of what to copyValue 78 /// to the inserted PHI nodes here. 79 /// 80 std::vector<Value*> PointerAllocaValues; 81 82 /// Visited - The set of basic blocks the renamer has already visited. 83 /// 84 std::set<BasicBlock*> Visited; 85 86 /// BBNumbers - Contains a stable numbering of basic blocks to avoid 87 /// non-determinstic behavior. 88 StableBasicBlockNumbering BBNumbers; 89 90 public: 91 PromoteMem2Reg(const std::vector<AllocaInst*> &A, DominatorTree &dt, 92 DominanceFrontier &df, const TargetData &td, 93 AliasSetTracker *ast) 94 : Allocas(A), DT(dt), DF(df), TD(td), AST(ast) {} 95 96 void run(); 97 98 /// dominates - Return true if I1 dominates I2 using the DominatorTree. 99 /// 100 bool dominates(Instruction *I1, Instruction *I2) const { 101 if (InvokeInst *II = dyn_cast<InvokeInst>(I1)) 102 I1 = II->getNormalDest()->begin(); 103 return DT[I1->getParent()]->dominates(DT[I2->getParent()]); 104 } 105 106 private: 107 void MarkDominatingPHILive(BasicBlock *BB, unsigned AllocaNum, 108 std::set<PHINode*> &DeadPHINodes); 109 void PromoteLocallyUsedAlloca(BasicBlock *BB, AllocaInst *AI); 110 void PromoteLocallyUsedAllocas(BasicBlock *BB, 111 const std::vector<AllocaInst*> &AIs); 112 113 void RenamePass(BasicBlock *BB, BasicBlock *Pred, 114 std::vector<Value*> &IncVals); 115 bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx, unsigned &Version, 116 std::set<PHINode*> &InsertedPHINodes); 117 }; 118} // end of anonymous namespace 119 120void PromoteMem2Reg::run() { 121 Function &F = *DF.getRoot()->getParent(); 122 123 // LocallyUsedAllocas - Keep track of all of the alloca instructions which are 124 // only used in a single basic block. These instructions can be efficiently 125 // promoted by performing a single linear scan over that one block. Since 126 // individual basic blocks are sometimes large, we group together all allocas 127 // that are live in a single basic block by the basic block they are live in. 128 std::map<BasicBlock*, std::vector<AllocaInst*> > LocallyUsedAllocas; 129 130 if (AST) PointerAllocaValues.resize(Allocas.size()); 131 132 for (unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) { 133 AllocaInst *AI = Allocas[AllocaNum]; 134 135 assert(isAllocaPromotable(AI, TD) && 136 "Cannot promote non-promotable alloca!"); 137 assert(AI->getParent()->getParent() == &F && 138 "All allocas should be in the same function, which is same as DF!"); 139 140 if (AI->use_empty()) { 141 // If there are no uses of the alloca, just delete it now. 142 if (AST) AST->deleteValue(AI); 143 AI->getParent()->getInstList().erase(AI); 144 145 // Remove the alloca from the Allocas list, since it has been processed 146 Allocas[AllocaNum] = Allocas.back(); 147 Allocas.pop_back(); 148 --AllocaNum; 149 continue; 150 } 151 152 // Calculate the set of read and write-locations for each alloca. This is 153 // analogous to finding the 'uses' and 'definitions' of each variable. 154 std::vector<BasicBlock*> DefiningBlocks; 155 std::vector<BasicBlock*> UsingBlocks; 156 157 BasicBlock *OnlyBlock = 0; 158 bool OnlyUsedInOneBlock = true; 159 160 // As we scan the uses of the alloca instruction, keep track of stores, and 161 // decide whether all of the loads and stores to the alloca are within the 162 // same basic block. 163 Value *AllocaPointerVal = 0; 164 for (Value::use_iterator U =AI->use_begin(), E = AI->use_end(); U != E;++U){ 165 Instruction *User = cast<Instruction>(*U); 166 if (StoreInst *SI = dyn_cast<StoreInst>(User)) { 167 // Remember the basic blocks which define new values for the alloca 168 DefiningBlocks.push_back(SI->getParent()); 169 AllocaPointerVal = SI->getOperand(0); 170 } else if (LoadInst *LI = dyn_cast<LoadInst>(User)) { 171 // Otherwise it must be a load instruction, keep track of variable reads 172 UsingBlocks.push_back(LI->getParent()); 173 AllocaPointerVal = LI; 174 } 175 176 if (OnlyUsedInOneBlock) { 177 if (OnlyBlock == 0) 178 OnlyBlock = User->getParent(); 179 else if (OnlyBlock != User->getParent()) 180 OnlyUsedInOneBlock = false; 181 } 182 } 183 184 // If the alloca is only read and written in one basic block, just perform a 185 // linear sweep over the block to eliminate it. 186 if (OnlyUsedInOneBlock) { 187 LocallyUsedAllocas[OnlyBlock].push_back(AI); 188 189 // Remove the alloca from the Allocas list, since it will be processed. 190 Allocas[AllocaNum] = Allocas.back(); 191 Allocas.pop_back(); 192 --AllocaNum; 193 continue; 194 } 195 196 if (AST) 197 PointerAllocaValues[AllocaNum] = AllocaPointerVal; 198 199 // If we haven't computed a numbering for the BB's in the function, do so 200 // now. 201 BBNumbers.compute(F); 202 203 // Compute the locations where PhiNodes need to be inserted. Look at the 204 // dominance frontier of EACH basic-block we have a write in. 205 // 206 unsigned CurrentVersion = 0; 207 std::set<PHINode*> InsertedPHINodes; 208 std::vector<unsigned> DFBlocks; 209 while (!DefiningBlocks.empty()) { 210 BasicBlock *BB = DefiningBlocks.back(); 211 DefiningBlocks.pop_back(); 212 213 // Look up the DF for this write, add it to PhiNodes 214 DominanceFrontier::const_iterator it = DF.find(BB); 215 if (it != DF.end()) { 216 const DominanceFrontier::DomSetType &S = it->second; 217 218 // In theory we don't need the indirection through the DFBlocks vector. 219 // In practice, the order of calling QueuePhiNode would depend on the 220 // (unspecified) ordering of basic blocks in the dominance frontier, 221 // which would give PHI nodes non-determinstic subscripts. Fix this by 222 // processing blocks in order of the occurance in the function. 223 for (DominanceFrontier::DomSetType::const_iterator P = S.begin(), 224 PE = S.end(); P != PE; ++P) 225 DFBlocks.push_back(BBNumbers.getNumber(*P)); 226 227 // Sort by which the block ordering in the function. 228 std::sort(DFBlocks.begin(), DFBlocks.end()); 229 230 for (unsigned i = 0, e = DFBlocks.size(); i != e; ++i) { 231 BasicBlock *BB = BBNumbers.getBlock(DFBlocks[i]); 232 if (QueuePhiNode(BB, AllocaNum, CurrentVersion, InsertedPHINodes)) 233 DefiningBlocks.push_back(BB); 234 } 235 DFBlocks.clear(); 236 } 237 } 238 239 // Now that we have inserted PHI nodes along the Iterated Dominance Frontier 240 // of the writes to the variable, scan through the reads of the variable, 241 // marking PHI nodes which are actually necessary as alive (by removing them 242 // from the InsertedPHINodes set). This is not perfect: there may PHI 243 // marked alive because of loads which are dominated by stores, but there 244 // will be no unmarked PHI nodes which are actually used. 245 // 246 for (unsigned i = 0, e = UsingBlocks.size(); i != e; ++i) 247 MarkDominatingPHILive(UsingBlocks[i], AllocaNum, InsertedPHINodes); 248 UsingBlocks.clear(); 249 250 // If there are any PHI nodes which are now known to be dead, remove them! 251 for (std::set<PHINode*>::iterator I = InsertedPHINodes.begin(), 252 E = InsertedPHINodes.end(); I != E; ++I) { 253 PHINode *PN = *I; 254 std::vector<PHINode*> &BBPNs = NewPhiNodes[PN->getParent()]; 255 BBPNs[AllocaNum] = 0; 256 257 // Check to see if we just removed the last inserted PHI node from this 258 // basic block. If so, remove the entry for the basic block. 259 bool HasOtherPHIs = false; 260 for (unsigned i = 0, e = BBPNs.size(); i != e; ++i) 261 if (BBPNs[i]) { 262 HasOtherPHIs = true; 263 break; 264 } 265 if (!HasOtherPHIs) 266 NewPhiNodes.erase(PN->getParent()); 267 268 if (AST && isa<PointerType>(PN->getType())) 269 AST->deleteValue(PN); 270 PN->getParent()->getInstList().erase(PN); 271 } 272 273 // Keep the reverse mapping of the 'Allocas' array. 274 AllocaLookup[Allocas[AllocaNum]] = AllocaNum; 275 } 276 277 // Process all allocas which are only used in a single basic block. 278 for (std::map<BasicBlock*, std::vector<AllocaInst*> >::iterator I = 279 LocallyUsedAllocas.begin(), E = LocallyUsedAllocas.end(); I != E; ++I){ 280 const std::vector<AllocaInst*> &Allocas = I->second; 281 assert(!Allocas.empty() && "empty alloca list??"); 282 283 // It's common for there to only be one alloca in the list. Handle it 284 // efficiently. 285 if (Allocas.size() == 1) 286 PromoteLocallyUsedAlloca(I->first, Allocas[0]); 287 else 288 PromoteLocallyUsedAllocas(I->first, Allocas); 289 } 290 291 if (Allocas.empty()) 292 return; // All of the allocas must have been trivial! 293 294 // Set the incoming values for the basic block to be null values for all of 295 // the alloca's. We do this in case there is a load of a value that has not 296 // been stored yet. In this case, it will get this null value. 297 // 298 std::vector<Value *> Values(Allocas.size()); 299 for (unsigned i = 0, e = Allocas.size(); i != e; ++i) 300 Values[i] = UndefValue::get(Allocas[i]->getAllocatedType()); 301 302 // Walks all basic blocks in the function performing the SSA rename algorithm 303 // and inserting the phi nodes we marked as necessary 304 // 305 RenamePass(F.begin(), 0, Values); 306 307 // The renamer uses the Visited set to avoid infinite loops. Clear it now. 308 Visited.clear(); 309 310 // Remove the allocas themselves from the function... 311 for (unsigned i = 0, e = Allocas.size(); i != e; ++i) { 312 Instruction *A = Allocas[i]; 313 314 // If there are any uses of the alloca instructions left, they must be in 315 // sections of dead code that were not processed on the dominance frontier. 316 // Just delete the users now. 317 // 318 if (!A->use_empty()) 319 A->replaceAllUsesWith(UndefValue::get(A->getType())); 320 if (AST) AST->deleteValue(A); 321 A->getParent()->getInstList().erase(A); 322 } 323 324 // At this point, the renamer has added entries to PHI nodes for all reachable 325 // code. Unfortunately, there may be blocks which are not reachable, which 326 // the renamer hasn't traversed. If this is the case, the PHI nodes may not 327 // have incoming values for all predecessors. Loop over all PHI nodes we have 328 // created, inserting undef values if they are missing any incoming values. 329 // 330 for (std::map<BasicBlock*, std::vector<PHINode *> >::iterator I = 331 NewPhiNodes.begin(), E = NewPhiNodes.end(); I != E; ++I) { 332 333 std::vector<BasicBlock*> Preds(pred_begin(I->first), pred_end(I->first)); 334 std::vector<PHINode*> &PNs = I->second; 335 assert(!PNs.empty() && "Empty PHI node list??"); 336 337 // Loop over all of the PHI nodes and see if there are any that we can get 338 // rid of because they merge all of the same incoming values. This can 339 // happen due to undef values coming into the PHI nodes. 340 PHINode *SomePHI = 0; 341 for (unsigned i = 0, e = PNs.size(); i != e; ++i) 342 if (PNs[i]) { 343 if (Value *V = hasConstantValue(PNs[i])) { 344 if (!isa<Instruction>(V) || dominates(cast<Instruction>(V), PNs[i])) { 345 PNs[i]->replaceAllUsesWith(V); 346 PNs[i]->eraseFromParent(); 347 PNs[i] = 0; 348 } 349 } 350 if (PNs[i]) 351 SomePHI = PNs[i]; 352 } 353 354 // Only do work here if there the PHI nodes are missing incoming values. We 355 // know that all PHI nodes that were inserted in a block will have the same 356 // number of incoming values, so we can just check any PHI node. 357 if (SomePHI && Preds.size() != SomePHI->getNumIncomingValues()) { 358 // Ok, now we know that all of the PHI nodes are missing entries for some 359 // basic blocks. Start by sorting the incoming predecessors for efficient 360 // access. 361 std::sort(Preds.begin(), Preds.end()); 362 363 // Now we loop through all BB's which have entries in SomePHI and remove 364 // them from the Preds list. 365 for (unsigned i = 0, e = SomePHI->getNumIncomingValues(); i != e; ++i) { 366 // Do a log(n) search of the Preds list for the entry we want. 367 std::vector<BasicBlock*>::iterator EntIt = 368 std::lower_bound(Preds.begin(), Preds.end(), 369 SomePHI->getIncomingBlock(i)); 370 assert(EntIt != Preds.end() && *EntIt == SomePHI->getIncomingBlock(i)&& 371 "PHI node has entry for a block which is not a predecessor!"); 372 373 // Remove the entry 374 Preds.erase(EntIt); 375 } 376 377 // At this point, the blocks left in the preds list must have dummy 378 // entries inserted into every PHI nodes for the block. 379 for (unsigned i = 0, e = PNs.size(); i != e; ++i) 380 if (PHINode *PN = PNs[i]) { 381 Value *UndefVal = UndefValue::get(PN->getType()); 382 for (unsigned pred = 0, e = Preds.size(); pred != e; ++pred) 383 PN->addIncoming(UndefVal, Preds[pred]); 384 } 385 } 386 } 387} 388 389// MarkDominatingPHILive - Mem2Reg wants to construct "pruned" SSA form, not 390// "minimal" SSA form. To do this, it inserts all of the PHI nodes on the IDF 391// as usual (inserting the PHI nodes in the DeadPHINodes set), then processes 392// each read of the variable. For each block that reads the variable, this 393// function is called, which removes used PHI nodes from the DeadPHINodes set. 394// After all of the reads have been processed, any PHI nodes left in the 395// DeadPHINodes set are removed. 396// 397void PromoteMem2Reg::MarkDominatingPHILive(BasicBlock *BB, unsigned AllocaNum, 398 std::set<PHINode*> &DeadPHINodes) { 399 // Scan the immediate dominators of this block looking for a block which has a 400 // PHI node for Alloca num. If we find it, mark the PHI node as being alive! 401 for (DominatorTree::Node *N = DT[BB]; N; N = N->getIDom()) { 402 BasicBlock *DomBB = N->getBlock(); 403 std::map<BasicBlock*, std::vector<PHINode*> >::iterator 404 I = NewPhiNodes.find(DomBB); 405 if (I != NewPhiNodes.end() && I->second[AllocaNum]) { 406 // Ok, we found an inserted PHI node which dominates this value. 407 PHINode *DominatingPHI = I->second[AllocaNum]; 408 409 // Find out if we previously thought it was dead. 410 std::set<PHINode*>::iterator DPNI = DeadPHINodes.find(DominatingPHI); 411 if (DPNI != DeadPHINodes.end()) { 412 // Ok, until now, we thought this PHI node was dead. Mark it as being 413 // alive/needed. 414 DeadPHINodes.erase(DPNI); 415 416 // Now that we have marked the PHI node alive, also mark any PHI nodes 417 // which it might use as being alive as well. 418 for (pred_iterator PI = pred_begin(DomBB), PE = pred_end(DomBB); 419 PI != PE; ++PI) 420 MarkDominatingPHILive(*PI, AllocaNum, DeadPHINodes); 421 } 422 } 423 } 424} 425 426/// PromoteLocallyUsedAlloca - Many allocas are only used within a single basic 427/// block. If this is the case, avoid traversing the CFG and inserting a lot of 428/// potentially useless PHI nodes by just performing a single linear pass over 429/// the basic block using the Alloca. 430/// 431void PromoteMem2Reg::PromoteLocallyUsedAlloca(BasicBlock *BB, AllocaInst *AI) { 432 assert(!AI->use_empty() && "There are no uses of the alloca!"); 433 434 // Handle degenerate cases quickly. 435 if (AI->hasOneUse()) { 436 Instruction *U = cast<Instruction>(AI->use_back()); 437 if (LoadInst *LI = dyn_cast<LoadInst>(U)) { 438 // Must be a load of uninitialized value. 439 LI->replaceAllUsesWith(UndefValue::get(AI->getAllocatedType())); 440 if (AST && isa<PointerType>(LI->getType())) 441 AST->deleteValue(LI); 442 } else { 443 // Otherwise it must be a store which is never read. 444 assert(isa<StoreInst>(U)); 445 } 446 BB->getInstList().erase(U); 447 } else { 448 // Uses of the uninitialized memory location shall get undef. 449 Value *CurVal = UndefValue::get(AI->getAllocatedType()); 450 451 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { 452 Instruction *Inst = I++; 453 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { 454 if (LI->getOperand(0) == AI) { 455 // Loads just returns the "current value"... 456 LI->replaceAllUsesWith(CurVal); 457 if (AST && isa<PointerType>(LI->getType())) 458 AST->deleteValue(LI); 459 BB->getInstList().erase(LI); 460 } 461 } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 462 if (SI->getOperand(1) == AI) { 463 // Store updates the "current value"... 464 CurVal = SI->getOperand(0); 465 BB->getInstList().erase(SI); 466 } 467 } 468 } 469 } 470 471 // After traversing the basic block, there should be no more uses of the 472 // alloca, remove it now. 473 assert(AI->use_empty() && "Uses of alloca from more than one BB??"); 474 if (AST) AST->deleteValue(AI); 475 AI->getParent()->getInstList().erase(AI); 476} 477 478/// PromoteLocallyUsedAllocas - This method is just like 479/// PromoteLocallyUsedAlloca, except that it processes multiple alloca 480/// instructions in parallel. This is important in cases where we have large 481/// basic blocks, as we don't want to rescan the entire basic block for each 482/// alloca which is locally used in it (which might be a lot). 483void PromoteMem2Reg:: 484PromoteLocallyUsedAllocas(BasicBlock *BB, const std::vector<AllocaInst*> &AIs) { 485 std::map<AllocaInst*, Value*> CurValues; 486 for (unsigned i = 0, e = AIs.size(); i != e; ++i) 487 CurValues[AIs[i]] = 0; // Insert with null value 488 489 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { 490 Instruction *Inst = I++; 491 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { 492 // Is this a load of an alloca we are tracking? 493 if (AllocaInst *AI = dyn_cast<AllocaInst>(LI->getOperand(0))) { 494 std::map<AllocaInst*, Value*>::iterator AIt = CurValues.find(AI); 495 if (AIt != CurValues.end()) { 496 // Loads just returns the "current value"... 497 if (AIt->second == 0) // Uninitialized value?? 498 AIt->second = UndefValue::get(AIt->first->getAllocatedType()); 499 LI->replaceAllUsesWith(AIt->second); 500 if (AST && isa<PointerType>(LI->getType())) 501 AST->deleteValue(LI); 502 BB->getInstList().erase(LI); 503 } 504 } 505 } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 506 if (AllocaInst *AI = dyn_cast<AllocaInst>(SI->getOperand(1))) { 507 std::map<AllocaInst*, Value*>::iterator AIt = CurValues.find(AI); 508 if (AIt != CurValues.end()) { 509 // Store updates the "current value"... 510 AIt->second = SI->getOperand(0); 511 BB->getInstList().erase(SI); 512 } 513 } 514 } 515 } 516} 517 518 519 520// QueuePhiNode - queues a phi-node to be added to a basic-block for a specific 521// Alloca returns true if there wasn't already a phi-node for that variable 522// 523bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo, 524 unsigned &Version, 525 std::set<PHINode*> &InsertedPHINodes) { 526 // Look up the basic-block in question. 527 std::vector<PHINode*> &BBPNs = NewPhiNodes[BB]; 528 if (BBPNs.empty()) BBPNs.resize(Allocas.size()); 529 530 // If the BB already has a phi node added for the i'th alloca then we're done! 531 if (BBPNs[AllocaNo]) return false; 532 533 // Create a PhiNode using the dereferenced type... and add the phi-node to the 534 // BasicBlock. 535 PHINode *PN = new PHINode(Allocas[AllocaNo]->getAllocatedType(), 536 Allocas[AllocaNo]->getName() + "." + 537 utostr(Version++), BB->begin()); 538 BBPNs[AllocaNo] = PN; 539 InsertedPHINodes.insert(PN); 540 541 if (AST && isa<PointerType>(PN->getType())) 542 AST->copyValue(PointerAllocaValues[AllocaNo], PN); 543 544 return true; 545} 546 547 548// RenamePass - Recursively traverse the CFG of the function, renaming loads and 549// stores to the allocas which we are promoting. IncomingVals indicates what 550// value each Alloca contains on exit from the predecessor block Pred. 551// 552void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred, 553 std::vector<Value*> &IncomingVals) { 554 555 // If this BB needs a PHI node, update the PHI node for each variable we need 556 // PHI nodes for. 557 std::map<BasicBlock*, std::vector<PHINode *> >::iterator 558 BBPNI = NewPhiNodes.find(BB); 559 if (BBPNI != NewPhiNodes.end()) { 560 std::vector<PHINode *> &BBPNs = BBPNI->second; 561 for (unsigned k = 0; k != BBPNs.size(); ++k) 562 if (PHINode *PN = BBPNs[k]) { 563 // Add this incoming value to the PHI node. 564 PN->addIncoming(IncomingVals[k], Pred); 565 566 // The currently active variable for this block is now the PHI. 567 IncomingVals[k] = PN; 568 } 569 } 570 571 // don't revisit nodes 572 if (Visited.count(BB)) return; 573 574 // mark as visited 575 Visited.insert(BB); 576 577 for (BasicBlock::iterator II = BB->begin(); !isa<TerminatorInst>(II); ) { 578 Instruction *I = II++; // get the instruction, increment iterator 579 580 if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 581 if (AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand())) { 582 std::map<AllocaInst*, unsigned>::iterator AI = AllocaLookup.find(Src); 583 if (AI != AllocaLookup.end()) { 584 Value *V = IncomingVals[AI->second]; 585 586 // walk the use list of this load and replace all uses with r 587 LI->replaceAllUsesWith(V); 588 if (AST && isa<PointerType>(LI->getType())) 589 AST->deleteValue(LI); 590 BB->getInstList().erase(LI); 591 } 592 } 593 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 594 // Delete this instruction and mark the name as the current holder of the 595 // value 596 if (AllocaInst *Dest = dyn_cast<AllocaInst>(SI->getPointerOperand())) { 597 std::map<AllocaInst *, unsigned>::iterator ai = AllocaLookup.find(Dest); 598 if (ai != AllocaLookup.end()) { 599 // what value were we writing? 600 IncomingVals[ai->second] = SI->getOperand(0); 601 BB->getInstList().erase(SI); 602 } 603 } 604 } 605 } 606 607 // Recurse to our successors. 608 TerminatorInst *TI = BB->getTerminator(); 609 for (unsigned i = 0; i != TI->getNumSuccessors(); i++) { 610 std::vector<Value*> OutgoingVals(IncomingVals); 611 RenamePass(TI->getSuccessor(i), BB, OutgoingVals); 612 } 613} 614 615/// PromoteMemToReg - Promote the specified list of alloca instructions into 616/// scalar registers, inserting PHI nodes as appropriate. This function makes 617/// use of DominanceFrontier information. This function does not modify the CFG 618/// of the function at all. All allocas must be from the same function. 619/// 620/// If AST is specified, the specified tracker is updated to reflect changes 621/// made to the IR. 622/// 623void llvm::PromoteMemToReg(const std::vector<AllocaInst*> &Allocas, 624 DominatorTree &DT, DominanceFrontier &DF, 625 const TargetData &TD, AliasSetTracker *AST) { 626 // If there is nothing to do, bail out... 627 if (Allocas.empty()) return; 628 PromoteMem2Reg(Allocas, DT, DF, TD, AST).run(); 629} 630