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 | // |
Chris Lattner | 457e1ac | 2002-04-15 22:42:23 +0000 | [diff] [blame] | 7 | // This pass requires a DCE & instcombine pass to be run after it for best |
| 8 | // results. |
| 9 | // |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #include "llvm/Transforms/IPO/PoolAllocate.h" |
Chris Lattner | 7608a46 | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 13 | #include "llvm/Transforms/Utils/CloneFunction.h" |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/DataStructureGraph.h" |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/Function.h" |
Chris Lattner | d92b01c | 2002-04-09 18:37:46 +0000 | [diff] [blame] | 17 | #include "llvm/BasicBlock.h" |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 18 | #include "llvm/iMemory.h" |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 19 | #include "llvm/iTerminators.h" |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 20 | #include "llvm/iPHINode.h" |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 21 | #include "llvm/iOther.h" |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 25 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 26 | #include "llvm/Argument.h" |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 27 | #include "Support/DepthFirstIterator.h" |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 28 | #include "Support/STLExtras.h" |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 31 | // DEBUG_CREATE_POOLS - Enable this to turn on debug output for the pool |
| 32 | // creation phase in the top level function of a transformed data structure. |
| 33 | // |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 34 | //#define DEBUG_CREATE_POOLS 1 |
| 35 | |
| 36 | // DEBUG_TRANSFORM_PROGRESS - Enable this to get lots of debug output on what |
| 37 | // the transformation is doing. |
| 38 | // |
| 39 | //#define DEBUG_TRANSFORM_PROGRESS 1 |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 457e1ac | 2002-04-15 22:42:23 +0000 | [diff] [blame] | 41 | // DEBUG_POOLBASE_LOAD_ELIMINATOR - Turn this on to get statistics about how |
| 42 | // many static loads were eliminated from a function... |
| 43 | // |
| 44 | #define DEBUG_POOLBASE_LOAD_ELIMINATOR 1 |
| 45 | |
Chris Lattner | 50e3d32 | 2002-04-13 23:13:18 +0000 | [diff] [blame] | 46 | #include "Support/CommandLine.h" |
| 47 | enum PtrSize { |
| 48 | Ptr8bits, Ptr16bits, Ptr32bits |
| 49 | }; |
| 50 | |
| 51 | static cl::Enum<enum PtrSize> ReqPointerSize("ptrsize", 0, |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 52 | "Set pointer size for -poolalloc pass", |
Chris Lattner | 50e3d32 | 2002-04-13 23:13:18 +0000 | [diff] [blame] | 53 | clEnumValN(Ptr32bits, "32", "Use 32 bit indices for pointers"), |
| 54 | clEnumValN(Ptr16bits, "16", "Use 16 bit indices for pointers"), |
| 55 | clEnumValN(Ptr8bits , "8", "Use 8 bit indices for pointers"), 0); |
| 56 | |
Chris Lattner | 457e1ac | 2002-04-15 22:42:23 +0000 | [diff] [blame] | 57 | static cl::Flag DisableRLE("no-pool-load-elim", "Disable pool load elimination after poolalloc pass", cl::Hidden); |
| 58 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 59 | const Type *POINTERTYPE; |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 60 | |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 61 | // FIXME: This is dependant on the sparc backend layout conventions!! |
| 62 | static TargetData TargetData("test"); |
| 63 | |
Chris Lattner | 50e3d32 | 2002-04-13 23:13:18 +0000 | [diff] [blame] | 64 | static const Type *getPointerTransformedType(const Type *Ty) { |
| 65 | if (PointerType *PT = dyn_cast<PointerType>(Ty)) { |
| 66 | return POINTERTYPE; |
| 67 | } else if (StructType *STy = dyn_cast<StructType>(Ty)) { |
| 68 | vector<const Type *> NewElTypes; |
| 69 | NewElTypes.reserve(STy->getElementTypes().size()); |
| 70 | for (StructType::ElementTypes::const_iterator |
| 71 | I = STy->getElementTypes().begin(), |
| 72 | E = STy->getElementTypes().end(); I != E; ++I) |
| 73 | NewElTypes.push_back(getPointerTransformedType(*I)); |
| 74 | return StructType::get(NewElTypes); |
| 75 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 76 | return ArrayType::get(getPointerTransformedType(ATy->getElementType()), |
| 77 | ATy->getNumElements()); |
| 78 | } else { |
| 79 | assert(Ty->isPrimitiveType() && "Unknown derived type!"); |
| 80 | return Ty; |
| 81 | } |
| 82 | } |
| 83 | |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 84 | namespace { |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 85 | struct PoolInfo { |
| 86 | DSNode *Node; // The node this pool allocation represents |
| 87 | Value *Handle; // LLVM value of the pool in the current context |
| 88 | const Type *NewType; // The transformed type of the memory objects |
| 89 | const Type *PoolType; // The type of the pool |
| 90 | |
| 91 | const Type *getOldType() const { return Node->getType(); } |
| 92 | |
| 93 | PoolInfo() { // Define a default ctor for map::operator[] |
| 94 | cerr << "Map subscript used to get element that doesn't exist!\n"; |
| 95 | abort(); // Invalid |
| 96 | } |
| 97 | |
| 98 | PoolInfo(DSNode *N, Value *H, const Type *NT, const Type *PT) |
| 99 | : Node(N), Handle(H), NewType(NT), PoolType(PT) { |
| 100 | // Handle can be null... |
| 101 | assert(N && NT && PT && "Pool info null!"); |
| 102 | } |
| 103 | |
| 104 | PoolInfo(DSNode *N) : Node(N), Handle(0), NewType(0), PoolType(0) { |
| 105 | assert(N && "Invalid pool info!"); |
| 106 | |
| 107 | // The new type of the memory object is the same as the old type, except |
| 108 | // that all of the pointer values are replaced with POINTERTYPE values. |
Chris Lattner | 50e3d32 | 2002-04-13 23:13:18 +0000 | [diff] [blame] | 109 | NewType = getPointerTransformedType(getOldType()); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 110 | } |
| 111 | }; |
| 112 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 113 | // ScalarInfo - Information about an LLVM value that we know points to some |
| 114 | // datastructure we are processing. |
| 115 | // |
| 116 | struct ScalarInfo { |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 117 | Value *Val; // Scalar value in Current Function |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 118 | PoolInfo Pool; // The pool the scalar points into |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 120 | ScalarInfo(Value *V, const PoolInfo &PI) : Val(V), Pool(PI) { |
| 121 | assert(V && "Null value passed to ScalarInfo ctor!"); |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 122 | } |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 125 | // CallArgInfo - Information on one operand for a call that got expanded. |
| 126 | struct CallArgInfo { |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 127 | int ArgNo; // Call argument number this corresponds to |
| 128 | DSNode *Node; // The graph node for the pool |
| 129 | Value *PoolHandle; // The LLVM value that is the pool pointer |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 130 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 131 | CallArgInfo(int Arg, DSNode *N, Value *PH) |
| 132 | : ArgNo(Arg), Node(N), PoolHandle(PH) { |
| 133 | assert(Arg >= -1 && N && PH && "Illegal values to CallArgInfo ctor!"); |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 136 | // operator< when sorting, sort by argument number. |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 137 | bool operator<(const CallArgInfo &CAI) const { |
| 138 | return ArgNo < CAI.ArgNo; |
| 139 | } |
| 140 | }; |
| 141 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 142 | // TransformFunctionInfo - Information about how a function eeds to be |
| 143 | // transformed. |
| 144 | // |
| 145 | struct TransformFunctionInfo { |
| 146 | // ArgInfo - Maintain information about the arguments that need to be |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 147 | // processed. Each CallArgInfo corresponds to an argument that needs to |
| 148 | // have a pool pointer passed into the transformed function with it. |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 149 | // |
| 150 | // As a special case, "argument" number -1 corresponds to the return value. |
| 151 | // |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 152 | vector<CallArgInfo> ArgInfo; |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 153 | |
| 154 | // Func - The function to be transformed... |
| 155 | Function *Func; |
| 156 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 157 | // The call instruction that is used to map CallArgInfo PoolHandle values |
| 158 | // into the new function values. |
| 159 | CallInst *Call; |
| 160 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 161 | // default ctor... |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 162 | TransformFunctionInfo() : Func(0), Call(0) {} |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 164 | bool operator<(const TransformFunctionInfo &TFI) const { |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 165 | if (Func < TFI.Func) return true; |
| 166 | if (Func > TFI.Func) return false; |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 167 | if (ArgInfo.size() < TFI.ArgInfo.size()) return true; |
| 168 | if (ArgInfo.size() > TFI.ArgInfo.size()) return false; |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 169 | return ArgInfo < TFI.ArgInfo; |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void finalizeConstruction() { |
| 173 | // 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] | 174 | // argument records, in order. Note that this must be a stable sort so |
| 175 | // that the entries with the same sorting criteria (ie they are multiple |
| 176 | // pool entries for the same argument) are kept in depth first order. |
| 177 | stable_sort(ArgInfo.begin(), ArgInfo.end()); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 178 | } |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 179 | |
| 180 | // addCallInfo - For a specified function call CI, figure out which pool |
| 181 | // descriptors need to be passed in as arguments, and which arguments need |
| 182 | // to be transformed into indices. If Arg != -1, the specified call |
| 183 | // argument is passed in as a pointer to a data structure. |
| 184 | // |
| 185 | void addCallInfo(DataStructure *DS, CallInst *CI, int Arg, |
| 186 | DSNode *GraphNode, map<DSNode*, PoolInfo> &PoolDescs); |
| 187 | |
| 188 | // Make sure that all dependant arguments are added to this transformation |
| 189 | // info. For example, if we call foo(null, P) and foo treats it's first and |
| 190 | // second arguments as belonging to the same data structure, the we MUST add |
| 191 | // entries to know that the null needs to be transformed into an index as |
| 192 | // well. |
| 193 | // |
| 194 | void ensureDependantArgumentsIncluded(DataStructure *DS, |
| 195 | map<DSNode*, PoolInfo> &PoolDescs); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | |
| 199 | // Define the pass class that we implement... |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 200 | struct PoolAllocate : public Pass { |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 201 | const char *getPassName() const { return "Pool Allocate"; } |
| 202 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 203 | PoolAllocate() { |
Chris Lattner | 50e3d32 | 2002-04-13 23:13:18 +0000 | [diff] [blame] | 204 | switch (ReqPointerSize) { |
| 205 | case Ptr32bits: POINTERTYPE = Type::UIntTy; break; |
| 206 | case Ptr16bits: POINTERTYPE = Type::UShortTy; break; |
| 207 | case Ptr8bits: POINTERTYPE = Type::UByteTy; break; |
| 208 | } |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 209 | |
| 210 | CurModule = 0; DS = 0; |
| 211 | PoolInit = PoolDestroy = PoolAlloc = PoolFree = 0; |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 214 | // getPoolType - Get the type used by the backend for a pool of a particular |
| 215 | // type. This pool record is used to allocate nodes of type NodeType. |
| 216 | // |
| 217 | // Here, PoolTy = { NodeType*, sbyte*, uint }* |
| 218 | // |
| 219 | const StructType *getPoolType(const Type *NodeType) { |
| 220 | vector<const Type*> PoolElements; |
| 221 | PoolElements.push_back(PointerType::get(NodeType)); |
| 222 | PoolElements.push_back(PointerType::get(Type::SByteTy)); |
| 223 | PoolElements.push_back(Type::UIntTy); |
Chris Lattner | 8f796d6 | 2002-04-13 19:25:57 +0000 | [diff] [blame] | 224 | StructType *Result = StructType::get(PoolElements); |
| 225 | |
| 226 | // Add a name to the symbol table to correspond to the backend |
| 227 | // representation of this pool... |
| 228 | assert(CurModule && "No current module!?"); |
| 229 | string Name = CurModule->getTypeName(NodeType); |
| 230 | if (Name.empty()) Name = CurModule->getTypeName(PoolElements[0]); |
| 231 | CurModule->addTypeName(Name+"oolbe", Result); |
| 232 | |
| 233 | return Result; |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 236 | bool run(Module *M); |
| 237 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 238 | // getAnalysisUsage - This function requires data structure information |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 239 | // to be able to see what is pool allocatable. |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 240 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 241 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 242 | AU.addRequired(DataStructure::ID); |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 243 | } |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 244 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 245 | public: |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 246 | // CurModule - The module being processed. |
| 247 | Module *CurModule; |
| 248 | |
| 249 | // DS - The data structure graph for the module being processed. |
| 250 | DataStructure *DS; |
| 251 | |
| 252 | // Prototypes that we add to support pool allocation... |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 253 | Function *PoolInit, *PoolDestroy, *PoolAlloc, *PoolAllocArray, *PoolFree; |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 254 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 255 | // The map of already transformed functions... note that the keys of this |
| 256 | // map do not have meaningful values for 'Call' or the 'PoolHandle' elements |
| 257 | // of the ArgInfo elements. |
| 258 | // |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 259 | map<TransformFunctionInfo, Function*> TransformedFunctions; |
| 260 | |
| 261 | // getTransformedFunction - Get a transformed function, or return null if |
| 262 | // the function specified hasn't been transformed yet. |
| 263 | // |
| 264 | Function *getTransformedFunction(TransformFunctionInfo &TFI) const { |
| 265 | map<TransformFunctionInfo, Function*>::const_iterator I = |
| 266 | TransformedFunctions.find(TFI); |
| 267 | if (I != TransformedFunctions.end()) return I->second; |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 272 | // addPoolPrototypes - Add prototypes for the pool functions to the |
| 273 | // specified module and update the Pool* instance variables to point to |
| 274 | // them. |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 275 | // |
| 276 | void addPoolPrototypes(Module *M); |
| 277 | |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 278 | |
| 279 | // CreatePools - Insert instructions into the function we are processing to |
| 280 | // create all of the memory pool objects themselves. This also inserts |
| 281 | // destruction code. Add an alloca for each pool that is allocated to the |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 282 | // PoolDescs map. |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 283 | // |
| 284 | void CreatePools(Function *F, const vector<AllocDSNode*> &Allocs, |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 285 | map<DSNode*, PoolInfo> &PoolDescs); |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 287 | // processFunction - Convert a function to use pool allocation where |
| 288 | // available. |
| 289 | // |
| 290 | bool processFunction(Function *F); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 291 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 292 | // transformFunctionBody - This transforms the instruction in 'F' to use the |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 293 | // pools specified in PoolDescs when modifying data structure nodes |
| 294 | // specified in the PoolDescs map. IPFGraph is the closed data structure |
| 295 | // graph for F, of which the PoolDescriptor nodes come from. |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 296 | // |
| 297 | void transformFunctionBody(Function *F, FunctionDSGraph &IPFGraph, |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 298 | map<DSNode*, PoolInfo> &PoolDescs); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 299 | |
| 300 | // transformFunction - Transform the specified function the specified way. |
| 301 | // 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] | 302 | // The nodes in the TransformFunctionInfo come out of callers data structure |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 303 | // graph, and the PoolDescs passed in are the caller's. |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 304 | // |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 305 | void transformFunction(TransformFunctionInfo &TFI, |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 306 | FunctionDSGraph &CallerIPGraph, |
| 307 | map<DSNode*, PoolInfo> &PoolDescs); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 309 | }; |
| 310 | } |
| 311 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 312 | // isNotPoolableAlloc - This is a predicate that returns true if the specified |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 313 | // allocation node in a data structure graph is eligable for pool allocation. |
| 314 | // |
| 315 | static bool isNotPoolableAlloc(const AllocDSNode *DS) { |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 316 | if (DS->isAllocaNode()) return true; // Do not pool allocate alloca's. |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 317 | return false; |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 320 | // processFunction - Convert a function to use pool allocation where |
| 321 | // available. |
| 322 | // |
| 323 | bool PoolAllocate::processFunction(Function *F) { |
| 324 | // Get the closed datastructure graph for the current function... if there are |
| 325 | // any allocations in this graph that are not escaping, we need to pool |
| 326 | // allocate them here! |
| 327 | // |
| 328 | FunctionDSGraph &IPGraph = DS->getClosedDSGraph(F); |
| 329 | |
| 330 | // Get all of the allocations that do not escape the current function. Since |
| 331 | // they are still live (they exist in the graph at all), this means we must |
| 332 | // have scalar references to these nodes, but the scalars are never returned. |
| 333 | // |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 334 | vector<AllocDSNode*> Allocs; |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 335 | IPGraph.getNonEscapingAllocations(Allocs); |
| 336 | |
| 337 | // Filter out allocations that we cannot handle. Currently, this includes |
| 338 | // variable sized array allocations and alloca's (which we do not want to |
| 339 | // pool allocate) |
| 340 | // |
| 341 | Allocs.erase(remove_if(Allocs.begin(), Allocs.end(), isNotPoolableAlloc), |
| 342 | Allocs.end()); |
| 343 | |
| 344 | |
| 345 | if (Allocs.empty()) return false; // Nothing to do. |
| 346 | |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 347 | #ifdef DEBUG_TRANSFORM_PROGRESS |
| 348 | cerr << "Transforming Function: " << F->getName() << "\n"; |
| 349 | #endif |
| 350 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 351 | // Insert instructions into the function we are processing to create all of |
| 352 | // the memory pool objects themselves. This also inserts destruction code. |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 353 | // This fills in the PoolDescs map to associate the alloc node with the |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 354 | // allocation of the memory pool corresponding to it. |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 355 | // |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 356 | map<DSNode*, PoolInfo> PoolDescs; |
| 357 | CreatePools(F, Allocs, PoolDescs); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 358 | |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 359 | #ifdef DEBUG_TRANSFORM_PROGRESS |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 360 | cerr << "Transformed Entry Function: \n" << F; |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 361 | #endif |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 362 | |
| 363 | // Now we need to figure out what called functions we need to transform, and |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 364 | // how. To do this, we look at all of the scalars, seeing which functions are |
| 365 | // either used as a scalar value (so they return a data structure), or are |
| 366 | // passed one of our scalar values. |
| 367 | // |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 368 | transformFunctionBody(F, IPGraph, PoolDescs); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 369 | |
| 370 | return true; |
| 371 | } |
| 372 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 373 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 374 | //===----------------------------------------------------------------------===// |
| 375 | // |
| 376 | // NewInstructionCreator - This class is used to traverse the function being |
| 377 | // modified, changing each instruction visit'ed to use and provide pointer |
| 378 | // indexes instead of real pointers. This is what changes the body of a |
| 379 | // function to use pool allocation. |
| 380 | // |
| 381 | class NewInstructionCreator : public InstVisitor<NewInstructionCreator> { |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 382 | PoolAllocate &PoolAllocator; |
| 383 | vector<ScalarInfo> &Scalars; |
| 384 | map<CallInst*, TransformFunctionInfo> &CallMap; |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 385 | map<Value*, Value*> &XFormMap; // Map old pointers to new indexes |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 387 | struct RefToUpdate { |
| 388 | Instruction *I; // Instruction to update |
| 389 | unsigned OpNum; // Operand number to update |
| 390 | Value *OldVal; // The old value it had |
| 391 | |
| 392 | RefToUpdate(Instruction *i, unsigned o, Value *ov) |
| 393 | : I(i), OpNum(o), OldVal(ov) {} |
| 394 | }; |
| 395 | vector<RefToUpdate> ReferencesToUpdate; |
| 396 | |
| 397 | const ScalarInfo &getScalarRef(const Value *V) { |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 398 | for (unsigned i = 0, e = Scalars.size(); i != e; ++i) |
| 399 | if (Scalars[i].Val == V) return Scalars[i]; |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 400 | |
| 401 | cerr << "Could not find scalar " << V << " in scalar map!\n"; |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 402 | assert(0 && "Scalar not found in getScalar!"); |
| 403 | abort(); |
| 404 | return Scalars[0]; |
| 405 | } |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 406 | |
| 407 | const ScalarInfo *getScalar(const Value *V) { |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 408 | for (unsigned i = 0, e = Scalars.size(); i != e; ++i) |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 409 | if (Scalars[i].Val == V) return &Scalars[i]; |
| 410 | return 0; |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 413 | BasicBlock::iterator ReplaceInstWith(Instruction *I, Instruction *New) { |
| 414 | BasicBlock *BB = I->getParent(); |
| 415 | BasicBlock::iterator RI = find(BB->begin(), BB->end(), I); |
| 416 | BB->getInstList().replaceWith(RI, New); |
| 417 | XFormMap[I] = New; |
| 418 | return RI; |
| 419 | } |
| 420 | |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 421 | Instruction *createPoolBaseInstruction(Value *PtrVal) { |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 422 | const ScalarInfo &SC = getScalarRef(PtrVal); |
| 423 | vector<Value*> Args(3); |
| 424 | Args[0] = ConstantUInt::get(Type::UIntTy, 0); // No pointer offset |
| 425 | Args[1] = ConstantUInt::get(Type::UByteTy, 0); // Field #0 of pool descriptr |
| 426 | Args[2] = ConstantUInt::get(Type::UByteTy, 0); // Field #0 of poolalloc val |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 427 | return new LoadInst(SC.Pool.Handle, Args, PtrVal->getName()+".poolbase"); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 431 | public: |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 432 | NewInstructionCreator(PoolAllocate &PA, vector<ScalarInfo> &S, |
| 433 | map<CallInst*, TransformFunctionInfo> &C, |
| 434 | map<Value*, Value*> &X) |
| 435 | : PoolAllocator(PA), Scalars(S), CallMap(C), XFormMap(X) {} |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 437 | |
| 438 | // updateReferences - The NewInstructionCreator is responsible for creating |
| 439 | // new instructions to replace the old ones in the function, and then link up |
| 440 | // references to values to their new values. For it to do this, however, it |
| 441 | // keeps track of information about the value mapping of old values to new |
| 442 | // values that need to be patched up. Given this value map and a set of |
| 443 | // instruction operands to patch, updateReferences performs the updates. |
| 444 | // |
| 445 | void updateReferences() { |
| 446 | for (unsigned i = 0, e = ReferencesToUpdate.size(); i != e; ++i) { |
| 447 | RefToUpdate &Ref = ReferencesToUpdate[i]; |
| 448 | Value *NewVal = XFormMap[Ref.OldVal]; |
| 449 | |
| 450 | if (NewVal == 0) { |
| 451 | if (isa<Constant>(Ref.OldVal) && // Refering to a null ptr? |
| 452 | cast<Constant>(Ref.OldVal)->isNullValue()) { |
| 453 | // Transform the null pointer into a null index... caching in XFormMap |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 454 | XFormMap[Ref.OldVal] = NewVal = Constant::getNullValue(POINTERTYPE); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 455 | //} else if (isa<Argument>(Ref.OldVal)) { |
| 456 | } else { |
| 457 | cerr << "Unknown reference to: " << Ref.OldVal << "\n"; |
| 458 | assert(XFormMap[Ref.OldVal] && |
| 459 | "Reference to value that was not updated found!"); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | Ref.I->setOperand(Ref.OpNum, NewVal); |
| 464 | } |
| 465 | ReferencesToUpdate.clear(); |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 468 | //===--------------------------------------------------------------------===// |
| 469 | // Transformation methods: |
| 470 | // These methods specify how each type of instruction is transformed by the |
| 471 | // NewInstructionCreator instance... |
| 472 | //===--------------------------------------------------------------------===// |
| 473 | |
| 474 | void visitGetElementPtrInst(GetElementPtrInst *I) { |
| 475 | assert(0 && "Cannot transform get element ptr instructions yet!"); |
| 476 | } |
| 477 | |
| 478 | // Replace the load instruction with a new one. |
| 479 | void visitLoadInst(LoadInst *I) { |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 480 | vector<Instruction *> BeforeInsts; |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 481 | |
| 482 | // Cast our index to be a UIntTy so we can use it to index into the pool... |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 483 | CastInst *Index = new CastInst(Constant::getNullValue(POINTERTYPE), |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 484 | Type::UIntTy, I->getOperand(0)->getName()); |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 485 | BeforeInsts.push_back(Index); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 486 | ReferencesToUpdate.push_back(RefToUpdate(Index, 0, I->getOperand(0))); |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 487 | |
| 488 | // Include the pool base instruction... |
| 489 | Instruction *PoolBase = createPoolBaseInstruction(I->getOperand(0)); |
| 490 | BeforeInsts.push_back(PoolBase); |
| 491 | |
| 492 | Instruction *IdxInst = |
| 493 | BinaryOperator::create(Instruction::Add, *I->idx_begin(), Index, |
| 494 | I->getName()+".idx"); |
| 495 | BeforeInsts.push_back(IdxInst); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 496 | |
| 497 | vector<Value*> Indices(I->idx_begin(), I->idx_end()); |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 498 | Indices[0] = IdxInst; |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 499 | Instruction *Address = new GetElementPtrInst(PoolBase, Indices, |
| 500 | I->getName()+".addr"); |
| 501 | BeforeInsts.push_back(Address); |
| 502 | |
| 503 | Instruction *NewLoad = new LoadInst(Address, I->getName()); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 504 | |
| 505 | // Replace the load instruction with the new load instruction... |
| 506 | BasicBlock::iterator II = ReplaceInstWith(I, NewLoad); |
| 507 | |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 508 | // Add all of the instructions before the load... |
| 509 | NewLoad->getParent()->getInstList().insert(II, BeforeInsts.begin(), |
| 510 | BeforeInsts.end()); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 511 | |
| 512 | // If not yielding a pool allocated pointer, use the new load value as the |
| 513 | // value in the program instead of the old load value... |
| 514 | // |
| 515 | if (!getScalar(I)) |
| 516 | I->replaceAllUsesWith(NewLoad); |
| 517 | } |
| 518 | |
| 519 | // Replace the store instruction with a new one. In the store instruction, |
| 520 | // the value stored could be a pointer type, meaning that the new store may |
| 521 | // have to change one or both of it's operands. |
| 522 | // |
| 523 | void visitStoreInst(StoreInst *I) { |
| 524 | assert(getScalar(I->getOperand(1)) && |
| 525 | "Store inst found only storing pool allocated pointer. " |
| 526 | "Not imp yet!"); |
| 527 | |
| 528 | Value *Val = I->getOperand(0); // The value to store... |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 529 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 530 | // Check to see if the value we are storing is a data structure pointer... |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 531 | //if (const ScalarInfo *ValScalar = getScalar(I->getOperand(0))) |
| 532 | if (isa<PointerType>(I->getOperand(0)->getType())) |
| 533 | Val = Constant::getNullValue(POINTERTYPE); // Yes, store a dummy |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 534 | |
| 535 | Instruction *PoolBase = createPoolBaseInstruction(I->getOperand(1)); |
| 536 | |
| 537 | // Cast our index to be a UIntTy so we can use it to index into the pool... |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 538 | CastInst *Index = new CastInst(Constant::getNullValue(POINTERTYPE), |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 539 | Type::UIntTy, I->getOperand(1)->getName()); |
| 540 | ReferencesToUpdate.push_back(RefToUpdate(Index, 0, I->getOperand(1))); |
| 541 | |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 542 | // Instructions to add after the Index... |
| 543 | vector<Instruction*> AfterInsts; |
| 544 | |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 545 | Instruction *IdxInst = |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 546 | BinaryOperator::create(Instruction::Add, *I->idx_begin(), Index, "idx"); |
| 547 | AfterInsts.push_back(IdxInst); |
| 548 | |
| 549 | vector<Value*> Indices(I->idx_begin(), I->idx_end()); |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 550 | Indices[0] = IdxInst; |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 551 | Instruction *Address = new GetElementPtrInst(PoolBase, Indices, |
| 552 | I->getName()+"storeaddr"); |
| 553 | AfterInsts.push_back(Address); |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 555 | Instruction *NewStore = new StoreInst(Val, Address); |
| 556 | AfterInsts.push_back(NewStore); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 557 | if (Val != I->getOperand(0)) // Value stored was a pointer? |
| 558 | ReferencesToUpdate.push_back(RefToUpdate(NewStore, 0, I->getOperand(0))); |
| 559 | |
| 560 | |
| 561 | // Replace the store instruction with the cast instruction... |
| 562 | BasicBlock::iterator II = ReplaceInstWith(I, Index); |
| 563 | |
| 564 | // Add the pool base calculator instruction before the index... |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 565 | II = Index->getParent()->getInstList().insert(II, PoolBase)+2; |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 566 | |
Chris Lattner | 39db871 | 2002-05-02 17:38:14 +0000 | [diff] [blame] | 567 | // Add the instructions that go after the index... |
| 568 | Index->getParent()->getInstList().insert(II, AfterInsts.begin(), |
| 569 | AfterInsts.end()); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | |
| 573 | // Create call to poolalloc for every malloc instruction |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 574 | void visitMallocInst(MallocInst *I) { |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 575 | const ScalarInfo &SCI = getScalarRef(I); |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 576 | vector<Value*> Args; |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 577 | |
| 578 | CallInst *Call; |
| 579 | if (!I->isArrayAllocation()) { |
| 580 | Args.push_back(SCI.Pool.Handle); |
| 581 | Call = new CallInst(PoolAllocator.PoolAlloc, Args, I->getName()); |
| 582 | } else { |
| 583 | Args.push_back(I->getArraySize()); |
| 584 | Args.push_back(SCI.Pool.Handle); |
| 585 | Call = new CallInst(PoolAllocator.PoolAllocArray, Args, I->getName()); |
| 586 | } |
| 587 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 588 | ReplaceInstWith(I, Call); |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 591 | // Convert a call to poolfree for every free instruction... |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 592 | void visitFreeInst(FreeInst *I) { |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 593 | // Create a new call to poolfree before the free instruction |
| 594 | vector<Value*> Args; |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 595 | Args.push_back(Constant::getNullValue(POINTERTYPE)); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 596 | Args.push_back(getScalarRef(I->getOperand(0)).Pool.Handle); |
| 597 | Instruction *NewCall = new CallInst(PoolAllocator.PoolFree, Args); |
| 598 | ReplaceInstWith(I, NewCall); |
Chris Lattner | 271255b | 2002-04-18 22:11:30 +0000 | [diff] [blame] | 599 | ReferencesToUpdate.push_back(RefToUpdate(NewCall, 1, I->getOperand(0))); |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | // visitCallInst - Create a new call instruction with the extra arguments for |
| 603 | // all of the memory pools that the call needs. |
| 604 | // |
| 605 | void visitCallInst(CallInst *I) { |
| 606 | TransformFunctionInfo &TI = CallMap[I]; |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 607 | |
| 608 | // Start with all of the old arguments... |
| 609 | vector<Value*> Args(I->op_begin()+1, I->op_end()); |
| 610 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 611 | for (unsigned i = 0, e = TI.ArgInfo.size(); i != e; ++i) { |
| 612 | // Replace all of the pointer arguments with our new pointer typed values. |
| 613 | if (TI.ArgInfo[i].ArgNo != -1) |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 614 | Args[TI.ArgInfo[i].ArgNo] = Constant::getNullValue(POINTERTYPE); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 615 | |
| 616 | // Add all of the pool arguments... |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 617 | Args.push_back(TI.ArgInfo[i].PoolHandle); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 618 | } |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 619 | |
| 620 | Function *NF = PoolAllocator.getTransformedFunction(TI); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 621 | Instruction *NewCall = new CallInst(NF, Args, I->getName()); |
| 622 | ReplaceInstWith(I, NewCall); |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 623 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 624 | // Keep track of the mapping of operands so that we can resolve them to real |
| 625 | // values later. |
| 626 | Value *RetVal = NewCall; |
| 627 | for (unsigned i = 0, e = TI.ArgInfo.size(); i != e; ++i) |
| 628 | if (TI.ArgInfo[i].ArgNo != -1) |
| 629 | ReferencesToUpdate.push_back(RefToUpdate(NewCall, TI.ArgInfo[i].ArgNo+1, |
| 630 | I->getOperand(TI.ArgInfo[i].ArgNo+1))); |
| 631 | else |
| 632 | RetVal = 0; // If returning a pointer, don't change retval... |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 633 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 634 | // If not returning a pointer, use the new call as the value in the program |
| 635 | // instead of the old call... |
| 636 | // |
| 637 | if (RetVal) |
| 638 | I->replaceAllUsesWith(RetVal); |
| 639 | } |
| 640 | |
| 641 | // visitPHINode - Create a new PHI node of POINTERTYPE for all of the old Phi |
| 642 | // nodes... |
| 643 | // |
| 644 | void visitPHINode(PHINode *PN) { |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 645 | Value *DummyVal = Constant::getNullValue(POINTERTYPE); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 646 | PHINode *NewPhi = new PHINode(POINTERTYPE, PN->getName()); |
| 647 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 648 | NewPhi->addIncoming(DummyVal, PN->getIncomingBlock(i)); |
| 649 | ReferencesToUpdate.push_back(RefToUpdate(NewPhi, i*2, |
| 650 | PN->getIncomingValue(i))); |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 653 | ReplaceInstWith(PN, NewPhi); |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 656 | // visitReturnInst - Replace ret instruction with a new return... |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 657 | void visitReturnInst(ReturnInst *I) { |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 658 | Instruction *Ret = new ReturnInst(Constant::getNullValue(POINTERTYPE)); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 659 | ReplaceInstWith(I, Ret); |
| 660 | ReferencesToUpdate.push_back(RefToUpdate(Ret, 0, I->getOperand(0))); |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 663 | // visitSetCondInst - Replace a conditional test instruction with a new one |
Chris Lattner | cf09a2a | 2002-04-01 00:45:33 +0000 | [diff] [blame] | 664 | void visitSetCondInst(SetCondInst *SCI) { |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 665 | BinaryOperator *I = (BinaryOperator*)SCI; |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 666 | Value *DummyVal = Constant::getNullValue(POINTERTYPE); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 667 | BinaryOperator *New = BinaryOperator::create(I->getOpcode(), DummyVal, |
| 668 | DummyVal, I->getName()); |
| 669 | ReplaceInstWith(I, New); |
| 670 | |
| 671 | ReferencesToUpdate.push_back(RefToUpdate(New, 0, I->getOperand(0))); |
| 672 | ReferencesToUpdate.push_back(RefToUpdate(New, 1, I->getOperand(1))); |
| 673 | |
| 674 | // Make sure branches refer to the new condition... |
| 675 | I->replaceAllUsesWith(New); |
Chris Lattner | cf09a2a | 2002-04-01 00:45:33 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 678 | void visitInstruction(Instruction *I) { |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 679 | cerr << "Unknown instruction to FunctionBodyTransformer:\n" << I; |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 680 | } |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 681 | }; |
| 682 | |
| 683 | |
Chris Lattner | 457e1ac | 2002-04-15 22:42:23 +0000 | [diff] [blame] | 684 | // PoolBaseLoadEliminator - Every load and store through a pool allocated |
| 685 | // pointer causes a load of the real pool base out of the pool descriptor. |
| 686 | // Iterate through the function, doing a local elimination pass of duplicate |
| 687 | // loads. This attempts to turn the all too common: |
| 688 | // |
| 689 | // %reg109.poolbase22 = load %root.pool* %root.pool, uint 0, ubyte 0, ubyte 0 |
| 690 | // %reg207 = load %root.p* %reg109.poolbase22, uint %reg109, ubyte 0, ubyte 0 |
| 691 | // %reg109.poolbase23 = load %root.pool* %root.pool, uint 0, ubyte 0, ubyte 0 |
| 692 | // store double %reg207, %root.p* %reg109.poolbase23, uint %reg109, ... |
| 693 | // |
| 694 | // into: |
| 695 | // %reg109.poolbase22 = load %root.pool* %root.pool, uint 0, ubyte 0, ubyte 0 |
| 696 | // %reg207 = load %root.p* %reg109.poolbase22, uint %reg109, ubyte 0, ubyte 0 |
| 697 | // store double %reg207, %root.p* %reg109.poolbase22, uint %reg109, ... |
| 698 | // |
| 699 | // |
| 700 | class PoolBaseLoadEliminator : public InstVisitor<PoolBaseLoadEliminator> { |
| 701 | // PoolDescValues - Keep track of the values in the current function that are |
| 702 | // pool descriptors (loads from which we want to eliminate). |
| 703 | // |
| 704 | vector<Value*> PoolDescValues; |
| 705 | |
| 706 | // PoolDescMap - As we are analyzing a BB, keep track of which load to use |
| 707 | // when referencing a pool descriptor. |
| 708 | // |
| 709 | map<Value*, LoadInst*> PoolDescMap; |
| 710 | |
| 711 | // These two fields keep track of statistics of how effective we are, if |
| 712 | // debugging is enabled. |
| 713 | // |
| 714 | unsigned Eliminated, Remaining; |
| 715 | public: |
| 716 | // Compact the pool descriptor map into a list of the pool descriptors in the |
| 717 | // current context that we should know about... |
| 718 | // |
| 719 | PoolBaseLoadEliminator(const map<DSNode*, PoolInfo> &PoolDescs) { |
| 720 | Eliminated = Remaining = 0; |
| 721 | for (map<DSNode*, PoolInfo>::const_iterator I = PoolDescs.begin(), |
| 722 | E = PoolDescs.end(); I != E; ++I) |
| 723 | PoolDescValues.push_back(I->second.Handle); |
| 724 | |
| 725 | // Remove duplicates from the list of pool values |
| 726 | sort(PoolDescValues.begin(), PoolDescValues.end()); |
| 727 | PoolDescValues.erase(unique(PoolDescValues.begin(), PoolDescValues.end()), |
| 728 | PoolDescValues.end()); |
| 729 | } |
| 730 | |
| 731 | #ifdef DEBUG_POOLBASE_LOAD_ELIMINATOR |
| 732 | void visitFunction(Function *F) { |
| 733 | cerr << "Pool Load Elim '" << F->getName() << "'\t"; |
| 734 | } |
| 735 | ~PoolBaseLoadEliminator() { |
| 736 | unsigned Total = Eliminated+Remaining; |
| 737 | if (Total) |
| 738 | cerr << "removed " << Eliminated << "[" |
| 739 | << Eliminated*100/Total << "%] loads, leaving " |
| 740 | << Remaining << ".\n"; |
| 741 | } |
| 742 | #endif |
| 743 | |
| 744 | // Loop over the function, looking for loads to eliminate. Because we are a |
| 745 | // local transformation, we reset all of our state when we enter a new basic |
| 746 | // block. |
| 747 | // |
| 748 | void visitBasicBlock(BasicBlock *) { |
| 749 | PoolDescMap.clear(); // Forget state. |
| 750 | } |
| 751 | |
| 752 | // Starting with an empty basic block, we scan it looking for loads of the |
| 753 | // pool descriptor. When we find a load, we add it to the PoolDescMap, |
| 754 | // indicating that we have a value available to recycle next time we see the |
| 755 | // poolbase of this instruction being loaded. |
| 756 | // |
| 757 | void visitLoadInst(LoadInst *LI) { |
| 758 | Value *LoadAddr = LI->getPointerOperand(); |
| 759 | map<Value*, LoadInst*>::iterator VIt = PoolDescMap.find(LoadAddr); |
| 760 | if (VIt != PoolDescMap.end()) { // We already have a value for this load? |
| 761 | LI->replaceAllUsesWith(VIt->second); // Make the current load dead |
| 762 | ++Eliminated; |
| 763 | } else { |
| 764 | // This load might not be a load of a pool pointer, check to see if it is |
| 765 | if (LI->getNumOperands() == 4 && // load pool, uint 0, ubyte 0, ubyte 0 |
| 766 | find(PoolDescValues.begin(), PoolDescValues.end(), LoadAddr) != |
| 767 | PoolDescValues.end()) { |
| 768 | |
| 769 | assert("Make sure it's a load of the pool base, not a chaining field" && |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 770 | LI->getOperand(1) == Constant::getNullValue(Type::UIntTy) && |
| 771 | LI->getOperand(2) == Constant::getNullValue(Type::UByteTy) && |
| 772 | LI->getOperand(3) == Constant::getNullValue(Type::UByteTy)); |
Chris Lattner | 457e1ac | 2002-04-15 22:42:23 +0000 | [diff] [blame] | 773 | |
| 774 | // If it is a load of a pool base, keep track of it for future reference |
| 775 | PoolDescMap.insert(make_pair(LoadAddr, LI)); |
| 776 | ++Remaining; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | // If we run across a function call, forget all state... Calls to |
| 782 | // poolalloc/poolfree can invalidate the pool base pointer, so it should be |
| 783 | // reloaded the next time it is used. Furthermore, a call to a random |
| 784 | // function might call one of these functions, so be conservative. Through |
| 785 | // more analysis, this could be improved in the future. |
| 786 | // |
| 787 | void visitCallInst(CallInst *) { |
| 788 | PoolDescMap.clear(); |
| 789 | } |
| 790 | }; |
| 791 | |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 792 | static void addNodeMapping(DSNode *SrcNode, const PointerValSet &PVS, |
| 793 | map<DSNode*, PointerValSet> &NodeMapping) { |
| 794 | for (unsigned i = 0, e = PVS.size(); i != e; ++i) |
| 795 | if (NodeMapping[SrcNode].add(PVS[i])) { // Not in map yet? |
| 796 | assert(PVS[i].Index == 0 && "Node indexing not supported yet!"); |
| 797 | DSNode *DestNode = PVS[i].Node; |
| 798 | |
| 799 | // Loop over all of the outgoing links in the mapped graph |
| 800 | for (unsigned l = 0, le = DestNode->getNumOutgoingLinks(); l != le; ++l) { |
| 801 | PointerValSet &SrcSet = SrcNode->getOutgoingLink(l); |
| 802 | const PointerValSet &DestSet = DestNode->getOutgoingLink(l); |
| 803 | |
| 804 | // Add all of the node mappings now! |
| 805 | for (unsigned si = 0, se = SrcSet.size(); si != se; ++si) { |
| 806 | assert(SrcSet[si].Index == 0 && "Can't handle node offset!"); |
| 807 | addNodeMapping(SrcSet[si].Node, DestSet, NodeMapping); |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | // CalculateNodeMapping - There is a partial isomorphism between the graph |
| 814 | // passed in and the graph that is actually used by the function. We need to |
| 815 | // figure out what this mapping is so that we can transformFunctionBody the |
| 816 | // instructions in the function itself. Note that every node in the graph that |
| 817 | // we are interested in must be both in the local graph of the called function, |
| 818 | // and in the local graph of the calling function. Because of this, we only |
| 819 | // define the mapping for these nodes [conveniently these are the only nodes we |
| 820 | // CAN define a mapping for...] |
| 821 | // |
| 822 | // The roots of the graph that we are transforming is rooted in the arguments |
| 823 | // passed into the function from the caller. This is where we start our |
| 824 | // mapping calculation. |
| 825 | // |
| 826 | // The NodeMapping calculated maps from the callers graph to the called graph. |
| 827 | // |
| 828 | static void CalculateNodeMapping(Function *F, TransformFunctionInfo &TFI, |
| 829 | FunctionDSGraph &CallerGraph, |
| 830 | FunctionDSGraph &CalledGraph, |
| 831 | map<DSNode*, PointerValSet> &NodeMapping) { |
| 832 | int LastArgNo = -2; |
| 833 | for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) { |
| 834 | // Figure out what nodes in the called graph the TFI.ArgInfo[i].Node node |
| 835 | // corresponds to... |
| 836 | // |
| 837 | // Only consider first node of sequence. Extra nodes may may be added |
| 838 | // to the TFI if the data structure requires more nodes than just the |
| 839 | // one the argument points to. We are only interested in the one the |
| 840 | // argument points to though. |
| 841 | // |
| 842 | if (TFI.ArgInfo[i].ArgNo != LastArgNo) { |
| 843 | if (TFI.ArgInfo[i].ArgNo == -1) { |
| 844 | addNodeMapping(TFI.ArgInfo[i].Node, CalledGraph.getRetNodes(), |
| 845 | NodeMapping); |
| 846 | } else { |
| 847 | // Figure out which node argument # ArgNo points to in the called graph. |
| 848 | Value *Arg = F->getArgumentList()[TFI.ArgInfo[i].ArgNo]; |
| 849 | addNodeMapping(TFI.ArgInfo[i].Node, CalledGraph.getValueMap()[Arg], |
| 850 | NodeMapping); |
| 851 | } |
| 852 | LastArgNo = TFI.ArgInfo[i].ArgNo; |
| 853 | } |
| 854 | } |
| 855 | } |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 856 | |
| 857 | |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 858 | |
| 859 | |
| 860 | // addCallInfo - For a specified function call CI, figure out which pool |
| 861 | // descriptors need to be passed in as arguments, and which arguments need to be |
| 862 | // transformed into indices. If Arg != -1, the specified call argument is |
| 863 | // passed in as a pointer to a data structure. |
| 864 | // |
| 865 | void TransformFunctionInfo::addCallInfo(DataStructure *DS, CallInst *CI, |
| 866 | int Arg, DSNode *GraphNode, |
| 867 | map<DSNode*, PoolInfo> &PoolDescs) { |
Chris Lattner | 0dc225c | 2002-03-31 07:17:46 +0000 | [diff] [blame] | 868 | assert(CI->getCalledFunction() && "Cannot handle indirect calls yet!"); |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 869 | assert(Func == 0 || Func == CI->getCalledFunction() && |
Chris Lattner | 0dc225c | 2002-03-31 07:17:46 +0000 | [diff] [blame] | 870 | "Function call record should always call the same function!"); |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 871 | assert(Call == 0 || Call == CI && |
Chris Lattner | 0dc225c | 2002-03-31 07:17:46 +0000 | [diff] [blame] | 872 | "Call element already filled in with different value!"); |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 873 | Func = CI->getCalledFunction(); |
| 874 | Call = CI; |
| 875 | //FunctionDSGraph &CalledGraph = DS->getClosedDSGraph(Func); |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 876 | |
| 877 | // For now, add the entire graph that is pointed to by the call argument. |
| 878 | // This graph can and should be pruned to only what the function itself will |
| 879 | // use, because often this will be a dramatically smaller subset of what we |
| 880 | // are providing. |
| 881 | // |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 882 | // FIXME: This should use pool links instead of extra arguments! |
| 883 | // |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 884 | for (df_iterator<DSNode*> I = df_begin(GraphNode), E = df_end(GraphNode); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 885 | I != E; ++I) |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 886 | ArgInfo.push_back(CallArgInfo(Arg, *I, PoolDescs[*I].Handle)); |
| 887 | } |
| 888 | |
| 889 | static void markReachableNodes(const PointerValSet &Vals, |
| 890 | set<DSNode*> &ReachableNodes) { |
| 891 | for (unsigned n = 0, ne = Vals.size(); n != ne; ++n) { |
| 892 | DSNode *N = Vals[n].Node; |
| 893 | if (ReachableNodes.count(N) == 0) // Haven't already processed node? |
| 894 | ReachableNodes.insert(df_begin(N), df_end(N)); // Insert all |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | // Make sure that all dependant arguments are added to this transformation info. |
| 899 | // For example, if we call foo(null, P) and foo treats it's first and second |
| 900 | // arguments as belonging to the same data structure, the we MUST add entries to |
| 901 | // know that the null needs to be transformed into an index as well. |
| 902 | // |
| 903 | void TransformFunctionInfo::ensureDependantArgumentsIncluded(DataStructure *DS, |
| 904 | map<DSNode*, PoolInfo> &PoolDescs) { |
| 905 | // FIXME: This does not work for indirect function calls!!! |
| 906 | if (Func == 0) return; // FIXME! |
| 907 | |
| 908 | // Make sure argument entries are sorted. |
| 909 | finalizeConstruction(); |
| 910 | |
| 911 | // Loop over the function signature, checking to see if there are any pointer |
| 912 | // arguments that we do not convert... if there is something we haven't |
| 913 | // converted, set done to false. |
| 914 | // |
| 915 | unsigned PtrNo = 0; |
| 916 | bool Done = true; |
| 917 | if (isa<PointerType>(Func->getReturnType())) // Make sure we convert retval |
| 918 | if (PtrNo < ArgInfo.size() && ArgInfo[PtrNo++].ArgNo == -1) { |
| 919 | // We DO transform the ret val... skip all possible entries for retval |
| 920 | while (PtrNo < ArgInfo.size() && ArgInfo[PtrNo].ArgNo == -1) |
| 921 | PtrNo++; |
| 922 | } else { |
| 923 | Done = false; |
| 924 | } |
| 925 | |
| 926 | for (unsigned i = 0, e = Func->getArgumentList().size(); i != e; ++i) { |
| 927 | Argument *Arg = Func->getArgumentList()[i]; |
| 928 | if (isa<PointerType>(Arg->getType())) { |
| 929 | if (PtrNo < ArgInfo.size() && ArgInfo[PtrNo++].ArgNo == (int)i) { |
| 930 | // We DO transform this arg... skip all possible entries for argument |
| 931 | while (PtrNo < ArgInfo.size() && ArgInfo[PtrNo].ArgNo == (int)i) |
| 932 | PtrNo++; |
| 933 | } else { |
| 934 | Done = false; |
| 935 | break; |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | // If we already have entries for all pointer arguments and retvals, there |
| 941 | // certainly is no work to do. Bail out early to avoid building relatively |
| 942 | // expensive data structures. |
| 943 | // |
| 944 | if (Done) return; |
| 945 | |
| 946 | #ifdef DEBUG_TRANSFORM_PROGRESS |
| 947 | cerr << "Must ensure dependant arguments for: " << Func->getName() << "\n"; |
| 948 | #endif |
| 949 | |
| 950 | // Otherwise, we MIGHT have to add the arguments/retval if they are part of |
| 951 | // the same datastructure graph as some other argument or retval that we ARE |
| 952 | // processing. |
| 953 | // |
| 954 | // Get the data structure graph for the called function. |
| 955 | // |
| 956 | FunctionDSGraph &CalledDS = DS->getClosedDSGraph(Func); |
| 957 | |
| 958 | // Build a mapping between the nodes in our current graph and the nodes in the |
| 959 | // called function's graph. We build it based on our _incomplete_ |
| 960 | // transformation information, because it contains all of the info that we |
| 961 | // should need. |
| 962 | // |
| 963 | map<DSNode*, PointerValSet> NodeMapping; |
| 964 | CalculateNodeMapping(Func, *this, |
| 965 | DS->getClosedDSGraph(Call->getParent()->getParent()), |
| 966 | CalledDS, NodeMapping); |
| 967 | |
| 968 | // Build the inverted version of the node mapping, that maps from a node in |
| 969 | // the called functions graph to a single node in the caller graph. |
| 970 | // |
| 971 | map<DSNode*, DSNode*> InverseNodeMap; |
| 972 | for (map<DSNode*, PointerValSet>::iterator I = NodeMapping.begin(), |
| 973 | E = NodeMapping.end(); I != E; ++I) { |
| 974 | PointerValSet &CalledNodes = I->second; |
| 975 | for (unsigned i = 0, e = CalledNodes.size(); i != e; ++i) |
| 976 | InverseNodeMap[CalledNodes[i].Node] = I->first; |
| 977 | } |
| 978 | NodeMapping.clear(); // Done with information, free memory |
| 979 | |
| 980 | // Build a set of reachable nodes from the arguments/retval that we ARE |
| 981 | // passing in... |
| 982 | set<DSNode*> ReachableNodes; |
| 983 | |
| 984 | // Loop through all of the arguments, marking all of the reachable data |
| 985 | // structure nodes reachable if they are from this pointer... |
| 986 | // |
| 987 | for (unsigned i = 0, e = ArgInfo.size(); i != e; ++i) { |
| 988 | if (ArgInfo[i].ArgNo == -1) { |
| 989 | if (i == 0) // Only process retvals once (performance opt) |
| 990 | markReachableNodes(CalledDS.getRetNodes(), ReachableNodes); |
| 991 | } else { // If it's an argument value... |
| 992 | Argument *Arg = Func->getArgumentList()[ArgInfo[i].ArgNo]; |
| 993 | if (isa<PointerType>(Arg->getType())) |
| 994 | markReachableNodes(CalledDS.getValueMap()[Arg], ReachableNodes); |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | // Now that we know which nodes are already reachable, see if any of the |
| 999 | // arguments that we are not passing values in for can reach one of the |
| 1000 | // existing nodes... |
| 1001 | // |
| 1002 | |
| 1003 | // <FIXME> IN THEORY, we should allow arbitrary paths from the argument to |
| 1004 | // nodes we know about. The problem is that if we do this, then I don't know |
| 1005 | // how to get pool pointers for this head list. Since we are completely |
| 1006 | // deadline driven, I'll just allow direct accesses to the graph. </FIXME> |
| 1007 | // |
| 1008 | |
| 1009 | PtrNo = 0; |
| 1010 | if (isa<PointerType>(Func->getReturnType())) // Make sure we convert retval |
| 1011 | if (PtrNo < ArgInfo.size() && ArgInfo[PtrNo++].ArgNo == -1) { |
| 1012 | // We DO transform the ret val... skip all possible entries for retval |
| 1013 | while (PtrNo < ArgInfo.size() && ArgInfo[PtrNo].ArgNo == -1) |
| 1014 | PtrNo++; |
| 1015 | } else { |
| 1016 | // See what the return value points to... |
| 1017 | |
| 1018 | // FIXME: This should generalize to any number of nodes, just see if any |
| 1019 | // are reachable. |
| 1020 | assert(CalledDS.getRetNodes().size() == 1 && |
| 1021 | "Assumes only one node is returned"); |
| 1022 | DSNode *N = CalledDS.getRetNodes()[0].Node; |
| 1023 | |
| 1024 | // If the return value is not marked as being passed in, but it NEEDS to |
| 1025 | // be transformed, then make it known now. |
| 1026 | // |
| 1027 | if (ReachableNodes.count(N)) { |
| 1028 | #ifdef DEBUG_TRANSFORM_PROGRESS |
| 1029 | cerr << "ensure dependant arguments adds return value entry!\n"; |
| 1030 | #endif |
| 1031 | addCallInfo(DS, Call, -1, InverseNodeMap[N], PoolDescs); |
| 1032 | |
| 1033 | // Keep sorted! |
| 1034 | finalizeConstruction(); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | for (unsigned i = 0, e = Func->getArgumentList().size(); i != e; ++i) { |
| 1039 | Argument *Arg = Func->getArgumentList()[i]; |
| 1040 | if (isa<PointerType>(Arg->getType())) { |
| 1041 | if (PtrNo < ArgInfo.size() && ArgInfo[PtrNo++].ArgNo == (int)i) { |
| 1042 | // We DO transform this arg... skip all possible entries for argument |
| 1043 | while (PtrNo < ArgInfo.size() && ArgInfo[PtrNo].ArgNo == (int)i) |
| 1044 | PtrNo++; |
| 1045 | } else { |
| 1046 | // This should generalize to any number of nodes, just see if any are |
| 1047 | // reachable. |
| 1048 | assert(CalledDS.getValueMap()[Arg].size() == 1 && |
| 1049 | "Only handle case where pointing to one node so far!"); |
| 1050 | |
| 1051 | // If the arg is not marked as being passed in, but it NEEDS to |
| 1052 | // be transformed, then make it known now. |
| 1053 | // |
| 1054 | DSNode *N = CalledDS.getValueMap()[Arg][0].Node; |
| 1055 | if (ReachableNodes.count(N)) { |
| 1056 | #ifdef DEBUG_TRANSFORM_PROGRESS |
| 1057 | cerr << "ensure dependant arguments adds for arg #" << i << "\n"; |
| 1058 | #endif |
| 1059 | addCallInfo(DS, Call, i, InverseNodeMap[N], PoolDescs); |
| 1060 | |
| 1061 | // Keep sorted! |
| 1062 | finalizeConstruction(); |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | } |
Chris Lattner | 396d5d7 | 2002-03-30 04:02:31 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1069 | |
| 1070 | // transformFunctionBody - This transforms the instruction in 'F' to use the |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1071 | // pools specified in PoolDescs when modifying data structure nodes specified in |
| 1072 | // the PoolDescs map. Specifically, scalar values specified in the Scalars |
| 1073 | // vector must be remapped. IPFGraph is the closed data structure graph for F, |
| 1074 | // of which the PoolDescriptor nodes come from. |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1075 | // |
| 1076 | void PoolAllocate::transformFunctionBody(Function *F, FunctionDSGraph &IPFGraph, |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1077 | map<DSNode*, PoolInfo> &PoolDescs) { |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1078 | |
| 1079 | // Loop through the value map looking for scalars that refer to nonescaping |
| 1080 | // allocations. Add them to the Scalars vector. Note that we may have |
| 1081 | // multiple entries in the Scalars vector for each value if it points to more |
| 1082 | // than one object. |
| 1083 | // |
| 1084 | map<Value*, PointerValSet> &ValMap = IPFGraph.getValueMap(); |
| 1085 | vector<ScalarInfo> Scalars; |
| 1086 | |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1087 | #ifdef DEBUG_TRANSFORM_PROGRESS |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 1088 | cerr << "Building scalar map for fn '" << F->getName() << "' body:\n"; |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1089 | #endif |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1091 | for (map<Value*, PointerValSet>::iterator I = ValMap.begin(), |
| 1092 | E = ValMap.end(); I != E; ++I) { |
| 1093 | const PointerValSet &PVS = I->second; // Set of things pointed to by scalar |
| 1094 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1095 | // Check to see if the scalar points to a data structure node... |
| 1096 | for (unsigned i = 0, e = PVS.size(); i != e; ++i) { |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 1097 | if (PVS[i].Index) { cerr << "Problem in " << F->getName() << " for " << I->first << "\n"; } |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1098 | assert(PVS[i].Index == 0 && "Nonzero not handled yet!"); |
| 1099 | |
| 1100 | // If the allocation is in the nonescaping set... |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1101 | map<DSNode*, PoolInfo>::iterator AI = PoolDescs.find(PVS[i].Node); |
| 1102 | if (AI != PoolDescs.end()) { // Add it to the list of scalars |
| 1103 | Scalars.push_back(ScalarInfo(I->first, AI->second)); |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1104 | #ifdef DEBUG_TRANSFORM_PROGRESS |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1105 | cerr << "\nScalar Mapping from:" << I->first |
| 1106 | << "Scalar Mapping to: "; PVS.print(cerr); |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1107 | #endif |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1108 | } |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1112 | #ifdef DEBUG_TRANSFORM_PROGRESS |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 1113 | cerr << "\nIn '" << F->getName() |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 1114 | << "': Found the following values that point to poolable nodes:\n"; |
| 1115 | |
| 1116 | for (unsigned i = 0, e = Scalars.size(); i != e; ++i) |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1117 | cerr << Scalars[i].Val; |
| 1118 | cerr << "\n"; |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1119 | #endif |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1120 | |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1121 | // CallMap - Contain an entry for every call instruction that needs to be |
| 1122 | // transformed. Each entry in the map contains information about what we need |
| 1123 | // to do to each call site to change it to work. |
| 1124 | // |
| 1125 | map<CallInst*, TransformFunctionInfo> CallMap; |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 1126 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1127 | // Now we need to figure out what called functions we need to transform, and |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1128 | // how. To do this, we look at all of the scalars, seeing which functions are |
| 1129 | // either used as a scalar value (so they return a data structure), or are |
| 1130 | // passed one of our scalar values. |
| 1131 | // |
| 1132 | for (unsigned i = 0, e = Scalars.size(); i != e; ++i) { |
| 1133 | Value *ScalarVal = Scalars[i].Val; |
| 1134 | |
| 1135 | // Check to see if the scalar _IS_ a call... |
| 1136 | if (CallInst *CI = dyn_cast<CallInst>(ScalarVal)) |
| 1137 | // If so, add information about the pool it will be returning... |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 1138 | CallMap[CI].addCallInfo(DS, CI, -1, Scalars[i].Pool.Node, PoolDescs); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1139 | |
| 1140 | // Check to see if the scalar is an operand to a call... |
| 1141 | for (Value::use_iterator UI = ScalarVal->use_begin(), |
| 1142 | UE = ScalarVal->use_end(); UI != UE; ++UI) { |
| 1143 | if (CallInst *CI = dyn_cast<CallInst>(*UI)) { |
| 1144 | // Find out which operand this is to the call instruction... |
| 1145 | User::op_iterator OI = find(CI->op_begin(), CI->op_end(), ScalarVal); |
| 1146 | assert(OI != CI->op_end() && "Call on use list but not an operand!?"); |
| 1147 | assert(OI != CI->op_begin() && "Pointer operand is call destination?"); |
| 1148 | |
| 1149 | // FIXME: This is broken if the same pointer is passed to a call more |
| 1150 | // than once! It will get multiple entries for the first pointer. |
| 1151 | |
| 1152 | // Add the operand number and pool handle to the call table... |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 1153 | CallMap[CI].addCallInfo(DS, CI, OI-CI->op_begin()-1, |
| 1154 | Scalars[i].Pool.Node, PoolDescs); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 1159 | // Make sure that all dependant arguments are added as well. For example, if |
| 1160 | // we call foo(null, P) and foo treats it's first and second arguments as |
| 1161 | // belonging to the same data structure, the we MUST set up the CallMap to |
| 1162 | // know that the null needs to be transformed into an index as well. |
| 1163 | // |
| 1164 | for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin(); |
| 1165 | I != CallMap.end(); ++I) |
| 1166 | I->second.ensureDependantArgumentsIncluded(DS, PoolDescs); |
| 1167 | |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1168 | #ifdef DEBUG_TRANSFORM_PROGRESS |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1169 | // Print out call map... |
| 1170 | for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin(); |
| 1171 | I != CallMap.end(); ++I) { |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1172 | cerr << "For call: " << I->first; |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1173 | cerr << I->second.Func->getName() << " must pass pool pointer for args #"; |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1174 | for (unsigned i = 0; i < I->second.ArgInfo.size(); ++i) |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1175 | cerr << I->second.ArgInfo[i].ArgNo << ", "; |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1176 | cerr << "\n\n"; |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1177 | } |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1178 | #endif |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1179 | |
| 1180 | // Loop through all of the call nodes, recursively creating the new functions |
| 1181 | // that we want to call... This uses a map to prevent infinite recursion and |
| 1182 | // to avoid duplicating functions unneccesarily. |
| 1183 | // |
| 1184 | for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin(), |
| 1185 | E = CallMap.end(); I != E; ++I) { |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1186 | // Transform all of the functions we need, or at least ensure there is a |
| 1187 | // cached version available. |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1188 | transformFunction(I->second, IPFGraph, PoolDescs); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 1191 | // Now that all of the functions that we want to call are available, transform |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1192 | // the local function so that it uses the pools locally and passes them to the |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 1193 | // functions that we just hacked up. |
| 1194 | // |
| 1195 | |
| 1196 | // First step, find the instructions to be modified. |
| 1197 | vector<Instruction*> InstToFix; |
| 1198 | for (unsigned i = 0, e = Scalars.size(); i != e; ++i) { |
| 1199 | Value *ScalarVal = Scalars[i].Val; |
| 1200 | |
| 1201 | // Check to see if the scalar _IS_ an instruction. If so, it is involved. |
| 1202 | if (Instruction *Inst = dyn_cast<Instruction>(ScalarVal)) |
| 1203 | InstToFix.push_back(Inst); |
| 1204 | |
| 1205 | // All all of the instructions that use the scalar as an operand... |
| 1206 | for (Value::use_iterator UI = ScalarVal->use_begin(), |
| 1207 | UE = ScalarVal->use_end(); UI != UE; ++UI) |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1208 | InstToFix.push_back(cast<Instruction>(*UI)); |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Chris Lattner | 50e3d32 | 2002-04-13 23:13:18 +0000 | [diff] [blame] | 1211 | // Make sure that we get return instructions that return a null value from the |
| 1212 | // function... |
| 1213 | // |
| 1214 | if (!IPFGraph.getRetNodes().empty()) { |
| 1215 | assert(IPFGraph.getRetNodes().size() == 1 && "Can only return one node?"); |
| 1216 | PointerVal RetNode = IPFGraph.getRetNodes()[0]; |
| 1217 | assert(RetNode.Index == 0 && "Subindexing not implemented yet!"); |
| 1218 | |
| 1219 | // Only process return instructions if the return value of this function is |
| 1220 | // part of one of the data structures we are transforming... |
| 1221 | // |
| 1222 | if (PoolDescs.count(RetNode.Node)) { |
| 1223 | // Loop over all of the basic blocks, adding return instructions... |
| 1224 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 1225 | if (ReturnInst *RI = dyn_cast<ReturnInst>((*I)->getTerminator())) |
| 1226 | InstToFix.push_back(RI); |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | |
| 1231 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 1232 | // Eliminate duplicates by sorting, then removing equal neighbors. |
| 1233 | sort(InstToFix.begin(), InstToFix.end()); |
| 1234 | InstToFix.erase(unique(InstToFix.begin(), InstToFix.end()), InstToFix.end()); |
| 1235 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1236 | // Loop over all of the instructions to transform, creating the new |
| 1237 | // replacement instructions for them. This also unlinks them from the |
| 1238 | // function so they can be safely deleted later. |
| 1239 | // |
| 1240 | map<Value*, Value*> XFormMap; |
| 1241 | NewInstructionCreator NIC(*this, Scalars, CallMap, XFormMap); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1242 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1243 | // Visit all instructions... creating the new instructions that we need and |
| 1244 | // unlinking the old instructions from the function... |
| 1245 | // |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1246 | #ifdef DEBUG_TRANSFORM_PROGRESS |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1247 | for (unsigned i = 0, e = InstToFix.size(); i != e; ++i) { |
| 1248 | cerr << "Fixing: " << InstToFix[i]; |
| 1249 | NIC.visit(InstToFix[i]); |
| 1250 | } |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1251 | #else |
| 1252 | NIC.visit(InstToFix.begin(), InstToFix.end()); |
| 1253 | #endif |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1254 | |
| 1255 | // Make all instructions we will delete "let go" of their operands... so that |
| 1256 | // we can safely delete Arguments whose types have changed... |
| 1257 | // |
| 1258 | for_each(InstToFix.begin(), InstToFix.end(), |
| 1259 | mem_fun(&Instruction::dropAllReferences)); |
| 1260 | |
| 1261 | // Loop through all of the pointer arguments coming into the function, |
| 1262 | // replacing them with arguments of POINTERTYPE to match the function type of |
| 1263 | // the function. |
| 1264 | // |
| 1265 | FunctionType::ParamTypes::const_iterator TI = |
| 1266 | F->getFunctionType()->getParamTypes().begin(); |
| 1267 | for (Function::ArgumentListType::iterator I = F->getArgumentList().begin(), |
| 1268 | E = F->getArgumentList().end(); I != E; ++I, ++TI) { |
| 1269 | Argument *Arg = *I; |
| 1270 | if (Arg->getType() != *TI) { |
| 1271 | assert(isa<PointerType>(Arg->getType()) && *TI == POINTERTYPE); |
| 1272 | Argument *NewArg = new Argument(*TI, Arg->getName()); |
| 1273 | XFormMap[Arg] = NewArg; // Map old arg into new arg... |
| 1274 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1275 | // Replace the old argument and then delete it... |
| 1276 | delete F->getArgumentList().replaceWith(I, NewArg); |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | // Now that all of the new instructions have been created, we can update all |
| 1281 | // of the references to dummy values to be references to the actual values |
| 1282 | // that are computed. |
| 1283 | // |
| 1284 | NIC.updateReferences(); |
| 1285 | |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1286 | #ifdef DEBUG_TRANSFORM_PROGRESS |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1287 | cerr << "TRANSFORMED FUNCTION:\n" << F; |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1288 | #endif |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1289 | |
| 1290 | // Delete all of the "instructions to fix" |
| 1291 | for_each(InstToFix.begin(), InstToFix.end(), deleter<Instruction>); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1292 | |
Chris Lattner | 457e1ac | 2002-04-15 22:42:23 +0000 | [diff] [blame] | 1293 | // Eliminate pool base loads that we can easily prove are redundant |
| 1294 | if (!DisableRLE) |
| 1295 | PoolBaseLoadEliminator(PoolDescs).visit(F); |
| 1296 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 1297 | // Since we have liberally hacked the function to pieces, we want to inform |
| 1298 | // the datastructure pass that its internal representation is out of date. |
| 1299 | // |
| 1300 | DS->invalidateFunction(F); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1303 | |
| 1304 | |
| 1305 | // transformFunction - Transform the specified function the specified way. It |
| 1306 | // we have already transformed that function that way, don't do anything. The |
| 1307 | // nodes in the TransformFunctionInfo come out of callers data structure graph. |
| 1308 | // |
| 1309 | void PoolAllocate::transformFunction(TransformFunctionInfo &TFI, |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1310 | FunctionDSGraph &CallerIPGraph, |
| 1311 | map<DSNode*, PoolInfo> &CallerPoolDesc) { |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1312 | if (getTransformedFunction(TFI)) return; // Function xformation already done? |
| 1313 | |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1314 | #ifdef DEBUG_TRANSFORM_PROGRESS |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1315 | cerr << "********** Entering transformFunction for " |
Chris Lattner | 0dc225c | 2002-03-31 07:17:46 +0000 | [diff] [blame] | 1316 | << TFI.Func->getName() << ":\n"; |
| 1317 | for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) |
| 1318 | cerr << " ArgInfo[" << i << "] = " << TFI.ArgInfo[i].ArgNo << "\n"; |
| 1319 | cerr << "\n"; |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1320 | #endif |
Chris Lattner | 0dc225c | 2002-03-31 07:17:46 +0000 | [diff] [blame] | 1321 | |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1322 | const FunctionType *OldFuncType = TFI.Func->getFunctionType(); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1323 | |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1324 | assert(!OldFuncType->isVarArg() && "Vararg functions not handled yet!"); |
Chris Lattner | 692ad5d | 2002-03-29 17:13:46 +0000 | [diff] [blame] | 1325 | |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1326 | // Build the type for the new function that we are transforming |
| 1327 | vector<const Type*> ArgTys; |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1328 | ArgTys.reserve(OldFuncType->getNumParams()+TFI.ArgInfo.size()); |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1329 | for (unsigned i = 0, e = OldFuncType->getNumParams(); i != e; ++i) |
| 1330 | ArgTys.push_back(OldFuncType->getParamType(i)); |
| 1331 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1332 | const Type *RetType = OldFuncType->getReturnType(); |
| 1333 | |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1334 | // Add one pool pointer for every argument that needs to be supplemented. |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1335 | for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) { |
| 1336 | if (TFI.ArgInfo[i].ArgNo == -1) |
| 1337 | RetType = POINTERTYPE; // Return a pointer |
| 1338 | else |
| 1339 | ArgTys[TFI.ArgInfo[i].ArgNo] = POINTERTYPE; // Pass a pointer |
| 1340 | ArgTys.push_back(PointerType::get(CallerPoolDesc.find(TFI.ArgInfo[i].Node) |
| 1341 | ->second.PoolType)); |
| 1342 | } |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1343 | |
| 1344 | // Build the new function type... |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1345 | const FunctionType *NewFuncType = FunctionType::get(RetType, ArgTys, |
| 1346 | OldFuncType->isVarArg()); |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1347 | |
| 1348 | // The new function is internal, because we know that only we can call it. |
| 1349 | // This also helps subsequent IP transformations to eliminate duplicated pool |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1350 | // pointers (which look like the same value is always passed into a parameter, |
| 1351 | // allowing it to be easily eliminated). |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1352 | // |
| 1353 | Function *NewFunc = new Function(NewFuncType, true, |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1354 | TFI.Func->getName()+".poolxform"); |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1355 | CurModule->getFunctionList().push_back(NewFunc); |
| 1356 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1357 | |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1358 | #ifdef DEBUG_TRANSFORM_PROGRESS |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1359 | cerr << "Created function prototype: " << NewFunc << "\n"; |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1360 | #endif |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1361 | |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1362 | // Add the newly formed function to the TransformedFunctions table so that |
| 1363 | // infinite recursion does not occur! |
| 1364 | // |
| 1365 | TransformedFunctions[TFI] = NewFunc; |
| 1366 | |
| 1367 | // Add arguments to the function... starting with all of the old arguments |
| 1368 | vector<Value*> ArgMap; |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1369 | for (unsigned i = 0, e = TFI.Func->getArgumentList().size(); i != e; ++i) { |
Chris Lattner | 2e9fa6d | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 1370 | const Argument *OFA = TFI.Func->getArgumentList()[i]; |
| 1371 | Argument *NFA = new Argument(OFA->getType(), OFA->getName()); |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1372 | NewFunc->getArgumentList().push_back(NFA); |
| 1373 | ArgMap.push_back(NFA); // Keep track of the arguments |
| 1374 | } |
| 1375 | |
| 1376 | // Now add all of the arguments corresponding to pools passed in... |
| 1377 | for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) { |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1378 | CallArgInfo &AI = TFI.ArgInfo[i]; |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1379 | string Name; |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1380 | if (AI.ArgNo == -1) |
| 1381 | Name = "ret"; |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1382 | else |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1383 | Name = ArgMap[AI.ArgNo]->getName(); // Get the arg name |
| 1384 | const Type *Ty = PointerType::get(CallerPoolDesc[AI.Node].PoolType); |
| 1385 | Argument *NFA = new Argument(Ty, Name+".pool"); |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1386 | NewFunc->getArgumentList().push_back(NFA); |
| 1387 | } |
| 1388 | |
| 1389 | // Now clone the body of the old function into the new function... |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1390 | CloneFunctionInto(NewFunc, TFI.Func, ArgMap); |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1391 | |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 1392 | // 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] | 1393 | // it has extra arguments for the pools coming in. Now we have to get the |
| 1394 | // data structure graph for the function we are replacing, and figure out how |
| 1395 | // our graph nodes map to the graph nodes in the dest function. |
| 1396 | // |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 1397 | FunctionDSGraph &DSGraph = DS->getClosedDSGraph(NewFunc); |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 1398 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1399 | // NodeMapping - Multimap from callers graph to called graph. We are |
| 1400 | // guaranteed that the called function graph has more nodes than the caller, |
| 1401 | // or exactly the same number of nodes. This is because the called function |
| 1402 | // might not know that two nodes are merged when considering the callers |
| 1403 | // context, but the caller obviously does. Because of this, a single node in |
| 1404 | // the calling function's data structure graph can map to multiple nodes in |
| 1405 | // the called functions graph. |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1406 | // |
| 1407 | map<DSNode*, PointerValSet> NodeMapping; |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 1408 | |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 1409 | CalculateNodeMapping(NewFunc, TFI, CallerIPGraph, DSGraph, |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1410 | NodeMapping); |
| 1411 | |
| 1412 | // Print out the node mapping... |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1413 | #ifdef DEBUG_TRANSFORM_PROGRESS |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 1414 | cerr << "\nNode mapping for call of " << NewFunc->getName() << "\n"; |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1415 | for (map<DSNode*, PointerValSet>::iterator I = NodeMapping.begin(); |
| 1416 | I != NodeMapping.end(); ++I) { |
| 1417 | cerr << "Map: "; I->first->print(cerr); |
| 1418 | cerr << "To: "; I->second.print(cerr); |
| 1419 | cerr << "\n"; |
| 1420 | } |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1421 | #endif |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1422 | |
| 1423 | // Fill in the PoolDescriptor information for the transformed function so that |
| 1424 | // it can determine which value holds the pool descriptor for each data |
| 1425 | // structure node that it accesses. |
| 1426 | // |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1427 | map<DSNode*, PoolInfo> PoolDescs; |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1428 | |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1429 | #ifdef DEBUG_TRANSFORM_PROGRESS |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 1430 | cerr << "\nCalculating the pool descriptor map:\n"; |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1431 | #endif |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1432 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1433 | // Calculate as much of the pool descriptor map as possible. Since we have |
| 1434 | // the node mapping between the caller and callee functions, and we have the |
| 1435 | // pool descriptor information of the caller, we can calculate a partical pool |
| 1436 | // descriptor map for the called function. |
| 1437 | // |
| 1438 | // The nodes that we do not have complete information for are the ones that |
| 1439 | // are accessed by loading pointers derived from arguments passed in, but that |
| 1440 | // are not passed in directly. In this case, we have all of the information |
| 1441 | // except a pool value. If the called function refers to this pool, the pool |
| 1442 | // value will be loaded from the pool graph and added to the map as neccesary. |
| 1443 | // |
| 1444 | for (map<DSNode*, PointerValSet>::iterator I = NodeMapping.begin(); |
| 1445 | I != NodeMapping.end(); ++I) { |
| 1446 | DSNode *CallerNode = I->first; |
| 1447 | PoolInfo &CallerPI = CallerPoolDesc[CallerNode]; |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1448 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1449 | // Check to see if we have a node pointer passed in for this value... |
| 1450 | Value *CalleeValue = 0; |
| 1451 | for (unsigned a = 0, ae = TFI.ArgInfo.size(); a != ae; ++a) |
| 1452 | if (TFI.ArgInfo[a].Node == CallerNode) { |
| 1453 | // Calculate the argument number that the pool is to the function |
| 1454 | // call... The call instruction should not have the pool operands added |
| 1455 | // yet. |
| 1456 | unsigned ArgNo = TFI.Call->getNumOperands()-1+a; |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1457 | #ifdef DEBUG_TRANSFORM_PROGRESS |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1458 | cerr << "Should be argument #: " << ArgNo << "[i = " << a << "]\n"; |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1459 | #endif |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1460 | assert(ArgNo < NewFunc->getArgumentList().size() && |
| 1461 | "Call already has pool arguments added??"); |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1462 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1463 | // Map the pool argument into the called function... |
| 1464 | CalleeValue = NewFunc->getArgumentList()[ArgNo]; |
| 1465 | break; // Found value, quit loop |
| 1466 | } |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1467 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1468 | // Loop over all of the data structure nodes that this incoming node maps to |
| 1469 | // Creating a PoolInfo structure for them. |
| 1470 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) { |
| 1471 | assert(I->second[i].Index == 0 && "Doesn't handle subindexing yet!"); |
| 1472 | DSNode *CalleeNode = I->second[i].Node; |
| 1473 | |
| 1474 | // Add the descriptor. We already know everything about it by now, much |
| 1475 | // of it is the same as the caller info. |
| 1476 | // |
| 1477 | PoolDescs.insert(make_pair(CalleeNode, |
| 1478 | PoolInfo(CalleeNode, CalleeValue, |
| 1479 | CallerPI.NewType, |
| 1480 | CallerPI.PoolType))); |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1481 | } |
Chris Lattner | 847b6e2 | 2002-03-30 20:53:14 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | // We must destroy the node mapping so that we don't have latent references |
| 1485 | // into the data structure graph for the new function. Otherwise we get |
| 1486 | // assertion failures when transformFunctionBody tries to invalidate the |
| 1487 | // graph. |
| 1488 | // |
| 1489 | NodeMapping.clear(); |
Chris Lattner | ca9f4d3 | 2002-03-30 09:12:35 +0000 | [diff] [blame] | 1490 | |
| 1491 | // Now that we know everything we need about the function, transform the body |
| 1492 | // now! |
| 1493 | // |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1494 | transformFunctionBody(NewFunc, DSGraph, PoolDescs); |
| 1495 | |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1496 | #ifdef DEBUG_TRANSFORM_PROGRESS |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1497 | cerr << "Function after transformation:\n" << NewFunc; |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1498 | #endif |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Chris Lattner | 8f796d6 | 2002-04-13 19:25:57 +0000 | [diff] [blame] | 1501 | static unsigned countPointerTypes(const Type *Ty) { |
| 1502 | if (isa<PointerType>(Ty)) { |
| 1503 | return 1; |
| 1504 | } else if (StructType *STy = dyn_cast<StructType>(Ty)) { |
| 1505 | unsigned Num = 0; |
| 1506 | for (unsigned i = 0, e = STy->getElementTypes().size(); i != e; ++i) |
| 1507 | Num += countPointerTypes(STy->getElementTypes()[i]); |
| 1508 | return Num; |
| 1509 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 1510 | return countPointerTypes(ATy->getElementType()); |
| 1511 | } else { |
| 1512 | assert(Ty->isPrimitiveType() && "Unknown derived type!"); |
| 1513 | return 0; |
| 1514 | } |
| 1515 | } |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 1516 | |
| 1517 | // CreatePools - Insert instructions into the function we are processing to |
| 1518 | // create all of the memory pool objects themselves. This also inserts |
| 1519 | // destruction code. Add an alloca for each pool that is allocated to the |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1520 | // PoolDescs vector. |
Chris Lattner | 66df97d | 2002-03-29 06:21:38 +0000 | [diff] [blame] | 1521 | // |
| 1522 | void PoolAllocate::CreatePools(Function *F, const vector<AllocDSNode*> &Allocs, |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1523 | map<DSNode*, PoolInfo> &PoolDescs) { |
| 1524 | // Find all of the return nodes in the function... |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1525 | vector<BasicBlock*> ReturnNodes; |
| 1526 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 1527 | if (isa<ReturnInst>((*I)->getTerminator())) |
| 1528 | ReturnNodes.push_back(*I); |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1529 | |
Chris Lattner | 3e78dea | 2002-04-18 14:43:30 +0000 | [diff] [blame] | 1530 | #ifdef DEBUG_CREATE_POOLS |
| 1531 | cerr << "Allocs that we are pool allocating:\n"; |
| 1532 | for (unsigned i = 0, e = Allocs.size(); i != e; ++i) |
| 1533 | Allocs[i]->dump(); |
| 1534 | #endif |
| 1535 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1536 | map<DSNode*, PATypeHolder> AbsPoolTyMap; |
| 1537 | |
| 1538 | // First pass over the allocations to process... |
| 1539 | for (unsigned i = 0, e = Allocs.size(); i != e; ++i) { |
| 1540 | // Create the pooldescriptor mapping... with null entries for everything |
| 1541 | // except the node & NewType fields. |
| 1542 | // |
| 1543 | map<DSNode*, PoolInfo>::iterator PI = |
| 1544 | PoolDescs.insert(make_pair(Allocs[i], PoolInfo(Allocs[i]))).first; |
| 1545 | |
Chris Lattner | 8f796d6 | 2002-04-13 19:25:57 +0000 | [diff] [blame] | 1546 | // Add a symbol table entry for the new type if there was one for the old |
| 1547 | // type... |
| 1548 | string OldName = CurModule->getTypeName(Allocs[i]->getType()); |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 1549 | if (OldName.empty()) OldName = "node"; |
| 1550 | CurModule->addTypeName(OldName+".p", PI->second.NewType); |
Chris Lattner | 8f796d6 | 2002-04-13 19:25:57 +0000 | [diff] [blame] | 1551 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1552 | // Create the abstract pool types that will need to be resolved in a second |
| 1553 | // pass once an abstract type is created for each pool. |
| 1554 | // |
| 1555 | // Can only handle limited shapes for now... |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 1556 | const Type *OldNodeTy = Allocs[i]->getType(); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1557 | vector<const Type*> PoolTypes; |
| 1558 | |
| 1559 | // Pool type is the first element of the pool descriptor type... |
| 1560 | PoolTypes.push_back(getPoolType(PoolDescs[Allocs[i]].NewType)); |
Chris Lattner | 8f796d6 | 2002-04-13 19:25:57 +0000 | [diff] [blame] | 1561 | |
| 1562 | unsigned NumPointers = countPointerTypes(OldNodeTy); |
| 1563 | while (NumPointers--) // Add a different opaque type for each pointer |
| 1564 | PoolTypes.push_back(OpaqueType::get()); |
| 1565 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1566 | assert(Allocs[i]->getNumLinks() == PoolTypes.size()-1 && |
| 1567 | "Node should have same number of pointers as pool!"); |
| 1568 | |
Chris Lattner | 8f796d6 | 2002-04-13 19:25:57 +0000 | [diff] [blame] | 1569 | StructType *PoolType = StructType::get(PoolTypes); |
| 1570 | |
| 1571 | // Add a symbol table entry for the pooltype if possible... |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 1572 | CurModule->addTypeName(OldName+".pool", PoolType); |
Chris Lattner | 8f796d6 | 2002-04-13 19:25:57 +0000 | [diff] [blame] | 1573 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1574 | // Create the pool type, with opaque values for pointers... |
Chris Lattner | 8f796d6 | 2002-04-13 19:25:57 +0000 | [diff] [blame] | 1575 | AbsPoolTyMap.insert(make_pair(Allocs[i], PoolType)); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1576 | #ifdef DEBUG_CREATE_POOLS |
| 1577 | cerr << "POOL TY: " << AbsPoolTyMap.find(Allocs[i])->second.get() << "\n"; |
| 1578 | #endif |
| 1579 | } |
| 1580 | |
| 1581 | // Now that we have types for all of the pool types, link them all together. |
| 1582 | for (unsigned i = 0, e = Allocs.size(); i != e; ++i) { |
| 1583 | PATypeHolder &PoolTyH = AbsPoolTyMap.find(Allocs[i])->second; |
| 1584 | |
| 1585 | // Resolve all of the outgoing pointer types of this pool node... |
| 1586 | for (unsigned p = 0, pe = Allocs[i]->getNumLinks(); p != pe; ++p) { |
| 1587 | PointerValSet &PVS = Allocs[i]->getLink(p); |
| 1588 | assert(!PVS.empty() && "Outgoing edge is empty, field unused, can" |
| 1589 | " probably just leave the type opaque or something dumb."); |
| 1590 | unsigned Out; |
| 1591 | for (Out = 0; AbsPoolTyMap.count(PVS[Out].Node) == 0; ++Out) |
| 1592 | assert(Out != PVS.size() && "No edge to an outgoing allocation node!?"); |
| 1593 | |
| 1594 | assert(PVS[Out].Index == 0 && "Subindexing not implemented yet!"); |
| 1595 | |
| 1596 | // The actual struct type could change each time through the loop, so it's |
| 1597 | // NOT loop invariant. |
| 1598 | StructType *PoolTy = cast<StructType>(PoolTyH.get()); |
| 1599 | |
| 1600 | // Get the opaque type... |
| 1601 | DerivedType *ElTy = |
| 1602 | cast<DerivedType>(PoolTy->getElementTypes()[p+1].get()); |
| 1603 | |
| 1604 | #ifdef DEBUG_CREATE_POOLS |
| 1605 | cerr << "Refining " << ElTy << " of " << PoolTy << " to " |
| 1606 | << AbsPoolTyMap.find(PVS[Out].Node)->second.get() << "\n"; |
| 1607 | #endif |
| 1608 | |
| 1609 | const Type *RefPoolTy = AbsPoolTyMap.find(PVS[Out].Node)->second.get(); |
| 1610 | ElTy->refineAbstractTypeTo(PointerType::get(RefPoolTy)); |
| 1611 | |
| 1612 | #ifdef DEBUG_CREATE_POOLS |
| 1613 | cerr << "Result pool type is: " << PoolTyH.get() << "\n"; |
| 1614 | #endif |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | // Create the code that goes in the entry and exit nodes for the function... |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1619 | vector<Instruction*> EntryNodeInsts; |
| 1620 | for (unsigned i = 0, e = Allocs.size(); i != e; ++i) { |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1621 | PoolInfo &PI = PoolDescs[Allocs[i]]; |
| 1622 | |
| 1623 | // Fill in the pool type for this pool... |
| 1624 | PI.PoolType = AbsPoolTyMap.find(Allocs[i])->second.get(); |
| 1625 | assert(!PI.PoolType->isAbstract() && |
| 1626 | "Pool type should not be abstract anymore!"); |
| 1627 | |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1628 | // Add an allocation and a free for each pool... |
Chris Lattner | 5da145b | 2002-04-13 19:52:54 +0000 | [diff] [blame] | 1629 | AllocaInst *PoolAlloc |
| 1630 | = new AllocaInst(PointerType::get(PI.PoolType), 0, |
| 1631 | CurModule->getTypeName(PI.PoolType)); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1632 | PI.Handle = PoolAlloc; |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1633 | EntryNodeInsts.push_back(PoolAlloc); |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1634 | AllocationInst *AI = Allocs[i]->getAllocation(); |
| 1635 | |
| 1636 | // Initialize the pool. We need to know how big each allocation is. For |
| 1637 | // our purposes here, we assume we are allocating a scalar, or array of |
| 1638 | // constant size. |
| 1639 | // |
Chris Lattner | acf1902 | 2002-04-14 06:14:41 +0000 | [diff] [blame] | 1640 | unsigned ElSize = TargetData.getTypeSize(PI.NewType); |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1641 | |
| 1642 | vector<Value*> Args; |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1643 | Args.push_back(ConstantUInt::get(Type::UIntTy, ElSize)); |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1644 | Args.push_back(PoolAlloc); // Pool to initialize |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1645 | EntryNodeInsts.push_back(new CallInst(PoolInit, Args)); |
| 1646 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1647 | // Add code to destroy the pool in all of the exit nodes of the function... |
Chris Lattner | 8f796d6 | 2002-04-13 19:25:57 +0000 | [diff] [blame] | 1648 | Args.clear(); |
| 1649 | Args.push_back(PoolAlloc); // Pool to initialize |
| 1650 | |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1651 | for (unsigned EN = 0, ENE = ReturnNodes.size(); EN != ENE; ++EN) { |
| 1652 | Instruction *Destroy = new CallInst(PoolDestroy, Args); |
| 1653 | |
| 1654 | // Insert it before the return instruction... |
| 1655 | BasicBlock *RetNode = ReturnNodes[EN]; |
| 1656 | RetNode->getInstList().insert(RetNode->end()-1, Destroy); |
| 1657 | } |
| 1658 | } |
| 1659 | |
Chris Lattner | 5da145b | 2002-04-13 19:52:54 +0000 | [diff] [blame] | 1660 | // Now that all of the pool descriptors have been created, link them together |
| 1661 | // so that called functions can get links as neccesary... |
| 1662 | // |
| 1663 | for (unsigned i = 0, e = Allocs.size(); i != e; ++i) { |
| 1664 | PoolInfo &PI = PoolDescs[Allocs[i]]; |
| 1665 | |
| 1666 | // For every pointer in the data structure, initialize a link that |
| 1667 | // indicates which pool to access... |
| 1668 | // |
| 1669 | vector<Value*> Indices(2); |
| 1670 | Indices[0] = ConstantUInt::get(Type::UIntTy, 0); |
| 1671 | for (unsigned l = 0, le = PI.Node->getNumLinks(); l != le; ++l) |
| 1672 | // Only store an entry for the field if the field is used! |
| 1673 | if (!PI.Node->getLink(l).empty()) { |
| 1674 | assert(PI.Node->getLink(l).size() == 1 && "Should have only one link!"); |
| 1675 | PointerVal PV = PI.Node->getLink(l)[0]; |
| 1676 | assert(PV.Index == 0 && "Subindexing not supported yet!"); |
| 1677 | PoolInfo &LinkedPool = PoolDescs[PV.Node]; |
| 1678 | Indices[1] = ConstantUInt::get(Type::UByteTy, 1+l); |
| 1679 | |
| 1680 | EntryNodeInsts.push_back(new StoreInst(LinkedPool.Handle, PI.Handle, |
| 1681 | Indices)); |
| 1682 | } |
| 1683 | } |
| 1684 | |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1685 | // Insert the entry node code into the entry block... |
| 1686 | F->getEntryNode()->getInstList().insert(F->getEntryNode()->begin()+1, |
| 1687 | EntryNodeInsts.begin(), |
| 1688 | EntryNodeInsts.end()); |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1692 | // addPoolPrototypes - Add prototypes for the pool functions to the specified |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 1693 | // module and update the Pool* instance variables to point to them. |
| 1694 | // |
| 1695 | void PoolAllocate::addPoolPrototypes(Module *M) { |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1696 | // Get poolinit function... |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1697 | vector<const Type*> Args; |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1698 | Args.push_back(Type::UIntTy); // Num bytes per element |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1699 | FunctionType *PoolInitTy = FunctionType::get(Type::VoidTy, Args, true); |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1700 | PoolInit = M->getOrInsertFunction("poolinit", PoolInitTy); |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 1701 | |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1702 | // Get pooldestroy function... |
| 1703 | Args.pop_back(); // Only takes a pool... |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1704 | FunctionType *PoolDestroyTy = FunctionType::get(Type::VoidTy, Args, true); |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1705 | PoolDestroy = M->getOrInsertFunction("pooldestroy", PoolDestroyTy); |
| 1706 | |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1707 | // Get the poolalloc function... |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1708 | FunctionType *PoolAllocTy = FunctionType::get(POINTERTYPE, Args, true); |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1709 | PoolAlloc = M->getOrInsertFunction("poolalloc", PoolAllocTy); |
| 1710 | |
| 1711 | // Get the poolfree function... |
Chris Lattner | 441e16f | 2002-04-12 20:23:15 +0000 | [diff] [blame] | 1712 | Args.push_back(POINTERTYPE); // Pointer to free |
| 1713 | FunctionType *PoolFreeTy = FunctionType::get(Type::VoidTy, Args, true); |
Chris Lattner | e0618ca | 2002-03-29 05:50:20 +0000 | [diff] [blame] | 1714 | PoolFree = M->getOrInsertFunction("poolfree", PoolFreeTy); |
| 1715 | |
Chris Lattner | 0e0c15b | 2002-04-27 02:29:32 +0000 | [diff] [blame] | 1716 | Args[0] = Type::UIntTy; // Number of slots to allocate |
| 1717 | FunctionType *PoolAllocArrayTy = FunctionType::get(POINTERTYPE, Args, true); |
| 1718 | PoolAllocArray = M->getOrInsertFunction("poolallocarray", PoolAllocArrayTy); |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | |
| 1722 | bool PoolAllocate::run(Module *M) { |
| 1723 | addPoolPrototypes(M); |
| 1724 | CurModule = M; |
| 1725 | |
| 1726 | DS = &getAnalysis<DataStructure>(); |
| 1727 | bool Changed = false; |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1728 | |
| 1729 | // We cannot use an iterator here because it will get invalidated when we add |
| 1730 | // functions to the module later... |
| 1731 | for (unsigned i = 0; i != M->size(); ++i) |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 1732 | if (!M->getFunctionList()[i]->isExternal()) { |
Chris Lattner | 291a1b1 | 2002-03-29 19:05:48 +0000 | [diff] [blame] | 1733 | Changed |= processFunction(M->getFunctionList()[i]); |
Chris Lattner | f32d65d | 2002-03-29 21:25:19 +0000 | [diff] [blame] | 1734 | if (Changed) { |
| 1735 | cerr << "Only processing one function\n"; |
| 1736 | break; |
| 1737 | } |
| 1738 | } |
Chris Lattner | 175f37c | 2002-03-29 03:40:59 +0000 | [diff] [blame] | 1739 | |
| 1740 | CurModule = 0; |
| 1741 | DS = 0; |
| 1742 | return false; |
| 1743 | } |
| 1744 | |
| 1745 | |
| 1746 | // createPoolAllocatePass - Global function to access the functionality of this |
| 1747 | // pass... |
| 1748 | // |
Chris Lattner | 64fd935 | 2002-03-28 18:08:31 +0000 | [diff] [blame] | 1749 | Pass *createPoolAllocatePass() { return new PoolAllocate(); } |