Initial checkin of new memory -> register promotion pass
[oota-llvm.git] / lib / Transforms / Utils / PromoteMemoryToRegister.cpp
1 //===- PromoteMemoryToRegister.cpp - Convert memory refs to regs ----------===//
2 //
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:
5 //
6 //        FROM CODE                           TO CODE
7 //   %X = alloca int, uint 1                 ret int 42
8 //   store int 42, int *%X
9 //   %Y = load int* %X
10 //   ret int %Y
11 //
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.
15 //
16 // After this, the code is transformed by...
17 //
18 //===----------------------------------------------------------------------===//
19
20 #include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
21 #include "llvm/iMemory.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Method.h"
24
25 #include "llvm/Assembly/Writer.h"  // For debugging
26
27 class PromotePass : public MethodPass {
28 public:
29   // runOnMethod - To run this pass, first we calculate the alloca instructions
30   // that are safe for promotion, then we promote each one.
31   //
32   virtual bool runOnMethod(Method *M) {
33     std::vector<AllocaInst*> Allocas;
34     findSafeAllocas(M, Allocas);      // Calculate safe allocas
35
36     // Transform each alloca in turn...
37     for (std::vector<AllocaInst*>::iterator I = Allocas.begin(),
38            E = Allocas.end(); I != E; ++I)
39       promoteAlloca(*I);
40
41     return !Allocas.empty();
42   }
43
44   // findSafeAllocas - Find allocas that are safe to promote
45   //
46   void findSafeAllocas(Method *M, std::vector<AllocaInst*> &Allocas) const;
47
48   // promoteAlloca - Convert the use chain of an alloca instruction into
49   // register references.
50   //
51   void promoteAlloca(AllocaInst *AI);
52 };
53
54
55 // findSafeAllocas - Find allocas that are safe to promote
56 //
57 void PromotePass::findSafeAllocas(Method *M,
58                                   std::vector<AllocaInst*> &Allocas) const {
59   BasicBlock *BB = M->front();  // Get the entry node for the method
60
61   // Look at all instructions in the entry node
62   for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
63     if (AllocaInst *AI = dyn_cast<AllocaInst>(*I))       // Is it an alloca?
64       if (!AI->isArrayAllocation()) {
65         bool isSafe = true;
66         for (Value::use_iterator UI = AI->use_begin(), UE = AI->use_end();
67              UI != UE; ++UI) {   // Loop over all of the uses of the alloca
68           // Only allow nonindexed memory access instructions...
69           if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(*UI)) {
70             if (MAI->hasIndices()) { isSafe = false; break; } // indexed?
71           } else {
72             isSafe = false; break;   // Not a load or store?
73           }
74         }
75
76         if (isSafe)              // If all checks pass, add alloca to safe list
77           Allocas.push_back(AI);
78       }
79
80 }
81
82 // promoteAlloca - Convert the use chain of an alloca instruction into
83 // register references.
84 //
85 void PromotePass::promoteAlloca(AllocaInst *AI) {
86   cerr << "TODO: Should process: " << AI;
87 }
88
89
90
91 Pass *newPromoteMemoryToRegister() {
92   return new PromotePass();
93 }