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" |
| 12 | #include "llvm/Pass.h" |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 13 | #include "llvm/Module.h" |
| 14 | #include "llvm/Function.h" |
| 15 | #include "llvm/iMemory.h" |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 16 | #include "llvm/iTerminators.h" |
| 17 | #include "llvm/iOther.h" |
| 18 | #include "llvm/ConstantVals.h" |
| 19 | #include "llvm/Target/TargetData.h" |
| 20 | #include "Support/STLExtras.h" |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 23 | |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 24 | // FIXME: This is dependant on the sparc backend layout conventions!! |
| 25 | static TargetData TargetData("test"); |
| 26 | |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 27 | namespace { |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 28 | // ScalarInfo - Information about an LLVM value that we know points to some |
| 29 | // datastructure we are processing. |
| 30 | // |
| 31 | struct ScalarInfo { |
| 32 | Value *Val; // Scalar value in Current Function |
| 33 | AllocDSNode *AllocNode; // Allocation node it points to |
| 34 | Value *PoolHandle; // PoolTy* LLVM value |
| 35 | |
| 36 | ScalarInfo(Value *V, AllocDSNode *AN, Value *PH) |
| 37 | : Val(V), AllocNode(AN), PoolHandle(PH) {} |
| 38 | }; |
| 39 | |
| 40 | // TransformFunctionInfo - Information about how a function eeds to be |
| 41 | // transformed. |
| 42 | // |
| 43 | struct TransformFunctionInfo { |
| 44 | // ArgInfo - Maintain information about the arguments that need to be |
| 45 | // processed. Each pair corresponds to an argument (whose number is the |
| 46 | // first element) that needs to have a pool pointer (the second element) |
| 47 | // passed into the transformed function with it. |
| 48 | // |
| 49 | // As a special case, "argument" number -1 corresponds to the return value. |
| 50 | // |
| 51 | vector<pair<int, Value*> > ArgInfo; |
| 52 | |
| 53 | // Func - The function to be transformed... |
| 54 | Function *Func; |
| 55 | |
| 56 | // default ctor... |
| 57 | TransformFunctionInfo() : Func(0) {} |
| 58 | |
| 59 | inline bool operator<(const TransformFunctionInfo &TFI) const { |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame^] | 60 | if (Func < TFI.Func) return true; |
| 61 | if (Func > TFI.Func) return false; |
| 62 | |
| 63 | // Loop over the arguments, checking to see if only the arg _numbers_ are |
| 64 | // less... |
| 65 | if (ArgInfo.size() < TFI.ArgInfo.size()) return true; |
| 66 | if (ArgInfo.size() > TFI.ArgInfo.size()) return false; |
| 67 | |
| 68 | for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) { |
| 69 | if (ArgInfo[i].first < TFI.ArgInfo[i].first) return true; |
| 70 | if (ArgInfo[i].first > TFI.ArgInfo[i].first) return false; |
| 71 | } |
| 72 | return false; // They must be equal |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void finalizeConstruction() { |
| 76 | // Sort the vector so that the return value is first, followed by the |
| 77 | // argument records, in order. |
| 78 | sort(ArgInfo.begin(), ArgInfo.end()); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | // Define the pass class that we implement... |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 84 | class PoolAllocate : public Pass { |
| 85 | // PoolTy - The type of a scalar value that contains a pool pointer. |
| 86 | PointerType *PoolTy; |
| 87 | public: |
| 88 | |
| 89 | PoolAllocate() { |
| 90 | // Initialize the PoolTy instance variable, since the type never changes. |
| 91 | vector<const Type*> PoolElements; |
| 92 | PoolElements.push_back(PointerType::get(Type::SByteTy)); |
| 93 | PoolElements.push_back(Type::UIntTy); |
| 94 | PoolTy = PointerType::get(StructType::get(PoolElements)); |
| 95 | // PoolTy = { sbyte*, uint }* |
| 96 | |
| 97 | CurModule = 0; DS = 0; |
| 98 | PoolInit = PoolDestroy = PoolAlloc = PoolFree = 0; |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 101 | bool run(Module *M); |
| 102 | |
| 103 | // getAnalysisUsageInfo - This function requires data structure information |
| 104 | // to be able to see what is pool allocatable. |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 105 | // |
| 106 | virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Required, |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 107 | Pass::AnalysisSet &,Pass::AnalysisSet &) { |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 108 | Required.push_back(DataStructure::ID); |
| 109 | } |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 110 | |
| 111 | private: |
| 112 | // CurModule - The module being processed. |
| 113 | Module *CurModule; |
| 114 | |
| 115 | // DS - The data structure graph for the module being processed. |
| 116 | DataStructure *DS; |
| 117 | |
| 118 | // Prototypes that we add to support pool allocation... |
| 119 | Function *PoolInit, *PoolDestroy, *PoolAlloc, *PoolFree; |
| 120 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 121 | // The map of already transformed functions... |
| 122 | map<TransformFunctionInfo, Function*> TransformedFunctions; |
| 123 | |
| 124 | // getTransformedFunction - Get a transformed function, or return null if |
| 125 | // the function specified hasn't been transformed yet. |
| 126 | // |
| 127 | Function *getTransformedFunction(TransformFunctionInfo &TFI) const { |
| 128 | map<TransformFunctionInfo, Function*>::const_iterator I = |
| 129 | TransformedFunctions.find(TFI); |
| 130 | if (I != TransformedFunctions.end()) return I->second; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 135 | // addPoolPrototypes - Add prototypes for the pool methods to the specified |
| 136 | // module and update the Pool* instance variables to point to them. |
| 137 | // |
| 138 | void addPoolPrototypes(Module *M); |
| 139 | |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 140 | |
| 141 | // CreatePools - Insert instructions into the function we are processing to |
| 142 | // create all of the memory pool objects themselves. This also inserts |
| 143 | // destruction code. Add an alloca for each pool that is allocated to the |
| 144 | // PoolDescriptors vector. |
| 145 | // |
| 146 | void CreatePools(Function *F, const vector<AllocDSNode*> &Allocs, |
| 147 | vector<AllocaInst*> &PoolDescriptors); |
| 148 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 149 | // processFunction - Convert a function to use pool allocation where |
| 150 | // available. |
| 151 | // |
| 152 | bool processFunction(Function *F); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | void transformFunctionBody(Function *F, vector<ScalarInfo> &Scalars); |
| 156 | |
| 157 | // transformFunction - Transform the specified function the specified way. |
| 158 | // It we have already transformed that function that way, don't do anything. |
| 159 | // |
| 160 | void transformFunction(TransformFunctionInfo &TFI); |
| 161 | |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 162 | }; |
| 163 | } |
| 164 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 165 | |
| 166 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 167 | // isNotPoolableAlloc - This is a predicate that returns true if the specified |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 168 | // allocation node in a data structure graph is eligable for pool allocation. |
| 169 | // |
| 170 | static bool isNotPoolableAlloc(const AllocDSNode *DS) { |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 171 | if (DS->isAllocaNode()) return true; // Do not pool allocate alloca's. |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 172 | |
| 173 | MallocInst *MI = cast<MallocInst>(DS->getAllocation()); |
| 174 | if (MI->isArrayAllocation() && !isa<Constant>(MI->getArraySize())) |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 175 | return true; // Do not allow variable size allocations... |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 176 | |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 177 | return false; |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 180 | // processFunction - Convert a function to use pool allocation where |
| 181 | // available. |
| 182 | // |
| 183 | bool PoolAllocate::processFunction(Function *F) { |
| 184 | // Get the closed datastructure graph for the current function... if there are |
| 185 | // any allocations in this graph that are not escaping, we need to pool |
| 186 | // allocate them here! |
| 187 | // |
| 188 | FunctionDSGraph &IPGraph = DS->getClosedDSGraph(F); |
| 189 | |
| 190 | // Get all of the allocations that do not escape the current function. Since |
| 191 | // they are still live (they exist in the graph at all), this means we must |
| 192 | // have scalar references to these nodes, but the scalars are never returned. |
| 193 | // |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 194 | vector<AllocDSNode*> Allocs; |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 195 | IPGraph.getNonEscapingAllocations(Allocs); |
| 196 | |
| 197 | // Filter out allocations that we cannot handle. Currently, this includes |
| 198 | // variable sized array allocations and alloca's (which we do not want to |
| 199 | // pool allocate) |
| 200 | // |
| 201 | Allocs.erase(remove_if(Allocs.begin(), Allocs.end(), isNotPoolableAlloc), |
| 202 | Allocs.end()); |
| 203 | |
| 204 | |
| 205 | if (Allocs.empty()) return false; // Nothing to do. |
| 206 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 207 | // Insert instructions into the function we are processing to create all of |
| 208 | // the memory pool objects themselves. This also inserts destruction code. |
| 209 | // This fills in the PoolDescriptors vector to be a array parallel with |
| 210 | // Allocs, but containing the alloca instructions that allocate the pool ptr. |
| 211 | // |
| 212 | vector<AllocaInst*> PoolDescriptors; |
| 213 | CreatePools(F, Allocs, PoolDescriptors); |
| 214 | |
| 215 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 216 | // Loop through the value map looking for scalars that refer to nonescaping |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 217 | // allocations. Add them to the Scalars vector. Note that we may have |
| 218 | // multiple entries in the Scalars vector for each value if it points to more |
| 219 | // than one object. |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 220 | // |
| 221 | map<Value*, PointerValSet> &ValMap = IPGraph.getValueMap(); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 222 | vector<ScalarInfo> Scalars; |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 223 | |
| 224 | for (map<Value*, PointerValSet>::iterator I = ValMap.begin(), |
| 225 | E = ValMap.end(); I != E; ++I) { |
| 226 | const PointerValSet &PVS = I->second; // Set of things pointed to by scalar |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 227 | |
| 228 | assert(PVS.size() == 1 && |
| 229 | "Only handle scalars that point to one thing so far!"); |
| 230 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 231 | // Check to see if the scalar points to anything that is an allocation... |
| 232 | for (unsigned i = 0, e = PVS.size(); i != e; ++i) |
| 233 | if (AllocDSNode *Alloc = dyn_cast<AllocDSNode>(PVS[i].Node)) { |
| 234 | assert(PVS[i].Index == 0 && "Nonzero not handled yet!"); |
| 235 | |
| 236 | // If the allocation is in the nonescaping set... |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 237 | vector<AllocDSNode*>::iterator AI = |
| 238 | find(Allocs.begin(), Allocs.end(), Alloc); |
| 239 | if (AI != Allocs.end()) { |
| 240 | unsigned IDX = AI-Allocs.begin(); |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 241 | // Add it to the list of scalars we have |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 242 | Scalars.push_back(ScalarInfo(I->first, Alloc, PoolDescriptors[IDX])); |
| 243 | } |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 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 | // |
| 252 | transformFunctionBody(F, Scalars); |
| 253 | |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | static void addCallInfo(TransformFunctionInfo &TFI, CallInst *CI, int Arg, |
| 258 | Value *PoolHandle) { |
| 259 | assert(CI->getCalledFunction() && "Cannot handle indirect calls yet!"); |
| 260 | TFI.ArgInfo.push_back(make_pair(Arg, PoolHandle)); |
| 261 | |
| 262 | assert(TFI.Func == 0 || TFI.Func == CI->getCalledFunction() && |
| 263 | "Function call record should always call the same function!"); |
| 264 | TFI.Func = CI->getCalledFunction(); |
| 265 | } |
| 266 | |
| 267 | void PoolAllocate::transformFunctionBody(Function *F, |
| 268 | vector<ScalarInfo> &Scalars) { |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 269 | cerr << "In '" << F->getName() |
| 270 | << "': Found the following values that point to poolable nodes:\n"; |
| 271 | |
| 272 | for (unsigned i = 0, e = Scalars.size(); i != e; ++i) |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 273 | Scalars[i].Val->dump(); |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 275 | // CallMap - Contain an entry for every call instruction that needs to be |
| 276 | // transformed. Each entry in the map contains information about what we need |
| 277 | // to do to each call site to change it to work. |
| 278 | // |
| 279 | map<CallInst*, TransformFunctionInfo> CallMap; |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 280 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 281 | // Now we need to figure out what called methods we need to transform, and |
| 282 | // how. To do this, we look at all of the scalars, seeing which functions are |
| 283 | // either used as a scalar value (so they return a data structure), or are |
| 284 | // passed one of our scalar values. |
| 285 | // |
| 286 | for (unsigned i = 0, e = Scalars.size(); i != e; ++i) { |
| 287 | Value *ScalarVal = Scalars[i].Val; |
| 288 | |
| 289 | // Check to see if the scalar _IS_ a call... |
| 290 | if (CallInst *CI = dyn_cast<CallInst>(ScalarVal)) |
| 291 | // If so, add information about the pool it will be returning... |
| 292 | addCallInfo(CallMap[CI], CI, -1, Scalars[i].PoolHandle); |
| 293 | |
| 294 | // Check to see if the scalar is an operand to a call... |
| 295 | for (Value::use_iterator UI = ScalarVal->use_begin(), |
| 296 | UE = ScalarVal->use_end(); UI != UE; ++UI) { |
| 297 | if (CallInst *CI = dyn_cast<CallInst>(*UI)) { |
| 298 | // Find out which operand this is to the call instruction... |
| 299 | User::op_iterator OI = find(CI->op_begin(), CI->op_end(), ScalarVal); |
| 300 | assert(OI != CI->op_end() && "Call on use list but not an operand!?"); |
| 301 | assert(OI != CI->op_begin() && "Pointer operand is call destination?"); |
| 302 | |
| 303 | // FIXME: This is broken if the same pointer is passed to a call more |
| 304 | // than once! It will get multiple entries for the first pointer. |
| 305 | |
| 306 | // Add the operand number and pool handle to the call table... |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame^] | 307 | addCallInfo(CallMap[CI], CI, OI-CI->op_begin()-1,Scalars[i].PoolHandle); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // Print out call map... |
| 313 | for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin(); |
| 314 | I != CallMap.end(); ++I) { |
| 315 | cerr << "\nFor call: "; |
| 316 | I->first->dump(); |
| 317 | I->second.finalizeConstruction(); |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame^] | 318 | cerr << I->second.Func->getName() << " must pass pool pointer for arg #"; |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 319 | for (unsigned i = 0; i < I->second.ArgInfo.size(); ++i) |
| 320 | cerr << I->second.ArgInfo[i].first << " "; |
| 321 | cerr << "\n"; |
| 322 | } |
| 323 | |
| 324 | // Loop through all of the call nodes, recursively creating the new functions |
| 325 | // that we want to call... This uses a map to prevent infinite recursion and |
| 326 | // to avoid duplicating functions unneccesarily. |
| 327 | // |
| 328 | for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin(), |
| 329 | E = CallMap.end(); I != E; ++I) { |
| 330 | // Make sure the entries are sorted. |
| 331 | I->second.finalizeConstruction(); |
| 332 | transformFunction(I->second); |
| 333 | } |
| 334 | |
| 335 | |
| 336 | |
| 337 | } |
| 338 | |
| 339 | |
| 340 | // transformFunction - Transform the specified function the specified way. |
| 341 | // It we have already transformed that function that way, don't do anything. |
| 342 | // |
| 343 | void PoolAllocate::transformFunction(TransformFunctionInfo &TFI) { |
| 344 | if (getTransformedFunction(TFI)) return; // Function xformation already done? |
| 345 | |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame^] | 346 | Function *FuncToXForm = TFI.Func; |
| 347 | const FunctionType *OldFuncType = FuncToXForm->getFunctionType(); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame^] | 349 | assert(!OldFuncType->isVarArg() && "Vararg functions not handled yet!"); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 350 | |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame^] | 351 | // Build the type for the new function that we are transforming |
| 352 | vector<const Type*> ArgTys; |
| 353 | for (unsigned i = 0, e = OldFuncType->getNumParams(); i != e; ++i) |
| 354 | ArgTys.push_back(OldFuncType->getParamType(i)); |
| 355 | |
| 356 | // Add one pool pointer for every argument that needs to be supplemented. |
| 357 | ArgTys.insert(ArgTys.end(), TFI.ArgInfo.size(), PoolTy); |
| 358 | |
| 359 | // Build the new function type... |
| 360 | const // FIXME when types are not const |
| 361 | FunctionType *NewFuncType = FunctionType::get(OldFuncType->getReturnType(), |
| 362 | ArgTys,OldFuncType->isVarArg()); |
| 363 | |
| 364 | // The new function is internal, because we know that only we can call it. |
| 365 | // This also helps subsequent IP transformations to eliminate duplicated pool |
| 366 | // pointers. [in the future when they are implemented]. |
| 367 | // |
| 368 | Function *NewFunc = new Function(NewFuncType, true, |
| 369 | FuncToXForm->getName()+".poolxform"); |
| 370 | CurModule->getFunctionList().push_back(NewFunc); |
| 371 | |
| 372 | // Add the newly formed function to the TransformedFunctions table so that |
| 373 | // infinite recursion does not occur! |
| 374 | // |
| 375 | TransformedFunctions[TFI] = NewFunc; |
| 376 | |
| 377 | // Add arguments to the function... starting with all of the old arguments |
| 378 | vector<Value*> ArgMap; |
| 379 | for (unsigned i = 0, e = FuncToXForm->getArgumentList().size(); i != e; ++i) { |
| 380 | const FunctionArgument *OFA = FuncToXForm->getArgumentList()[i]; |
| 381 | FunctionArgument *NFA = new FunctionArgument(OFA->getType(),OFA->getName()); |
| 382 | NewFunc->getArgumentList().push_back(NFA); |
| 383 | ArgMap.push_back(NFA); // Keep track of the arguments |
| 384 | } |
| 385 | |
| 386 | // Now add all of the arguments corresponding to pools passed in... |
| 387 | for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) { |
| 388 | string Name; |
| 389 | if (TFI.ArgInfo[i].first == -1) |
| 390 | Name = "retpool"; |
| 391 | else |
| 392 | Name = ArgMap[TFI.ArgInfo[i].first]->getName(); // Get the arg name |
| 393 | FunctionArgument *NFA = new FunctionArgument(PoolTy, Name+".pool"); |
| 394 | NewFunc->getArgumentList().push_back(NFA); |
| 395 | } |
| 396 | |
| 397 | // Now clone the body of the old function into the new function... |
| 398 | CloneFunctionInto(NewFunc, FuncToXForm, ArgMap); |
| 399 | |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | |
| 403 | // CreatePools - Insert instructions into the function we are processing to |
| 404 | // create all of the memory pool objects themselves. This also inserts |
| 405 | // destruction code. Add an alloca for each pool that is allocated to the |
| 406 | // PoolDescriptors vector. |
| 407 | // |
| 408 | void PoolAllocate::CreatePools(Function *F, const vector<AllocDSNode*> &Allocs, |
| 409 | vector<AllocaInst*> &PoolDescriptors) { |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 410 | // FIXME: This should use an IP version of the UnifyAllExits pass! |
| 411 | vector<BasicBlock*> ReturnNodes; |
| 412 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 413 | if (isa<ReturnInst>((*I)->getTerminator())) |
| 414 | ReturnNodes.push_back(*I); |
| 415 | |
| 416 | |
| 417 | // Create the code that goes in the entry and exit nodes for the method... |
| 418 | vector<Instruction*> EntryNodeInsts; |
| 419 | for (unsigned i = 0, e = Allocs.size(); i != e; ++i) { |
| 420 | // Add an allocation and a free for each pool... |
| 421 | AllocaInst *PoolAlloc = new AllocaInst(PoolTy, 0, "pool"); |
| 422 | EntryNodeInsts.push_back(PoolAlloc); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 423 | PoolDescriptors.push_back(PoolAlloc); // Keep track of pool allocas |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 424 | AllocationInst *AI = Allocs[i]->getAllocation(); |
| 425 | |
| 426 | // Initialize the pool. We need to know how big each allocation is. For |
| 427 | // our purposes here, we assume we are allocating a scalar, or array of |
| 428 | // constant size. |
| 429 | // |
| 430 | unsigned ElSize = TargetData.getTypeSize(AI->getAllocatedType()); |
| 431 | ElSize *= cast<ConstantUInt>(AI->getArraySize())->getValue(); |
| 432 | |
| 433 | vector<Value*> Args; |
| 434 | Args.push_back(PoolAlloc); // Pool to initialize |
| 435 | Args.push_back(ConstantUInt::get(Type::UIntTy, ElSize)); |
| 436 | EntryNodeInsts.push_back(new CallInst(PoolInit, Args)); |
| 437 | |
| 438 | // Destroy the pool... |
| 439 | Args.pop_back(); |
| 440 | |
| 441 | for (unsigned EN = 0, ENE = ReturnNodes.size(); EN != ENE; ++EN) { |
| 442 | Instruction *Destroy = new CallInst(PoolDestroy, Args); |
| 443 | |
| 444 | // Insert it before the return instruction... |
| 445 | BasicBlock *RetNode = ReturnNodes[EN]; |
| 446 | RetNode->getInstList().insert(RetNode->end()-1, Destroy); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | // Insert the entry node code into the entry block... |
| 451 | F->getEntryNode()->getInstList().insert(F->getEntryNode()->begin()+1, |
| 452 | EntryNodeInsts.begin(), |
| 453 | EntryNodeInsts.end()); |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 457 | // addPoolPrototypes - Add prototypes for the pool methods to the specified |
| 458 | // module and update the Pool* instance variables to point to them. |
| 459 | // |
| 460 | void PoolAllocate::addPoolPrototypes(Module *M) { |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 461 | // Get PoolInit function... |
| 462 | vector<const Type*> Args; |
| 463 | Args.push_back(PoolTy); // Pool to initialize |
| 464 | Args.push_back(Type::UIntTy); // Num bytes per element |
| 465 | FunctionType *PoolInitTy = FunctionType::get(Type::VoidTy, Args, false); |
| 466 | PoolInit = M->getOrInsertFunction("poolinit", PoolInitTy); |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 467 | |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 468 | // Get pooldestroy function... |
| 469 | Args.pop_back(); // Only takes a pool... |
| 470 | FunctionType *PoolDestroyTy = FunctionType::get(Type::VoidTy, Args, false); |
| 471 | PoolDestroy = M->getOrInsertFunction("pooldestroy", PoolDestroyTy); |
| 472 | |
| 473 | const Type *PtrVoid = PointerType::get(Type::SByteTy); |
| 474 | |
| 475 | // Get the poolalloc function... |
| 476 | FunctionType *PoolAllocTy = FunctionType::get(PtrVoid, Args, false); |
| 477 | PoolAlloc = M->getOrInsertFunction("poolalloc", PoolAllocTy); |
| 478 | |
| 479 | // Get the poolfree function... |
| 480 | Args.push_back(PtrVoid); |
| 481 | FunctionType *PoolFreeTy = FunctionType::get(Type::VoidTy, Args, false); |
| 482 | PoolFree = M->getOrInsertFunction("poolfree", PoolFreeTy); |
| 483 | |
| 484 | // Add the %PoolTy type to the symbol table of the module... |
| 485 | M->addTypeName("PoolTy", PoolTy->getElementType()); |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
| 489 | bool PoolAllocate::run(Module *M) { |
| 490 | addPoolPrototypes(M); |
| 491 | CurModule = M; |
| 492 | |
| 493 | DS = &getAnalysis<DataStructure>(); |
| 494 | bool Changed = false; |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame^] | 495 | |
| 496 | // We cannot use an iterator here because it will get invalidated when we add |
| 497 | // functions to the module later... |
| 498 | for (unsigned i = 0; i != M->size(); ++i) |
| 499 | if (!M->getFunctionList()[i]->isExternal()) |
| 500 | Changed |= processFunction(M->getFunctionList()[i]); |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 501 | |
| 502 | CurModule = 0; |
| 503 | DS = 0; |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | |
| 508 | // createPoolAllocatePass - Global function to access the functionality of this |
| 509 | // pass... |
| 510 | // |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 511 | Pass *createPoolAllocatePass() { return new PoolAllocate(); } |