1 //===- PromoteMemoryToRegister.cpp - Convert memory refs to regs ----------===//
3 // This pass is used to promote memory references to be register references. A
4 // simple example of the transformation performed by this pass is:
7 // %X = alloca int, uint 1 ret int 42
8 // store int 42, int *%X
12 // To do this transformation, a simple analysis is done to ensure it is safe.
13 // Currently this just loops over all alloca instructions, looking for
14 // instructions that are only used in simple load and stores.
16 // After this, the code is transformed by...
18 //===----------------------------------------------------------------------===//
20 #include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
21 #include "llvm/Analysis/Dominators.h"
22 #include "llvm/iMemory.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Method.h"
25 #include "llvm/BasicBlock.h"
26 #include "llvm/Assembly/Writer.h" // For debugging
27 using cfg::DominanceFrontier;
29 // PromotePass - This class is implements the PromoteMemoryToRegister pass
31 class PromotePass : public MethodPass {
33 // runOnMethod - To run this pass, first we calculate the alloca instructions
34 // that are safe for promotion, then we promote each one.
36 virtual bool runOnMethod(Method *M) {
37 std::vector<AllocaInst*> Allocas;
38 findSafeAllocas(M, Allocas); // Calculate safe allocas
40 // Get dominance frontier information...
41 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
43 // Transform each alloca in turn...
44 for (std::vector<AllocaInst*>::iterator I = Allocas.begin(),
45 E = Allocas.end(); I != E; ++I)
46 promoteAlloca(*I, DF);
48 return !Allocas.empty();
52 // getAnalysisUsageInfo - We need dominance frontiers
54 virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Requires,
55 Pass::AnalysisSet &Destroyed,
56 Pass::AnalysisSet &Provided) {
57 Requires.push_back(DominanceFrontier::ID);
61 // findSafeAllocas - Find allocas that are safe to promote
63 void findSafeAllocas(Method *M, std::vector<AllocaInst*> &Allocas) const;
65 // promoteAlloca - Convert the use chain of an alloca instruction into
66 // register references.
68 void promoteAlloca(AllocaInst *AI, DominanceFrontier &DF);
72 // findSafeAllocas - Find allocas that are safe to promote
74 void PromotePass::findSafeAllocas(Method *M,
75 std::vector<AllocaInst*> &Allocas) const {
76 BasicBlock *BB = M->front(); // Get the entry node for the method
78 // Look at all instructions in the entry node
79 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
80 if (AllocaInst *AI = dyn_cast<AllocaInst>(*I)) // Is it an alloca?
81 if (!AI->isArrayAllocation()) {
83 for (Value::use_iterator UI = AI->use_begin(), UE = AI->use_end();
84 UI != UE; ++UI) { // Loop over all of the uses of the alloca
85 // Only allow nonindexed memory access instructions...
86 if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(*UI)) {
87 if (MAI->hasIndices()) { isSafe = false; break; } // indexed?
89 isSafe = false; break; // Not a load or store?
93 if (isSafe) // If all checks pass, add alloca to safe list
94 Allocas.push_back(AI);
101 // promoteAlloca - Convert the use chain of an alloca instruction into
102 // register references.
104 void PromotePass::promoteAlloca(AllocaInst *AI, DominanceFrontier &DFInfo) {
105 std::cerr << "TODO: Should process: " << AI;
109 // newPromoteMemoryToRegister - Provide an entry point to create this pass.
111 Pass *newPromoteMemoryToRegister() {
112 return new PromotePass();