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