Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 1 | //===-- PoolAllocate.cpp - Pool Allocation Pass ---------------------------===// |
| 2 | // |
| 3 | // This transform changes programs so that disjoint data structures are |
| 4 | // allocated out of different pools of memory, increasing locality and shrinking |
| 5 | // pointer size. |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Transforms/IPO/PoolAllocate.h" |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/CloneFunction.h" |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 11 | #include "llvm/Analysis/DataStructure.h" |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 12 | #include "llvm/Analysis/DataStructureGraph.h" |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 13 | #include "llvm/Pass.h" |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 14 | #include "llvm/Module.h" |
| 15 | #include "llvm/Function.h" |
Chris Lattner | d92b01c | 2002-04-09 18:37:46 +0000 | [diff] [blame] | 16 | #include "llvm/BasicBlock.h" |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 17 | #include "llvm/iMemory.h" |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 18 | #include "llvm/iTerminators.h" |
| 19 | #include "llvm/iOther.h" |
| 20 | #include "llvm/ConstantVals.h" |
| 21 | #include "llvm/Target/TargetData.h" |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 22 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 23 | #include "Support/DepthFirstIterator.h" |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 24 | #include "Support/STLExtras.h" |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 27 | |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 28 | // FIXME: This is dependant on the sparc backend layout conventions!! |
| 29 | static TargetData TargetData("test"); |
| 30 | |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 31 | namespace { |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 32 | // ScalarInfo - Information about an LLVM value that we know points to some |
| 33 | // datastructure we are processing. |
| 34 | // |
| 35 | struct ScalarInfo { |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 36 | Value *Val; // Scalar value in Current Function |
| 37 | DSNode *Node; // DataStructure node it points to |
| 38 | Value *PoolHandle; // PoolTy* LLVM value |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 39 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 40 | ScalarInfo(Value *V, DSNode *N, Value *PH) |
| 41 | : Val(V), Node(N), PoolHandle(PH) { |
| 42 | assert(V && N && PH && "Null value passed to ScalarInfo ctor!"); |
| 43 | } |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 46 | // CallArgInfo - Information on one operand for a call that got expanded. |
| 47 | struct CallArgInfo { |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 48 | int ArgNo; // Call argument number this corresponds to |
| 49 | DSNode *Node; // The graph node for the pool |
| 50 | Value *PoolHandle; // The LLVM value that is the pool pointer |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 51 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 52 | CallArgInfo(int Arg, DSNode *N, Value *PH) |
| 53 | : ArgNo(Arg), Node(N), PoolHandle(PH) { |
| 54 | assert(Arg >= -1 && N && PH && "Illegal values to CallArgInfo ctor!"); |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 57 | // operator< when sorting, sort by argument number. |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 58 | bool operator<(const CallArgInfo &CAI) const { |
| 59 | return ArgNo < CAI.ArgNo; |
| 60 | } |
| 61 | }; |
| 62 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 63 | // TransformFunctionInfo - Information about how a function eeds to be |
| 64 | // transformed. |
| 65 | // |
| 66 | struct TransformFunctionInfo { |
| 67 | // ArgInfo - Maintain information about the arguments that need to be |
| 68 | // processed. Each pair corresponds to an argument (whose number is the |
| 69 | // first element) that needs to have a pool pointer (the second element) |
| 70 | // passed into the transformed function with it. |
| 71 | // |
| 72 | // As a special case, "argument" number -1 corresponds to the return value. |
| 73 | // |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 74 | vector<CallArgInfo> ArgInfo; |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 75 | |
| 76 | // Func - The function to be transformed... |
| 77 | Function *Func; |
| 78 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 79 | // The call instruction that is used to map CallArgInfo PoolHandle values |
| 80 | // into the new function values. |
| 81 | CallInst *Call; |
| 82 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 83 | // default ctor... |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 84 | TransformFunctionInfo() : Func(0), Call(0) {} |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 86 | bool operator<(const TransformFunctionInfo &TFI) const { |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 87 | if (Func < TFI.Func) return true; |
| 88 | if (Func > TFI.Func) return false; |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 89 | if (ArgInfo.size() < TFI.ArgInfo.size()) return true; |
| 90 | if (ArgInfo.size() > TFI.ArgInfo.size()) return false; |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 91 | return ArgInfo < TFI.ArgInfo; |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void finalizeConstruction() { |
| 95 | // Sort the vector so that the return value is first, followed by the |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 96 | // argument records, in order. Note that this must be a stable sort so |
| 97 | // that the entries with the same sorting criteria (ie they are multiple |
| 98 | // pool entries for the same argument) are kept in depth first order. |
| 99 | stable_sort(ArgInfo.begin(), ArgInfo.end()); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 100 | } |
| 101 | }; |
| 102 | |
| 103 | |
| 104 | // Define the pass class that we implement... |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 105 | class PoolAllocate : public Pass { |
| 106 | // PoolTy - The type of a scalar value that contains a pool pointer. |
| 107 | PointerType *PoolTy; |
| 108 | public: |
| 109 | |
| 110 | PoolAllocate() { |
| 111 | // Initialize the PoolTy instance variable, since the type never changes. |
| 112 | vector<const Type*> PoolElements; |
| 113 | PoolElements.push_back(PointerType::get(Type::SByteTy)); |
| 114 | PoolElements.push_back(Type::UIntTy); |
| 115 | PoolTy = PointerType::get(StructType::get(PoolElements)); |
| 116 | // PoolTy = { sbyte*, uint }* |
| 117 | |
| 118 | CurModule = 0; DS = 0; |
| 119 | PoolInit = PoolDestroy = PoolAlloc = PoolFree = 0; |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 122 | bool run(Module *M); |
| 123 | |
| 124 | // getAnalysisUsageInfo - This function requires data structure information |
| 125 | // to be able to see what is pool allocatable. |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 126 | // |
| 127 | virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Required, |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 128 | Pass::AnalysisSet &,Pass::AnalysisSet &) { |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 129 | Required.push_back(DataStructure::ID); |
| 130 | } |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 131 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 132 | public: |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 133 | // CurModule - The module being processed. |
| 134 | Module *CurModule; |
| 135 | |
| 136 | // DS - The data structure graph for the module being processed. |
| 137 | DataStructure *DS; |
| 138 | |
| 139 | // Prototypes that we add to support pool allocation... |
| 140 | Function *PoolInit, *PoolDestroy, *PoolAlloc, *PoolFree; |
| 141 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 142 | // The map of already transformed functions... note that the keys of this |
| 143 | // map do not have meaningful values for 'Call' or the 'PoolHandle' elements |
| 144 | // of the ArgInfo elements. |
| 145 | // |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 146 | map<TransformFunctionInfo, Function*> TransformedFunctions; |
| 147 | |
| 148 | // getTransformedFunction - Get a transformed function, or return null if |
| 149 | // the function specified hasn't been transformed yet. |
| 150 | // |
| 151 | Function *getTransformedFunction(TransformFunctionInfo &TFI) const { |
| 152 | map<TransformFunctionInfo, Function*>::const_iterator I = |
| 153 | TransformedFunctions.find(TFI); |
| 154 | if (I != TransformedFunctions.end()) return I->second; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 159 | // addPoolPrototypes - Add prototypes for the pool methods to the specified |
| 160 | // module and update the Pool* instance variables to point to them. |
| 161 | // |
| 162 | void addPoolPrototypes(Module *M); |
| 163 | |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 164 | |
| 165 | // CreatePools - Insert instructions into the function we are processing to |
| 166 | // create all of the memory pool objects themselves. This also inserts |
| 167 | // destruction code. Add an alloca for each pool that is allocated to the |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 168 | // PoolDescriptors map. |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 169 | // |
| 170 | void CreatePools(Function *F, const vector<AllocDSNode*> &Allocs, |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 171 | map<DSNode*, Value*> &PoolDescriptors); |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 172 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 173 | // processFunction - Convert a function to use pool allocation where |
| 174 | // available. |
| 175 | // |
| 176 | bool processFunction(Function *F); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 177 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 178 | // transformFunctionBody - This transforms the instruction in 'F' to use the |
| 179 | // pools specified in PoolDescriptors when modifying data structure nodes |
| 180 | // specified in the PoolDescriptors map. IPFGraph is the closed data |
| 181 | // structure graph for F, of which the PoolDescriptor nodes come from. |
| 182 | // |
| 183 | void transformFunctionBody(Function *F, FunctionDSGraph &IPFGraph, |
| 184 | map<DSNode*, Value*> &PoolDescriptors); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 185 | |
| 186 | // transformFunction - Transform the specified function the specified way. |
| 187 | // It we have already transformed that function that way, don't do anything. |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 188 | // The nodes in the TransformFunctionInfo come out of callers data structure |
| 189 | // graph. |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 190 | // |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 191 | void transformFunction(TransformFunctionInfo &TFI, |
| 192 | FunctionDSGraph &CallerIPGraph); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 194 | }; |
| 195 | } |
| 196 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 197 | |
| 198 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 199 | // isNotPoolableAlloc - This is a predicate that returns true if the specified |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 200 | // allocation node in a data structure graph is eligable for pool allocation. |
| 201 | // |
| 202 | static bool isNotPoolableAlloc(const AllocDSNode *DS) { |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 203 | if (DS->isAllocaNode()) return true; // Do not pool allocate alloca's. |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 204 | |
| 205 | MallocInst *MI = cast<MallocInst>(DS->getAllocation()); |
| 206 | if (MI->isArrayAllocation() && !isa<Constant>(MI->getArraySize())) |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 207 | return true; // Do not allow variable size allocations... |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 208 | |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 209 | return false; |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 212 | // processFunction - Convert a function to use pool allocation where |
| 213 | // available. |
| 214 | // |
| 215 | bool PoolAllocate::processFunction(Function *F) { |
| 216 | // Get the closed datastructure graph for the current function... if there are |
| 217 | // any allocations in this graph that are not escaping, we need to pool |
| 218 | // allocate them here! |
| 219 | // |
| 220 | FunctionDSGraph &IPGraph = DS->getClosedDSGraph(F); |
| 221 | |
| 222 | // Get all of the allocations that do not escape the current function. Since |
| 223 | // they are still live (they exist in the graph at all), this means we must |
| 224 | // have scalar references to these nodes, but the scalars are never returned. |
| 225 | // |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 226 | vector<AllocDSNode*> Allocs; |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 227 | IPGraph.getNonEscapingAllocations(Allocs); |
| 228 | |
| 229 | // Filter out allocations that we cannot handle. Currently, this includes |
| 230 | // variable sized array allocations and alloca's (which we do not want to |
| 231 | // pool allocate) |
| 232 | // |
| 233 | Allocs.erase(remove_if(Allocs.begin(), Allocs.end(), isNotPoolableAlloc), |
| 234 | Allocs.end()); |
| 235 | |
| 236 | |
| 237 | if (Allocs.empty()) return false; // Nothing to do. |
| 238 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 239 | // Insert instructions into the function we are processing to create all of |
| 240 | // the memory pool objects themselves. This also inserts destruction code. |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 241 | // This fills in the PoolDescriptors map to associate the alloc node with the |
| 242 | // allocation of the memory pool corresponding to it. |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 243 | // |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 244 | map<DSNode*, Value*> PoolDescriptors; |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 245 | CreatePools(F, Allocs, PoolDescriptors); |
| 246 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 247 | // Now we need to figure out what called methods we need to transform, and |
| 248 | // how. To do this, we look at all of the scalars, seeing which functions are |
| 249 | // either used as a scalar value (so they return a data structure), or are |
| 250 | // passed one of our scalar values. |
| 251 | // |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 252 | transformFunctionBody(F, IPGraph, PoolDescriptors); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 253 | |
| 254 | return true; |
| 255 | } |
| 256 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 257 | |
| 258 | class FunctionBodyTransformer : public InstVisitor<FunctionBodyTransformer> { |
| 259 | PoolAllocate &PoolAllocator; |
| 260 | vector<ScalarInfo> &Scalars; |
| 261 | map<CallInst*, TransformFunctionInfo> &CallMap; |
| 262 | |
| 263 | const ScalarInfo &getScalar(const Value *V) { |
| 264 | for (unsigned i = 0, e = Scalars.size(); i != e; ++i) |
| 265 | if (Scalars[i].Val == V) return Scalars[i]; |
| 266 | assert(0 && "Scalar not found in getScalar!"); |
| 267 | abort(); |
| 268 | return Scalars[0]; |
| 269 | } |
| 270 | |
| 271 | // updateScalars - Map the scalars array entries that look like 'From' to look |
| 272 | // like 'To'. |
| 273 | // |
| 274 | void updateScalars(Value *From, Value *To) { |
| 275 | for (unsigned i = 0, e = Scalars.size(); i != e; ++i) |
| 276 | if (Scalars[i].Val == From) Scalars[i].Val = To; |
| 277 | } |
| 278 | |
| 279 | public: |
| 280 | FunctionBodyTransformer(PoolAllocate &PA, vector<ScalarInfo> &S, |
| 281 | map<CallInst*, TransformFunctionInfo> &C) |
| 282 | : PoolAllocator(PA), Scalars(S), CallMap(C) {} |
| 283 | |
| 284 | void visitMemAccessInst(MemAccessInst *MAI) { |
| 285 | // Don't do anything to load, store, or GEP yet... |
| 286 | } |
| 287 | |
| 288 | // Convert a malloc instruction into a call to poolalloc |
| 289 | void visitMallocInst(MallocInst *I) { |
| 290 | const ScalarInfo &SC = getScalar(I); |
| 291 | BasicBlock *BB = I->getParent(); |
| 292 | BasicBlock::iterator MI = find(BB->begin(), BB->end(), I); |
| 293 | BB->getInstList().remove(MI); // Remove the Malloc instruction from the BB |
| 294 | |
| 295 | // Create a new call to poolalloc before the malloc instruction |
| 296 | vector<Value*> Args; |
| 297 | Args.push_back(SC.PoolHandle); |
| 298 | CallInst *Call = new CallInst(PoolAllocator.PoolAlloc, Args, I->getName()); |
| 299 | MI = BB->getInstList().insert(MI, Call)+1; |
| 300 | |
| 301 | // If the type desired is not void*, cast it now... |
| 302 | Value *Ptr = Call; |
| 303 | if (Call->getType() != I->getType()) { |
| 304 | CastInst *CI = new CastInst(Ptr, I->getType(), I->getName()); |
| 305 | BB->getInstList().insert(MI, CI); |
| 306 | Ptr = CI; |
| 307 | } |
| 308 | |
| 309 | // Change everything that used the malloc to now use the pool alloc... |
| 310 | I->replaceAllUsesWith(Ptr); |
| 311 | |
| 312 | // Update the scalars array... |
| 313 | updateScalars(I, Ptr); |
| 314 | |
| 315 | // Delete the instruction now. |
| 316 | delete I; |
| 317 | } |
| 318 | |
| 319 | // Convert the free instruction into a call to poolfree |
| 320 | void visitFreeInst(FreeInst *I) { |
| 321 | Value *Ptr = I->getOperand(0); |
| 322 | const ScalarInfo &SC = getScalar(Ptr); |
| 323 | BasicBlock *BB = I->getParent(); |
| 324 | BasicBlock::iterator FI = find(BB->begin(), BB->end(), I); |
| 325 | |
| 326 | // If the value is not an sbyte*, convert it now! |
| 327 | if (Ptr->getType() != PointerType::get(Type::SByteTy)) { |
| 328 | CastInst *CI = new CastInst(Ptr, PointerType::get(Type::SByteTy), |
| 329 | Ptr->getName()); |
| 330 | FI = BB->getInstList().insert(FI, CI)+1; |
| 331 | Ptr = CI; |
| 332 | } |
| 333 | |
| 334 | // Create a new call to poolfree before the free instruction |
| 335 | vector<Value*> Args; |
| 336 | Args.push_back(SC.PoolHandle); |
| 337 | Args.push_back(Ptr); |
| 338 | CallInst *Call = new CallInst(PoolAllocator.PoolFree, Args); |
| 339 | FI = BB->getInstList().insert(FI, Call)+1; |
| 340 | |
| 341 | // Remove the old free instruction... |
| 342 | delete BB->getInstList().remove(FI); |
| 343 | } |
| 344 | |
| 345 | // visitCallInst - Create a new call instruction with the extra arguments for |
| 346 | // all of the memory pools that the call needs. |
| 347 | // |
| 348 | void visitCallInst(CallInst *I) { |
| 349 | TransformFunctionInfo &TI = CallMap[I]; |
| 350 | BasicBlock *BB = I->getParent(); |
| 351 | BasicBlock::iterator CI = find(BB->begin(), BB->end(), I); |
| 352 | BB->getInstList().remove(CI); // Remove the old call instruction |
| 353 | |
| 354 | // Start with all of the old arguments... |
| 355 | vector<Value*> Args(I->op_begin()+1, I->op_end()); |
| 356 | |
| 357 | // Add all of the pool arguments... |
| 358 | for (unsigned i = 0, e = TI.ArgInfo.size(); i != e; ++i) |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 359 | Args.push_back(TI.ArgInfo[i].PoolHandle); |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 360 | |
| 361 | Function *NF = PoolAllocator.getTransformedFunction(TI); |
| 362 | CallInst *NewCall = new CallInst(NF, Args, I->getName()); |
| 363 | BB->getInstList().insert(CI, NewCall); |
| 364 | |
| 365 | // Change everything that used the malloc to now use the pool alloc... |
| 366 | if (I->getType() != Type::VoidTy) { |
| 367 | I->replaceAllUsesWith(NewCall); |
| 368 | |
| 369 | // Update the scalars array... |
| 370 | updateScalars(I, NewCall); |
| 371 | } |
| 372 | |
| 373 | delete I; // Delete the old call instruction now... |
| 374 | } |
| 375 | |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 376 | void visitPHINode(PHINode *PN) { |
| 377 | // Handle PHI Node |
| 378 | } |
| 379 | |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 380 | void visitReturnInst(ReturnInst *I) { |
| 381 | // Nothing of interest |
| 382 | } |
| 383 | |
Chris Lattner | cf09a2a | 2002-04-01 00:45:33 +0000 | [diff] [blame] | 384 | void visitSetCondInst(SetCondInst *SCI) { |
| 385 | // hrm, notice a pattern? |
| 386 | } |
| 387 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 388 | void visitInstruction(Instruction *I) { |
| 389 | cerr << "Unknown instruction to FunctionBodyTransformer:\n"; |
| 390 | I->dump(); |
| 391 | } |
| 392 | |
| 393 | }; |
| 394 | |
| 395 | |
Chris Lattner | 0dc225c | 2002-03-31 07:17:46 +0000 | [diff] [blame] | 396 | static void addCallInfo(DataStructure *DS, |
| 397 | TransformFunctionInfo &TFI, CallInst *CI, int Arg, |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 398 | DSNode *GraphNode, |
| 399 | map<DSNode*, Value*> &PoolDescriptors) { |
Chris Lattner | 0dc225c | 2002-03-31 07:17:46 +0000 | [diff] [blame] | 400 | assert(CI->getCalledFunction() && "Cannot handle indirect calls yet!"); |
| 401 | assert(TFI.Func == 0 || TFI.Func == CI->getCalledFunction() && |
| 402 | "Function call record should always call the same function!"); |
| 403 | assert(TFI.Call == 0 || TFI.Call == CI && |
| 404 | "Call element already filled in with different value!"); |
| 405 | TFI.Func = CI->getCalledFunction(); |
| 406 | TFI.Call = CI; |
| 407 | //FunctionDSGraph &CalledGraph = DS->getClosedDSGraph(TFI.Func); |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 408 | |
| 409 | // For now, add the entire graph that is pointed to by the call argument. |
| 410 | // This graph can and should be pruned to only what the function itself will |
| 411 | // use, because often this will be a dramatically smaller subset of what we |
| 412 | // are providing. |
| 413 | // |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 414 | for (df_iterator<DSNode*> I = df_begin(GraphNode), E = df_end(GraphNode); |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 415 | I != E; ++I) { |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 416 | TFI.ArgInfo.push_back(CallArgInfo(Arg, *I, PoolDescriptors[*I])); |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 417 | } |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 420 | |
| 421 | // transformFunctionBody - This transforms the instruction in 'F' to use the |
| 422 | // pools specified in PoolDescriptors when modifying data structure nodes |
| 423 | // specified in the PoolDescriptors map. Specifically, scalar values specified |
| 424 | // in the Scalars vector must be remapped. IPFGraph is the closed data |
| 425 | // structure graph for F, of which the PoolDescriptor nodes come from. |
| 426 | // |
| 427 | void PoolAllocate::transformFunctionBody(Function *F, FunctionDSGraph &IPFGraph, |
| 428 | map<DSNode*, Value*> &PoolDescriptors) { |
| 429 | |
| 430 | // Loop through the value map looking for scalars that refer to nonescaping |
| 431 | // allocations. Add them to the Scalars vector. Note that we may have |
| 432 | // multiple entries in the Scalars vector for each value if it points to more |
| 433 | // than one object. |
| 434 | // |
| 435 | map<Value*, PointerValSet> &ValMap = IPFGraph.getValueMap(); |
| 436 | vector<ScalarInfo> Scalars; |
| 437 | |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 438 | cerr << "Building scalar map:\n"; |
| 439 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 440 | for (map<Value*, PointerValSet>::iterator I = ValMap.begin(), |
| 441 | E = ValMap.end(); I != E; ++I) { |
| 442 | const PointerValSet &PVS = I->second; // Set of things pointed to by scalar |
| 443 | |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 444 | cerr << "Scalar Mapping from:"; I->first->dump(); |
| 445 | cerr << "\nScalar Mapping to: "; PVS.print(cerr); |
| 446 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 447 | // Check to see if the scalar points to a data structure node... |
| 448 | for (unsigned i = 0, e = PVS.size(); i != e; ++i) { |
| 449 | assert(PVS[i].Index == 0 && "Nonzero not handled yet!"); |
| 450 | |
| 451 | // If the allocation is in the nonescaping set... |
| 452 | map<DSNode*, Value*>::iterator AI = PoolDescriptors.find(PVS[i].Node); |
| 453 | if (AI != PoolDescriptors.end()) // Add it to the list of scalars |
| 454 | Scalars.push_back(ScalarInfo(I->first, PVS[i].Node, AI->second)); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | |
| 459 | |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 460 | cerr << "\nIn '" << F->getName() |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 461 | << "': Found the following values that point to poolable nodes:\n"; |
| 462 | |
| 463 | for (unsigned i = 0, e = Scalars.size(); i != e; ++i) |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 464 | Scalars[i].Val->dump(); |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 465 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 466 | // CallMap - Contain an entry for every call instruction that needs to be |
| 467 | // transformed. Each entry in the map contains information about what we need |
| 468 | // to do to each call site to change it to work. |
| 469 | // |
| 470 | map<CallInst*, TransformFunctionInfo> CallMap; |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 471 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 472 | // Now we need to figure out what called methods we need to transform, and |
| 473 | // how. To do this, we look at all of the scalars, seeing which functions are |
| 474 | // either used as a scalar value (so they return a data structure), or are |
| 475 | // passed one of our scalar values. |
| 476 | // |
| 477 | for (unsigned i = 0, e = Scalars.size(); i != e; ++i) { |
| 478 | Value *ScalarVal = Scalars[i].Val; |
| 479 | |
| 480 | // Check to see if the scalar _IS_ a call... |
| 481 | if (CallInst *CI = dyn_cast<CallInst>(ScalarVal)) |
| 482 | // If so, add information about the pool it will be returning... |
Chris Lattner | 0dc225c | 2002-03-31 07:17:46 +0000 | [diff] [blame] | 483 | addCallInfo(DS, CallMap[CI], CI, -1, Scalars[i].Node, PoolDescriptors); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 484 | |
| 485 | // Check to see if the scalar is an operand to a call... |
| 486 | for (Value::use_iterator UI = ScalarVal->use_begin(), |
| 487 | UE = ScalarVal->use_end(); UI != UE; ++UI) { |
| 488 | if (CallInst *CI = dyn_cast<CallInst>(*UI)) { |
| 489 | // Find out which operand this is to the call instruction... |
| 490 | User::op_iterator OI = find(CI->op_begin(), CI->op_end(), ScalarVal); |
| 491 | assert(OI != CI->op_end() && "Call on use list but not an operand!?"); |
| 492 | assert(OI != CI->op_begin() && "Pointer operand is call destination?"); |
| 493 | |
| 494 | // FIXME: This is broken if the same pointer is passed to a call more |
| 495 | // than once! It will get multiple entries for the first pointer. |
| 496 | |
| 497 | // Add the operand number and pool handle to the call table... |
Chris Lattner | 0dc225c | 2002-03-31 07:17:46 +0000 | [diff] [blame] | 498 | addCallInfo(DS, CallMap[CI], CI, OI-CI->op_begin()-1, Scalars[i].Node, |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 499 | PoolDescriptors); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // Print out call map... |
| 505 | for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin(); |
| 506 | I != CallMap.end(); ++I) { |
| 507 | cerr << "\nFor call: "; |
| 508 | I->first->dump(); |
| 509 | I->second.finalizeConstruction(); |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 510 | cerr << I->second.Func->getName() << " must pass pool pointer for args #"; |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 511 | for (unsigned i = 0; i < I->second.ArgInfo.size(); ++i) |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 512 | cerr << I->second.ArgInfo[i].ArgNo << ", "; |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 513 | cerr << "\n"; |
| 514 | } |
| 515 | |
| 516 | // Loop through all of the call nodes, recursively creating the new functions |
| 517 | // that we want to call... This uses a map to prevent infinite recursion and |
| 518 | // to avoid duplicating functions unneccesarily. |
| 519 | // |
| 520 | for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin(), |
| 521 | E = CallMap.end(); I != E; ++I) { |
| 522 | // Make sure the entries are sorted. |
| 523 | I->second.finalizeConstruction(); |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 524 | |
| 525 | // Transform all of the functions we need, or at least ensure there is a |
| 526 | // cached version available. |
| 527 | transformFunction(I->second, IPFGraph); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 530 | // Now that all of the functions that we want to call are available, transform |
| 531 | // the local method so that it uses the pools locally and passes them to the |
| 532 | // functions that we just hacked up. |
| 533 | // |
| 534 | |
| 535 | // First step, find the instructions to be modified. |
| 536 | vector<Instruction*> InstToFix; |
| 537 | for (unsigned i = 0, e = Scalars.size(); i != e; ++i) { |
| 538 | Value *ScalarVal = Scalars[i].Val; |
| 539 | |
| 540 | // Check to see if the scalar _IS_ an instruction. If so, it is involved. |
| 541 | if (Instruction *Inst = dyn_cast<Instruction>(ScalarVal)) |
| 542 | InstToFix.push_back(Inst); |
| 543 | |
| 544 | // All all of the instructions that use the scalar as an operand... |
| 545 | for (Value::use_iterator UI = ScalarVal->use_begin(), |
| 546 | UE = ScalarVal->use_end(); UI != UE; ++UI) |
| 547 | InstToFix.push_back(dyn_cast<Instruction>(*UI)); |
| 548 | } |
| 549 | |
| 550 | // Eliminate duplicates by sorting, then removing equal neighbors. |
| 551 | sort(InstToFix.begin(), InstToFix.end()); |
| 552 | InstToFix.erase(unique(InstToFix.begin(), InstToFix.end()), InstToFix.end()); |
| 553 | |
| 554 | // Use a FunctionBodyTransformer to transform all of the involved instructions |
| 555 | FunctionBodyTransformer FBT(*this, Scalars, CallMap); |
| 556 | for (unsigned i = 0, e = InstToFix.size(); i != e; ++i) |
| 557 | FBT.visit(InstToFix[i]); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 558 | |
| 559 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 560 | // Since we have liberally hacked the function to pieces, we want to inform |
| 561 | // the datastructure pass that its internal representation is out of date. |
| 562 | // |
| 563 | DS->invalidateFunction(F); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 566 | static void addNodeMapping(DSNode *SrcNode, const PointerValSet &PVS, |
| 567 | map<DSNode*, PointerValSet> &NodeMapping) { |
| 568 | for (unsigned i = 0, e = PVS.size(); i != e; ++i) |
| 569 | if (NodeMapping[SrcNode].add(PVS[i])) { // Not in map yet? |
| 570 | assert(PVS[i].Index == 0 && "Node indexing not supported yet!"); |
| 571 | DSNode *DestNode = PVS[i].Node; |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 572 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 573 | // Loop over all of the outgoing links in the mapped graph |
| 574 | for (unsigned l = 0, le = DestNode->getNumOutgoingLinks(); l != le; ++l) { |
| 575 | PointerValSet &SrcSet = SrcNode->getOutgoingLink(l); |
| 576 | const PointerValSet &DestSet = DestNode->getOutgoingLink(l); |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 577 | |
| 578 | // Add all of the node mappings now! |
| 579 | for (unsigned si = 0, se = SrcSet.size(); si != se; ++si) { |
| 580 | assert(SrcSet[si].Index == 0 && "Can't handle node offset!"); |
| 581 | addNodeMapping(SrcSet[si].Node, DestSet, NodeMapping); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | // CalculateNodeMapping - There is a partial isomorphism between the graph |
| 588 | // passed in and the graph that is actually used by the function. We need to |
| 589 | // figure out what this mapping is so that we can transformFunctionBody the |
| 590 | // instructions in the function itself. Note that every node in the graph that |
| 591 | // we are interested in must be both in the local graph of the called function, |
| 592 | // and in the local graph of the calling function. Because of this, we only |
| 593 | // define the mapping for these nodes [conveniently these are the only nodes we |
| 594 | // CAN define a mapping for...] |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 595 | // |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 596 | // The roots of the graph that we are transforming is rooted in the arguments |
| 597 | // passed into the function from the caller. This is where we start our |
| 598 | // mapping calculation. |
| 599 | // |
| 600 | // The NodeMapping calculated maps from the callers graph to the called graph. |
| 601 | // |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 602 | static void CalculateNodeMapping(Function *F, TransformFunctionInfo &TFI, |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 603 | FunctionDSGraph &CallerGraph, |
| 604 | FunctionDSGraph &CalledGraph, |
| 605 | map<DSNode*, PointerValSet> &NodeMapping) { |
| 606 | int LastArgNo = -2; |
| 607 | for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) { |
| 608 | // Figure out what nodes in the called graph the TFI.ArgInfo[i].Node node |
| 609 | // corresponds to... |
| 610 | // |
| 611 | // Only consider first node of sequence. Extra nodes may may be added |
| 612 | // to the TFI if the data structure requires more nodes than just the |
| 613 | // one the argument points to. We are only interested in the one the |
| 614 | // argument points to though. |
| 615 | // |
| 616 | if (TFI.ArgInfo[i].ArgNo != LastArgNo) { |
| 617 | if (TFI.ArgInfo[i].ArgNo == -1) { |
| 618 | addNodeMapping(TFI.ArgInfo[i].Node, CalledGraph.getRetNodes(), |
| 619 | NodeMapping); |
| 620 | } else { |
| 621 | // Figure out which node argument # ArgNo points to in the called graph. |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 622 | Value *Arg = F->getArgumentList()[TFI.ArgInfo[i].ArgNo]; |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 623 | addNodeMapping(TFI.ArgInfo[i].Node, CalledGraph.getValueMap()[Arg], |
| 624 | NodeMapping); |
| 625 | } |
| 626 | LastArgNo = TFI.ArgInfo[i].ArgNo; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | |
| 632 | // transformFunction - Transform the specified function the specified way. It |
| 633 | // we have already transformed that function that way, don't do anything. The |
| 634 | // nodes in the TransformFunctionInfo come out of callers data structure graph. |
| 635 | // |
| 636 | void PoolAllocate::transformFunction(TransformFunctionInfo &TFI, |
| 637 | FunctionDSGraph &CallerIPGraph) { |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 638 | if (getTransformedFunction(TFI)) return; // Function xformation already done? |
| 639 | |
Chris Lattner | 0dc225c | 2002-03-31 07:17:46 +0000 | [diff] [blame] | 640 | cerr << "**********\nEntering transformFunction for " |
| 641 | << TFI.Func->getName() << ":\n"; |
| 642 | for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) |
| 643 | cerr << " ArgInfo[" << i << "] = " << TFI.ArgInfo[i].ArgNo << "\n"; |
| 644 | cerr << "\n"; |
| 645 | |
| 646 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 647 | const FunctionType *OldFuncType = TFI.Func->getFunctionType(); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 648 | |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 649 | assert(!OldFuncType->isVarArg() && "Vararg functions not handled yet!"); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 650 | |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 651 | // Build the type for the new function that we are transforming |
| 652 | vector<const Type*> ArgTys; |
| 653 | for (unsigned i = 0, e = OldFuncType->getNumParams(); i != e; ++i) |
| 654 | ArgTys.push_back(OldFuncType->getParamType(i)); |
| 655 | |
| 656 | // Add one pool pointer for every argument that needs to be supplemented. |
| 657 | ArgTys.insert(ArgTys.end(), TFI.ArgInfo.size(), PoolTy); |
| 658 | |
| 659 | // Build the new function type... |
| 660 | const // FIXME when types are not const |
| 661 | FunctionType *NewFuncType = FunctionType::get(OldFuncType->getReturnType(), |
| 662 | ArgTys,OldFuncType->isVarArg()); |
| 663 | |
| 664 | // The new function is internal, because we know that only we can call it. |
| 665 | // This also helps subsequent IP transformations to eliminate duplicated pool |
| 666 | // pointers. [in the future when they are implemented]. |
| 667 | // |
| 668 | Function *NewFunc = new Function(NewFuncType, true, |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 669 | TFI.Func->getName()+".poolxform"); |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 670 | CurModule->getFunctionList().push_back(NewFunc); |
| 671 | |
| 672 | // Add the newly formed function to the TransformedFunctions table so that |
| 673 | // infinite recursion does not occur! |
| 674 | // |
| 675 | TransformedFunctions[TFI] = NewFunc; |
| 676 | |
| 677 | // Add arguments to the function... starting with all of the old arguments |
| 678 | vector<Value*> ArgMap; |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 679 | for (unsigned i = 0, e = TFI.Func->getArgumentList().size(); i != e; ++i) { |
| 680 | const FunctionArgument *OFA = TFI.Func->getArgumentList()[i]; |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 681 | FunctionArgument *NFA = new FunctionArgument(OFA->getType(),OFA->getName()); |
| 682 | NewFunc->getArgumentList().push_back(NFA); |
| 683 | ArgMap.push_back(NFA); // Keep track of the arguments |
| 684 | } |
| 685 | |
| 686 | // Now add all of the arguments corresponding to pools passed in... |
| 687 | for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) { |
| 688 | string Name; |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 689 | if (TFI.ArgInfo[i].ArgNo == -1) |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 690 | Name = "retpool"; |
| 691 | else |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 692 | Name = ArgMap[TFI.ArgInfo[i].ArgNo]->getName(); // Get the arg name |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 693 | FunctionArgument *NFA = new FunctionArgument(PoolTy, Name+".pool"); |
| 694 | NewFunc->getArgumentList().push_back(NFA); |
| 695 | } |
| 696 | |
| 697 | // Now clone the body of the old function into the new function... |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 698 | CloneFunctionInto(NewFunc, TFI.Func, ArgMap); |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 699 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 700 | // Okay, now we have a function that is identical to the old one, except that |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 701 | // it has extra arguments for the pools coming in. Now we have to get the |
| 702 | // data structure graph for the function we are replacing, and figure out how |
| 703 | // our graph nodes map to the graph nodes in the dest function. |
| 704 | // |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 705 | FunctionDSGraph &DSGraph = DS->getClosedDSGraph(NewFunc); |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 706 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 707 | // NodeMapping - Multimap from callers graph to called graph. |
| 708 | // |
| 709 | map<DSNode*, PointerValSet> NodeMapping; |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 710 | |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 711 | CalculateNodeMapping(NewFunc, TFI, CallerIPGraph, DSGraph, |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 712 | NodeMapping); |
| 713 | |
| 714 | // Print out the node mapping... |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 715 | cerr << "\nNode mapping for call of " << NewFunc->getName() << "\n"; |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 716 | for (map<DSNode*, PointerValSet>::iterator I = NodeMapping.begin(); |
| 717 | I != NodeMapping.end(); ++I) { |
| 718 | cerr << "Map: "; I->first->print(cerr); |
| 719 | cerr << "To: "; I->second.print(cerr); |
| 720 | cerr << "\n"; |
| 721 | } |
| 722 | |
| 723 | // Fill in the PoolDescriptor information for the transformed function so that |
| 724 | // it can determine which value holds the pool descriptor for each data |
| 725 | // structure node that it accesses. |
| 726 | // |
| 727 | map<DSNode*, Value*> PoolDescriptors; |
| 728 | |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 729 | cerr << "\nCalculating the pool descriptor map:\n"; |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 730 | |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 731 | // All of the pool descriptors must be passed in as arguments... |
| 732 | for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) { |
| 733 | DSNode *CallerNode = TFI.ArgInfo[i].Node; |
| 734 | Value *CallerPool = TFI.ArgInfo[i].PoolHandle; |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 735 | |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 736 | cerr << "Mapped caller node: "; CallerNode->print(cerr); |
| 737 | cerr << "Mapped caller pool: "; CallerPool->dump(); |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 738 | |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 739 | // Calculate the argument number that the pool is to the function call... |
| 740 | // The call instruction should not have the pool operands added yet. |
| 741 | unsigned ArgNo = TFI.Call->getNumOperands()-1+i; |
| 742 | cerr << "Should be argument #: " << ArgNo << "[i = " << i << "]\n"; |
| 743 | assert(ArgNo < NewFunc->getArgumentList().size() && |
| 744 | "Call already has pool arguments added??"); |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 745 | |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 746 | // Map the pool argument into the called function... |
| 747 | Value *CalleePool = NewFunc->getArgumentList()[ArgNo]; |
| 748 | |
| 749 | // Map the DSNode into the callee's DSGraph |
| 750 | const PointerValSet &CalleeNodes = NodeMapping[CallerNode]; |
| 751 | for (unsigned n = 0, ne = CalleeNodes.size(); n != ne; ++n) { |
| 752 | assert(CalleeNodes[n].Index == 0 && "Indexed node not handled yet!"); |
| 753 | DSNode *CalleeNode = CalleeNodes[n].Node; |
| 754 | |
| 755 | cerr << "*** to callee node: "; CalleeNode->print(cerr); |
| 756 | cerr << "*** to callee pool: "; CalleePool->dump(); |
| 757 | cerr << "\n"; |
| 758 | |
| 759 | assert(CalleeNode && CalleePool && "Invalid nodes!"); |
| 760 | Value *&PV = PoolDescriptors[CalleeNode]; |
| 761 | //assert((PV == 0 || PV == CalleePool) && "Invalid node remapping!"); |
| 762 | PV = CalleePool; // Update the pool descriptor map! |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 763 | } |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | // We must destroy the node mapping so that we don't have latent references |
| 767 | // into the data structure graph for the new function. Otherwise we get |
| 768 | // assertion failures when transformFunctionBody tries to invalidate the |
| 769 | // graph. |
| 770 | // |
| 771 | NodeMapping.clear(); |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 772 | |
| 773 | // Now that we know everything we need about the function, transform the body |
| 774 | // now! |
| 775 | // |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 776 | transformFunctionBody(NewFunc, DSGraph, PoolDescriptors); |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 777 | |
| 778 | cerr << "Function after transformation:\n"; |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 779 | NewFunc->dump(); |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | |
| 783 | // CreatePools - Insert instructions into the function we are processing to |
| 784 | // create all of the memory pool objects themselves. This also inserts |
| 785 | // destruction code. Add an alloca for each pool that is allocated to the |
| 786 | // PoolDescriptors vector. |
| 787 | // |
| 788 | void PoolAllocate::CreatePools(Function *F, const vector<AllocDSNode*> &Allocs, |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 789 | map<DSNode*, Value*> &PoolDescriptors) { |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 790 | // FIXME: This should use an IP version of the UnifyAllExits pass! |
| 791 | vector<BasicBlock*> ReturnNodes; |
| 792 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 793 | if (isa<ReturnInst>((*I)->getTerminator())) |
| 794 | ReturnNodes.push_back(*I); |
| 795 | |
| 796 | |
| 797 | // Create the code that goes in the entry and exit nodes for the method... |
| 798 | vector<Instruction*> EntryNodeInsts; |
| 799 | for (unsigned i = 0, e = Allocs.size(); i != e; ++i) { |
| 800 | // Add an allocation and a free for each pool... |
| 801 | AllocaInst *PoolAlloc = new AllocaInst(PoolTy, 0, "pool"); |
| 802 | EntryNodeInsts.push_back(PoolAlloc); |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 803 | PoolDescriptors[Allocs[i]] = PoolAlloc; // Keep track of pool allocas |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 804 | AllocationInst *AI = Allocs[i]->getAllocation(); |
| 805 | |
| 806 | // Initialize the pool. We need to know how big each allocation is. For |
| 807 | // our purposes here, we assume we are allocating a scalar, or array of |
| 808 | // constant size. |
| 809 | // |
| 810 | unsigned ElSize = TargetData.getTypeSize(AI->getAllocatedType()); |
| 811 | ElSize *= cast<ConstantUInt>(AI->getArraySize())->getValue(); |
| 812 | |
| 813 | vector<Value*> Args; |
| 814 | Args.push_back(PoolAlloc); // Pool to initialize |
| 815 | Args.push_back(ConstantUInt::get(Type::UIntTy, ElSize)); |
| 816 | EntryNodeInsts.push_back(new CallInst(PoolInit, Args)); |
| 817 | |
| 818 | // Destroy the pool... |
| 819 | Args.pop_back(); |
| 820 | |
| 821 | for (unsigned EN = 0, ENE = ReturnNodes.size(); EN != ENE; ++EN) { |
| 822 | Instruction *Destroy = new CallInst(PoolDestroy, Args); |
| 823 | |
| 824 | // Insert it before the return instruction... |
| 825 | BasicBlock *RetNode = ReturnNodes[EN]; |
| 826 | RetNode->getInstList().insert(RetNode->end()-1, Destroy); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | // Insert the entry node code into the entry block... |
| 831 | F->getEntryNode()->getInstList().insert(F->getEntryNode()->begin()+1, |
| 832 | EntryNodeInsts.begin(), |
| 833 | EntryNodeInsts.end()); |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 837 | // addPoolPrototypes - Add prototypes for the pool methods to the specified |
| 838 | // module and update the Pool* instance variables to point to them. |
| 839 | // |
| 840 | void PoolAllocate::addPoolPrototypes(Module *M) { |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 841 | // Get PoolInit function... |
| 842 | vector<const Type*> Args; |
| 843 | Args.push_back(PoolTy); // Pool to initialize |
| 844 | Args.push_back(Type::UIntTy); // Num bytes per element |
| 845 | FunctionType *PoolInitTy = FunctionType::get(Type::VoidTy, Args, false); |
| 846 | PoolInit = M->getOrInsertFunction("poolinit", PoolInitTy); |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 847 | |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 848 | // Get pooldestroy function... |
| 849 | Args.pop_back(); // Only takes a pool... |
| 850 | FunctionType *PoolDestroyTy = FunctionType::get(Type::VoidTy, Args, false); |
| 851 | PoolDestroy = M->getOrInsertFunction("pooldestroy", PoolDestroyTy); |
| 852 | |
| 853 | const Type *PtrVoid = PointerType::get(Type::SByteTy); |
| 854 | |
| 855 | // Get the poolalloc function... |
| 856 | FunctionType *PoolAllocTy = FunctionType::get(PtrVoid, Args, false); |
| 857 | PoolAlloc = M->getOrInsertFunction("poolalloc", PoolAllocTy); |
| 858 | |
| 859 | // Get the poolfree function... |
| 860 | Args.push_back(PtrVoid); |
| 861 | FunctionType *PoolFreeTy = FunctionType::get(Type::VoidTy, Args, false); |
| 862 | PoolFree = M->getOrInsertFunction("poolfree", PoolFreeTy); |
| 863 | |
| 864 | // Add the %PoolTy type to the symbol table of the module... |
| 865 | M->addTypeName("PoolTy", PoolTy->getElementType()); |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | |
| 869 | bool PoolAllocate::run(Module *M) { |
| 870 | addPoolPrototypes(M); |
| 871 | CurModule = M; |
| 872 | |
| 873 | DS = &getAnalysis<DataStructure>(); |
| 874 | bool Changed = false; |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 875 | |
| 876 | // We cannot use an iterator here because it will get invalidated when we add |
| 877 | // functions to the module later... |
| 878 | for (unsigned i = 0; i != M->size(); ++i) |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 879 | if (!M->getFunctionList()[i]->isExternal()) { |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 880 | Changed |= processFunction(M->getFunctionList()[i]); |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 881 | if (Changed) { |
| 882 | cerr << "Only processing one function\n"; |
| 883 | break; |
| 884 | } |
| 885 | } |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 886 | |
| 887 | CurModule = 0; |
| 888 | DS = 0; |
| 889 | return false; |
| 890 | } |
| 891 | |
| 892 | |
| 893 | // createPoolAllocatePass - Global function to access the functionality of this |
| 894 | // pass... |
| 895 | // |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 896 | Pass *createPoolAllocatePass() { return new PoolAllocate(); } |