Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1 | //===- ScalarReplAggregates.cpp - Scalar Replacement of Aggregates --------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 9 | // |
| 10 | // This transformation implements the well known scalar replacement of |
| 11 | // aggregates transformation. This xform breaks up alloca instructions of |
| 12 | // aggregate type (structure or array) into individual alloca instructions for |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 13 | // each member (if possible). Then, if possible, it transforms the individual |
| 14 | // alloca instructions into nice clean scalar SSA form. |
| 15 | // |
| 16 | // This combines a simple SRoA algorithm with the Mem2Reg algorithm because |
| 17 | // often interact, especially for C++ programs. As such, iterating between |
| 18 | // SRoA, then Mem2Reg until we run out of things to promote works well. |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 22 | #define DEBUG_TYPE "scalarrepl" |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
| 25 | #include "llvm/DerivedTypes.h" |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 26 | #include "llvm/Function.h" |
Misha Brukman | 2b3387a | 2004-07-29 17:05:13 +0000 | [diff] [blame] | 27 | #include "llvm/Instructions.h" |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 28 | #include "llvm/IntrinsicInst.h" |
| 29 | #include "llvm/Pass.h" |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/Dominators.h" |
| 31 | #include "llvm/Target/TargetData.h" |
| 32 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 34 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 35 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Compiler.h" |
Chris Lattner | a731513 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/Statistic.h" |
| 39 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 40d2aeb | 2003-12-02 17:43:55 +0000 | [diff] [blame] | 40 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 42 | STATISTIC(NumReplaced, "Number of allocas broken up"); |
| 43 | STATISTIC(NumPromoted, "Number of allocas promoted"); |
| 44 | STATISTIC(NumConverted, "Number of aggregates converted to scalar"); |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 46 | namespace { |
Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 47 | struct VISIBILITY_HIDDEN SROA : public FunctionPass { |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 48 | bool runOnFunction(Function &F); |
| 49 | |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 50 | bool performScalarRepl(Function &F); |
| 51 | bool performPromotion(Function &F); |
| 52 | |
Chris Lattner | c817458 | 2003-08-31 00:45:13 +0000 | [diff] [blame] | 53 | // getAnalysisUsage - This pass does not require any passes, but we know it |
| 54 | // will not alter the CFG, so say so. |
| 55 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | a906bac | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 56 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 57 | AU.addRequired<DominanceFrontier>(); |
| 58 | AU.addRequired<TargetData>(); |
Chris Lattner | c817458 | 2003-08-31 00:45:13 +0000 | [diff] [blame] | 59 | AU.setPreservesCFG(); |
| 60 | } |
| 61 | |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 62 | private: |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 63 | int isSafeElementUse(Value *Ptr, bool isFirstElt, AllocationInst *AI); |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 64 | int isSafeUseOfAllocation(Instruction *User, AllocationInst *AI); |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 65 | bool isSafeMemIntrinsicOnAllocation(MemIntrinsic *MI, AllocationInst *AI); |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 66 | bool isSafeUseOfBitCastedAllocation(BitCastInst *User, AllocationInst *AI); |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 67 | int isSafeAllocaToScalarRepl(AllocationInst *AI); |
| 68 | void CanonicalizeAllocaUsers(AllocationInst *AI); |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 69 | AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocationInst *Base); |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 71 | void RewriteBitCastUserOfAlloca(Instruction *BCInst, AllocationInst *AI, |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 72 | SmallVector<AllocaInst*, 32> &NewElts); |
| 73 | |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 74 | const Type *CanConvertToScalar(Value *V, bool &IsNotTrivial); |
| 75 | void ConvertToScalar(AllocationInst *AI, const Type *Ty); |
| 76 | void ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, unsigned Offset); |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 79 | RegisterPass<SROA> X("scalarrepl", "Scalar Replacement of Aggregates"); |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 82 | // Public interface to the ScalarReplAggregates pass |
Chris Lattner | 3e86084 | 2004-09-20 04:43:15 +0000 | [diff] [blame] | 83 | FunctionPass *llvm::createScalarReplAggregatesPass() { return new SROA(); } |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 84 | |
| 85 | |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 86 | bool SROA::runOnFunction(Function &F) { |
Chris Lattner | 9a95f2a | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 87 | bool Changed = performPromotion(F); |
| 88 | while (1) { |
| 89 | bool LocalChange = performScalarRepl(F); |
| 90 | if (!LocalChange) break; // No need to repromote if no scalarrepl |
| 91 | Changed = true; |
| 92 | LocalChange = performPromotion(F); |
| 93 | if (!LocalChange) break; // No need to re-scalarrepl if no promotion |
| 94 | } |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 95 | |
| 96 | return Changed; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | bool SROA::performPromotion(Function &F) { |
| 101 | std::vector<AllocaInst*> Allocas; |
| 102 | const TargetData &TD = getAnalysis<TargetData>(); |
Chris Lattner | a906bac | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 103 | DominatorTree &DT = getAnalysis<DominatorTree>(); |
| 104 | DominanceFrontier &DF = getAnalysis<DominanceFrontier>(); |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 5dac64f | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 106 | BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 9a95f2a | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 108 | bool Changed = false; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 110 | while (1) { |
| 111 | Allocas.clear(); |
| 112 | |
| 113 | // Find allocas that are safe to promote, by looking at all instructions in |
| 114 | // the entry node |
| 115 | for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) |
| 116 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? |
| 117 | if (isAllocaPromotable(AI, TD)) |
| 118 | Allocas.push_back(AI); |
| 119 | |
| 120 | if (Allocas.empty()) break; |
| 121 | |
Chris Lattner | a906bac | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 122 | PromoteMemToReg(Allocas, DT, DF, TD); |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 123 | NumPromoted += Allocas.size(); |
| 124 | Changed = true; |
| 125 | } |
| 126 | |
| 127 | return Changed; |
| 128 | } |
| 129 | |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 130 | // performScalarRepl - This algorithm is a simple worklist driven algorithm, |
| 131 | // which runs on all of the malloc/alloca instructions in the function, removing |
| 132 | // them if they are only used by getelementptr instructions. |
| 133 | // |
| 134 | bool SROA::performScalarRepl(Function &F) { |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 135 | std::vector<AllocationInst*> WorkList; |
| 136 | |
| 137 | // Scan the entry basic block, adding any alloca's and mallocs to the worklist |
Chris Lattner | 5dac64f | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 138 | BasicBlock &BB = F.getEntryBlock(); |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 139 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) |
| 140 | if (AllocationInst *A = dyn_cast<AllocationInst>(I)) |
| 141 | WorkList.push_back(A); |
| 142 | |
| 143 | // Process the worklist |
| 144 | bool Changed = false; |
| 145 | while (!WorkList.empty()) { |
| 146 | AllocationInst *AI = WorkList.back(); |
| 147 | WorkList.pop_back(); |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 148 | |
Chris Lattner | f171af9 | 2006-12-22 23:14:42 +0000 | [diff] [blame] | 149 | // Handle dead allocas trivially. These can be formed by SROA'ing arrays |
| 150 | // with unused elements. |
| 151 | if (AI->use_empty()) { |
| 152 | AI->eraseFromParent(); |
| 153 | continue; |
| 154 | } |
| 155 | |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 156 | // If we can turn this aggregate value (potentially with casts) into a |
| 157 | // simple scalar value that can be mem2reg'd into a register value. |
| 158 | bool IsNotTrivial = false; |
| 159 | if (const Type *ActualType = CanConvertToScalar(AI, IsNotTrivial)) |
Chris Lattner | dae49df | 2006-04-20 20:48:50 +0000 | [diff] [blame] | 160 | if (IsNotTrivial && ActualType != Type::VoidTy) { |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 161 | ConvertToScalar(AI, ActualType); |
| 162 | Changed = true; |
| 163 | continue; |
| 164 | } |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 165 | |
| 166 | // We cannot transform the allocation instruction if it is an array |
Chris Lattner | c16b210 | 2003-05-27 16:09:27 +0000 | [diff] [blame] | 167 | // allocation (allocations OF arrays are ok though), and an allocation of a |
| 168 | // scalar value cannot be decomposed at all. |
| 169 | // |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 170 | if (AI->isArrayAllocation() || |
Chris Lattner | c16b210 | 2003-05-27 16:09:27 +0000 | [diff] [blame] | 171 | (!isa<StructType>(AI->getAllocatedType()) && |
| 172 | !isa<ArrayType>(AI->getAllocatedType()))) continue; |
| 173 | |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 174 | // Check that all of the users of the allocation are capable of being |
| 175 | // transformed. |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 176 | switch (isSafeAllocaToScalarRepl(AI)) { |
| 177 | default: assert(0 && "Unexpected value!"); |
| 178 | case 0: // Not safe to scalar replace. |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 179 | continue; |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 180 | case 1: // Safe, but requires cleanup/canonicalizations first |
| 181 | CanonicalizeAllocaUsers(AI); |
| 182 | case 3: // Safe to scalar replace. |
| 183 | break; |
| 184 | } |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 185 | |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 186 | DOUT << "Found inst to xform: " << *AI; |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 187 | Changed = true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 189 | SmallVector<AllocaInst*, 32> ElementAllocas; |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 190 | if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) { |
| 191 | ElementAllocas.reserve(ST->getNumContainedTypes()); |
| 192 | for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) { |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 193 | AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0, |
| 194 | AI->getAlignment(), |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 195 | AI->getName() + "." + utostr(i), AI); |
| 196 | ElementAllocas.push_back(NA); |
| 197 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 198 | } |
| 199 | } else { |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 200 | const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType()); |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 201 | ElementAllocas.reserve(AT->getNumElements()); |
| 202 | const Type *ElTy = AT->getElementType(); |
| 203 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 204 | AllocaInst *NA = new AllocaInst(ElTy, 0, AI->getAlignment(), |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 205 | AI->getName() + "." + utostr(i), AI); |
| 206 | ElementAllocas.push_back(NA); |
| 207 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 208 | } |
| 209 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 210 | |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 211 | // Now that we have created the alloca instructions that we want to use, |
| 212 | // expand the getelementptr instructions to use them. |
| 213 | // |
Chris Lattner | b5f8eb8 | 2004-06-19 02:02:22 +0000 | [diff] [blame] | 214 | while (!AI->use_empty()) { |
| 215 | Instruction *User = cast<Instruction>(AI->use_back()); |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 216 | if (BitCastInst *BCInst = dyn_cast<BitCastInst>(User)) { |
| 217 | RewriteBitCastUserOfAlloca(BCInst, AI, ElementAllocas); |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 218 | BCInst->eraseFromParent(); |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 219 | continue; |
| 220 | } |
| 221 | |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 222 | GetElementPtrInst *GEPI = cast<GetElementPtrInst>(User); |
| 223 | // We now know that the GEP is of the form: GEP <ptr>, 0, <cst> |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 224 | unsigned Idx = |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 225 | (unsigned)cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 226 | |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 227 | assert(Idx < ElementAllocas.size() && "Index out of range?"); |
| 228 | AllocaInst *AllocaToUse = ElementAllocas[Idx]; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 229 | |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 230 | Value *RepValue; |
| 231 | if (GEPI->getNumOperands() == 3) { |
| 232 | // Do not insert a new getelementptr instruction with zero indices, only |
| 233 | // to have it optimized out later. |
| 234 | RepValue = AllocaToUse; |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 235 | } else { |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 236 | // We are indexing deeply into the structure, so we still need a |
| 237 | // getelement ptr instruction to finish the indexing. This may be |
| 238 | // expanded itself once the worklist is rerun. |
| 239 | // |
Chris Lattner | a731513 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 240 | SmallVector<Value*, 8> NewArgs; |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 241 | NewArgs.push_back(Constant::getNullValue(Type::Int32Ty)); |
Chris Lattner | a731513 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 242 | NewArgs.append(GEPI->op_begin()+3, GEPI->op_end()); |
| 243 | RepValue = new GetElementPtrInst(AllocaToUse, &NewArgs[0], |
| 244 | NewArgs.size(), "", GEPI); |
Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 245 | RepValue->takeName(GEPI); |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 246 | } |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 247 | |
| 248 | // If this GEP is to the start of the aggregate, check for memcpys. |
| 249 | if (Idx == 0) { |
| 250 | bool IsStartOfAggregateGEP = true; |
| 251 | for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i) { |
| 252 | if (!isa<ConstantInt>(GEPI->getOperand(i))) { |
| 253 | IsStartOfAggregateGEP = false; |
| 254 | break; |
| 255 | } |
| 256 | if (!cast<ConstantInt>(GEPI->getOperand(i))->isZero()) { |
| 257 | IsStartOfAggregateGEP = false; |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (IsStartOfAggregateGEP) |
| 263 | RewriteBitCastUserOfAlloca(GEPI, AI, ElementAllocas); |
| 264 | } |
| 265 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 266 | |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 267 | // Move all of the users over to the new GEP. |
| 268 | GEPI->replaceAllUsesWith(RepValue); |
| 269 | // Delete the old GEP |
| 270 | GEPI->eraseFromParent(); |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // Finally, delete the Alloca instruction |
Chris Lattner | f171af9 | 2006-12-22 23:14:42 +0000 | [diff] [blame] | 274 | AI->eraseFromParent(); |
Chris Lattner | c16b210 | 2003-05-27 16:09:27 +0000 | [diff] [blame] | 275 | NumReplaced++; |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | return Changed; |
| 279 | } |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 280 | |
| 281 | |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 282 | /// isSafeElementUse - Check to see if this use is an allowed use for a |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 283 | /// getelementptr instruction of an array aggregate allocation. isFirstElt |
| 284 | /// indicates whether Ptr is known to the start of the aggregate. |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 285 | /// |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 286 | int SROA::isSafeElementUse(Value *Ptr, bool isFirstElt, AllocationInst *AI) { |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 287 | for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end(); |
| 288 | I != E; ++I) { |
| 289 | Instruction *User = cast<Instruction>(*I); |
| 290 | switch (User->getOpcode()) { |
| 291 | case Instruction::Load: break; |
| 292 | case Instruction::Store: |
| 293 | // Store is ok if storing INTO the pointer, not storing the pointer |
| 294 | if (User->getOperand(0) == Ptr) return 0; |
| 295 | break; |
| 296 | case Instruction::GetElementPtr: { |
| 297 | GetElementPtrInst *GEP = cast<GetElementPtrInst>(User); |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 298 | bool AreAllZeroIndices = isFirstElt; |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 299 | if (GEP->getNumOperands() > 1) { |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 300 | if (!isa<ConstantInt>(GEP->getOperand(1)) || |
| 301 | !cast<ConstantInt>(GEP->getOperand(1))->isZero()) |
| 302 | return 0; // Using pointer arithmetic to navigate the array. |
| 303 | |
| 304 | if (AreAllZeroIndices) { |
| 305 | for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) { |
| 306 | if (!isa<ConstantInt>(GEP->getOperand(i)) || |
| 307 | !cast<ConstantInt>(GEP->getOperand(i))->isZero()) { |
| 308 | AreAllZeroIndices = false; |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 313 | } |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 314 | if (!isSafeElementUse(GEP, AreAllZeroIndices, AI)) return 0; |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 315 | break; |
| 316 | } |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 317 | case Instruction::BitCast: |
| 318 | if (isFirstElt && |
| 319 | isSafeUseOfBitCastedAllocation(cast<BitCastInst>(User), AI)) |
| 320 | break; |
| 321 | DOUT << " Transformation preventing inst: " << *User; |
| 322 | return 0; |
| 323 | case Instruction::Call: |
| 324 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) { |
| 325 | if (isFirstElt && isSafeMemIntrinsicOnAllocation(MI, AI)) |
| 326 | break; |
| 327 | } |
| 328 | DOUT << " Transformation preventing inst: " << *User; |
| 329 | return 0; |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 330 | default: |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 331 | DOUT << " Transformation preventing inst: " << *User; |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | } |
| 335 | return 3; // All users look ok :) |
| 336 | } |
| 337 | |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 338 | /// AllUsersAreLoads - Return true if all users of this value are loads. |
| 339 | static bool AllUsersAreLoads(Value *Ptr) { |
| 340 | for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end(); |
| 341 | I != E; ++I) |
| 342 | if (cast<Instruction>(*I)->getOpcode() != Instruction::Load) |
| 343 | return false; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 344 | return true; |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 347 | /// isSafeUseOfAllocation - Check to see if this user is an allowed use for an |
| 348 | /// aggregate allocation. |
| 349 | /// |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 350 | int SROA::isSafeUseOfAllocation(Instruction *User, AllocationInst *AI) { |
| 351 | if (BitCastInst *C = dyn_cast<BitCastInst>(User)) |
Chris Lattner | abd3bff | 2007-03-08 07:03:55 +0000 | [diff] [blame] | 352 | return isSafeUseOfBitCastedAllocation(C, AI) ? 3 : 0; |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 353 | if (!isa<GetElementPtrInst>(User)) return 0; |
Chris Lattner | 5231070 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 354 | |
| 355 | GetElementPtrInst *GEPI = cast<GetElementPtrInst>(User); |
| 356 | gep_type_iterator I = gep_type_begin(GEPI), E = gep_type_end(GEPI); |
| 357 | |
Chris Lattner | fc34f8b | 2006-03-08 01:05:29 +0000 | [diff] [blame] | 358 | // The GEP is not safe to transform if not of the form "GEP <ptr>, 0, <cst>". |
Chris Lattner | 5231070 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 359 | if (I == E || |
| 360 | I.getOperand() != Constant::getNullValue(I.getOperand()->getType())) |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 361 | return 0; |
Chris Lattner | 5231070 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 362 | |
| 363 | ++I; |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 364 | if (I == E) return 0; // ran out of GEP indices?? |
Chris Lattner | 5231070 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 365 | |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 366 | bool IsAllZeroIndices = true; |
| 367 | |
Chris Lattner | 5231070 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 368 | // If this is a use of an array allocation, do a bit more checking for sanity. |
| 369 | if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) { |
| 370 | uint64_t NumElements = AT->getNumElements(); |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 372 | if (ConstantInt *Idx = dyn_cast<ConstantInt>(I.getOperand())) { |
| 373 | IsAllZeroIndices &= Idx->isZero(); |
| 374 | |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 375 | // Check to make sure that index falls within the array. If not, |
| 376 | // something funny is going on, so we won't do the optimization. |
| 377 | // |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 378 | if (Idx->getZExtValue() >= NumElements) |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 379 | return 0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 380 | |
Chris Lattner | fc34f8b | 2006-03-08 01:05:29 +0000 | [diff] [blame] | 381 | // We cannot scalar repl this level of the array unless any array |
| 382 | // sub-indices are in-range constants. In particular, consider: |
| 383 | // A[0][i]. We cannot know that the user isn't doing invalid things like |
| 384 | // allowing i to index an out-of-range subscript that accesses A[1]. |
| 385 | // |
| 386 | // Scalar replacing *just* the outer index of the array is probably not |
| 387 | // going to be a win anyway, so just give up. |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 388 | for (++I; I != E && (isa<ArrayType>(*I) || isa<VectorType>(*I)); ++I) { |
Chris Lattner | 4967f6d | 2006-11-07 22:42:47 +0000 | [diff] [blame] | 389 | uint64_t NumElements; |
| 390 | if (const ArrayType *SubArrayTy = dyn_cast<ArrayType>(*I)) |
| 391 | NumElements = SubArrayTy->getNumElements(); |
| 392 | else |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 393 | NumElements = cast<VectorType>(*I)->getNumElements(); |
Chris Lattner | 4967f6d | 2006-11-07 22:42:47 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 395 | ConstantInt *IdxVal = dyn_cast<ConstantInt>(I.getOperand()); |
| 396 | if (!IdxVal) return 0; |
| 397 | if (IdxVal->getZExtValue() >= NumElements) |
Chris Lattner | fc34f8b | 2006-03-08 01:05:29 +0000 | [diff] [blame] | 398 | return 0; |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 399 | IsAllZeroIndices &= IdxVal->isZero(); |
Chris Lattner | fc34f8b | 2006-03-08 01:05:29 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 402 | } else { |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 403 | IsAllZeroIndices = 0; |
| 404 | |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 405 | // If this is an array index and the index is not constant, we cannot |
| 406 | // promote... that is unless the array has exactly one or two elements in |
| 407 | // it, in which case we CAN promote it, but we have to canonicalize this |
| 408 | // out if this is the only problem. |
Chris Lattner | fc34f8b | 2006-03-08 01:05:29 +0000 | [diff] [blame] | 409 | if ((NumElements == 1 || NumElements == 2) && |
| 410 | AllUsersAreLoads(GEPI)) |
| 411 | return 1; // Canonicalization required! |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 412 | return 0; |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 413 | } |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 414 | } |
Chris Lattner | 5231070 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 415 | |
| 416 | // If there are any non-simple uses of this getelementptr, make sure to reject |
| 417 | // them. |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 418 | return isSafeElementUse(GEPI, IsAllZeroIndices, AI); |
| 419 | } |
| 420 | |
| 421 | /// isSafeMemIntrinsicOnAllocation - Return true if the specified memory |
| 422 | /// intrinsic can be promoted by SROA. At this point, we know that the operand |
| 423 | /// of the memintrinsic is a pointer to the beginning of the allocation. |
| 424 | bool SROA::isSafeMemIntrinsicOnAllocation(MemIntrinsic *MI, AllocationInst *AI){ |
| 425 | // If not constant length, give up. |
| 426 | ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength()); |
| 427 | if (!Length) return false; |
| 428 | |
| 429 | // If not the whole aggregate, give up. |
| 430 | const TargetData &TD = getAnalysis<TargetData>(); |
| 431 | if (Length->getZExtValue() != TD.getTypeSize(AI->getType()->getElementType())) |
| 432 | return false; |
| 433 | |
| 434 | // We only know about memcpy/memset/memmove. |
| 435 | if (!isa<MemCpyInst>(MI) && !isa<MemSetInst>(MI) && !isa<MemMoveInst>(MI)) |
| 436 | return false; |
| 437 | // Otherwise, we can transform it. |
| 438 | return true; |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 441 | /// isSafeUseOfBitCastedAllocation - Return true if all users of this bitcast |
| 442 | /// are |
| 443 | bool SROA::isSafeUseOfBitCastedAllocation(BitCastInst *BC, AllocationInst *AI) { |
| 444 | for (Value::use_iterator UI = BC->use_begin(), E = BC->use_end(); |
| 445 | UI != E; ++UI) { |
| 446 | if (BitCastInst *BCU = dyn_cast<BitCastInst>(UI)) { |
| 447 | if (!isSafeUseOfBitCastedAllocation(BCU, AI)) |
| 448 | return false; |
| 449 | } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(UI)) { |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 450 | if (!isSafeMemIntrinsicOnAllocation(MI, AI)) |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 451 | return false; |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 452 | } else { |
| 453 | return false; |
| 454 | } |
| 455 | } |
| 456 | return true; |
| 457 | } |
| 458 | |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 459 | /// RewriteBitCastUserOfAlloca - BCInst (transitively) bitcasts AI, or indexes |
| 460 | /// to its first element. Transform users of the cast to use the new values |
| 461 | /// instead. |
| 462 | void SROA::RewriteBitCastUserOfAlloca(Instruction *BCInst, AllocationInst *AI, |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 463 | SmallVector<AllocaInst*, 32> &NewElts) { |
| 464 | Constant *Zero = Constant::getNullValue(Type::Int32Ty); |
| 465 | const TargetData &TD = getAnalysis<TargetData>(); |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 466 | |
| 467 | Value::use_iterator UI = BCInst->use_begin(), UE = BCInst->use_end(); |
| 468 | while (UI != UE) { |
| 469 | if (BitCastInst *BCU = dyn_cast<BitCastInst>(*UI)) { |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 470 | RewriteBitCastUserOfAlloca(BCU, AI, NewElts); |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 471 | ++UI; |
| 472 | BCU->eraseFromParent(); |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 473 | continue; |
| 474 | } |
| 475 | |
| 476 | // Otherwise, must be memcpy/memmove/memset of the entire aggregate. Split |
| 477 | // into one per element. |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 478 | MemIntrinsic *MI = dyn_cast<MemIntrinsic>(*UI); |
| 479 | |
| 480 | // If it's not a mem intrinsic, it must be some other user of a gep of the |
| 481 | // first pointer. Just leave these alone. |
| 482 | if (!MI) { |
| 483 | ++UI; |
| 484 | continue; |
| 485 | } |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 486 | |
| 487 | // If this is a memcpy/memmove, construct the other pointer as the |
| 488 | // appropriate type. |
| 489 | Value *OtherPtr = 0; |
| 490 | if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(MI)) { |
| 491 | if (BCInst == MCI->getRawDest()) |
| 492 | OtherPtr = MCI->getRawSource(); |
| 493 | else { |
| 494 | assert(BCInst == MCI->getRawSource()); |
| 495 | OtherPtr = MCI->getRawDest(); |
| 496 | } |
| 497 | } else if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
| 498 | if (BCInst == MMI->getRawDest()) |
| 499 | OtherPtr = MMI->getRawSource(); |
| 500 | else { |
| 501 | assert(BCInst == MMI->getRawSource()); |
| 502 | OtherPtr = MMI->getRawDest(); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // If there is an other pointer, we want to convert it to the same pointer |
| 507 | // type as AI has, so we can GEP through it. |
| 508 | if (OtherPtr) { |
| 509 | // It is likely that OtherPtr is a bitcast, if so, remove it. |
| 510 | if (BitCastInst *BC = dyn_cast<BitCastInst>(OtherPtr)) |
| 511 | OtherPtr = BC->getOperand(0); |
| 512 | if (ConstantExpr *BCE = dyn_cast<ConstantExpr>(OtherPtr)) |
| 513 | if (BCE->getOpcode() == Instruction::BitCast) |
| 514 | OtherPtr = BCE->getOperand(0); |
| 515 | |
| 516 | // If the pointer is not the right type, insert a bitcast to the right |
| 517 | // type. |
| 518 | if (OtherPtr->getType() != AI->getType()) |
| 519 | OtherPtr = new BitCastInst(OtherPtr, AI->getType(), OtherPtr->getName(), |
| 520 | MI); |
| 521 | } |
| 522 | |
| 523 | // Process each element of the aggregate. |
| 524 | Value *TheFn = MI->getOperand(0); |
| 525 | const Type *BytePtrTy = MI->getRawDest()->getType(); |
| 526 | bool SROADest = MI->getRawDest() == BCInst; |
| 527 | |
| 528 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 529 | // If this is a memcpy/memmove, emit a GEP of the other element address. |
| 530 | Value *OtherElt = 0; |
| 531 | if (OtherPtr) { |
| 532 | OtherElt = new GetElementPtrInst(OtherPtr, Zero, |
| 533 | ConstantInt::get(Type::Int32Ty, i), |
| 534 | OtherPtr->getNameStr()+"."+utostr(i), |
| 535 | MI); |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | Value *EltPtr = NewElts[i]; |
Chris Lattner | 9f022d5 | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 539 | const Type *EltTy =cast<PointerType>(EltPtr->getType())->getElementType(); |
| 540 | |
| 541 | // If we got down to a scalar, insert a load or store as appropriate. |
| 542 | if (EltTy->isFirstClassType()) { |
| 543 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
| 544 | Value *Elt = new LoadInst(SROADest ? OtherElt : EltPtr, "tmp", |
| 545 | MI); |
| 546 | new StoreInst(Elt, SROADest ? EltPtr : OtherElt, MI); |
| 547 | continue; |
| 548 | } else { |
| 549 | assert(isa<MemSetInst>(MI)); |
| 550 | |
| 551 | // If the stored element is zero (common case), just store a null |
| 552 | // constant. |
| 553 | Constant *StoreVal; |
| 554 | if (ConstantInt *CI = dyn_cast<ConstantInt>(MI->getOperand(2))) { |
| 555 | if (CI->isZero()) { |
| 556 | StoreVal = Constant::getNullValue(EltTy); // 0.0, null, 0, <0,0> |
| 557 | } else { |
| 558 | // If EltTy is a packed type, get the element type. |
| 559 | const Type *ValTy = EltTy; |
| 560 | if (const VectorType *VTy = dyn_cast<VectorType>(ValTy)) |
| 561 | ValTy = VTy->getElementType(); |
| 562 | |
| 563 | // Construct an integer with the right value. |
| 564 | unsigned EltSize = TD.getTypeSize(ValTy); |
| 565 | APInt OneVal(EltSize*8, CI->getZExtValue()); |
| 566 | APInt TotalVal(OneVal); |
| 567 | // Set each byte. |
| 568 | for (unsigned i = 0; i != EltSize-1; ++i) { |
| 569 | TotalVal = TotalVal.shl(8); |
| 570 | TotalVal |= OneVal; |
| 571 | } |
| 572 | |
| 573 | // Convert the integer value to the appropriate type. |
| 574 | StoreVal = ConstantInt::get(TotalVal); |
| 575 | if (isa<PointerType>(ValTy)) |
| 576 | StoreVal = ConstantExpr::getIntToPtr(StoreVal, ValTy); |
| 577 | else if (ValTy->isFloatingPoint()) |
| 578 | StoreVal = ConstantExpr::getBitCast(StoreVal, ValTy); |
| 579 | assert(StoreVal->getType() == ValTy && "Type mismatch!"); |
| 580 | |
| 581 | // If the requested value was a vector constant, create it. |
| 582 | if (EltTy != ValTy) { |
| 583 | unsigned NumElts = cast<VectorType>(ValTy)->getNumElements(); |
| 584 | SmallVector<Constant*, 16> Elts(NumElts, StoreVal); |
| 585 | StoreVal = ConstantVector::get(&Elts[0], NumElts); |
| 586 | } |
| 587 | } |
| 588 | new StoreInst(StoreVal, EltPtr, MI); |
| 589 | continue; |
| 590 | } |
| 591 | // Otherwise, if we're storing a byte variable, use a memset call for |
| 592 | // this element. |
| 593 | } |
| 594 | } |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 595 | |
| 596 | // Cast the element pointer to BytePtrTy. |
| 597 | if (EltPtr->getType() != BytePtrTy) |
| 598 | EltPtr = new BitCastInst(EltPtr, BytePtrTy, EltPtr->getNameStr(), MI); |
Chris Lattner | 9f022d5 | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 599 | |
| 600 | // Cast the other pointer (if we have one) to BytePtrTy. |
| 601 | if (OtherElt && OtherElt->getType() != BytePtrTy) |
| 602 | OtherElt = new BitCastInst(OtherElt, BytePtrTy,OtherElt->getNameStr(), |
| 603 | MI); |
| 604 | |
| 605 | unsigned EltSize = TD.getTypeSize(EltTy); |
| 606 | |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 607 | // Finally, insert the meminst for this element. |
| 608 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
| 609 | Value *Ops[] = { |
| 610 | SROADest ? EltPtr : OtherElt, // Dest ptr |
| 611 | SROADest ? OtherElt : EltPtr, // Src ptr |
| 612 | ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size |
| 613 | Zero // Align |
| 614 | }; |
| 615 | new CallInst(TheFn, Ops, 4, "", MI); |
Chris Lattner | 9f022d5 | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 616 | } else { |
| 617 | assert(isa<MemSetInst>(MI)); |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 618 | Value *Ops[] = { |
| 619 | EltPtr, MI->getOperand(2), // Dest, Value, |
| 620 | ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size |
| 621 | Zero // Align |
| 622 | }; |
| 623 | new CallInst(TheFn, Ops, 4, "", MI); |
| 624 | } |
Chris Lattner | 9f022d5 | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 625 | } |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 626 | |
| 627 | // Finally, MI is now dead, as we've modified its actions to occur on all of |
| 628 | // the elements of the aggregate. |
Chris Lattner | 877a3b4 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 629 | ++UI; |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 630 | MI->eraseFromParent(); |
| 631 | } |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 635 | /// isSafeStructAllocaToScalarRepl - Check to see if the specified allocation of |
| 636 | /// an aggregate can be broken down into elements. Return 0 if not, 3 if safe, |
| 637 | /// or 1 if safe after canonicalization has been performed. |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 638 | /// |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 639 | int SROA::isSafeAllocaToScalarRepl(AllocationInst *AI) { |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 640 | // Loop over the use list of the alloca. We can only transform it if all of |
| 641 | // the users are safe to transform. |
| 642 | // |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 643 | int isSafe = 3; |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 644 | for (Value::use_iterator I = AI->use_begin(), E = AI->use_end(); |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 645 | I != E; ++I) { |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 646 | isSafe &= isSafeUseOfAllocation(cast<Instruction>(*I), AI); |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 647 | if (isSafe == 0) { |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 648 | DOUT << "Cannot transform: " << *AI << " due to user: " << **I; |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 649 | return 0; |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 650 | } |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 651 | } |
| 652 | // If we require cleanup, isSafe is now 1, otherwise it is 3. |
| 653 | return isSafe; |
| 654 | } |
| 655 | |
| 656 | /// CanonicalizeAllocaUsers - If SROA reported that it can promote the specified |
| 657 | /// allocation, but only if cleaned up, perform the cleanups required. |
| 658 | void SROA::CanonicalizeAllocaUsers(AllocationInst *AI) { |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 659 | // At this point, we know that the end result will be SROA'd and promoted, so |
| 660 | // we can insert ugly code if required so long as sroa+mem2reg will clean it |
| 661 | // up. |
| 662 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 663 | UI != E; ) { |
Chris Lattner | 9c62db7 | 2007-03-19 18:25:57 +0000 | [diff] [blame] | 664 | GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI++); |
| 665 | if (!GEPI) continue; |
Reid Spencer | 9339638 | 2004-11-15 17:29:41 +0000 | [diff] [blame] | 666 | gep_type_iterator I = gep_type_begin(GEPI); |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 667 | ++I; |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 668 | |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 669 | if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) { |
| 670 | uint64_t NumElements = AT->getNumElements(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 671 | |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 672 | if (!isa<ConstantInt>(I.getOperand())) { |
| 673 | if (NumElements == 1) { |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 674 | GEPI->setOperand(2, Constant::getNullValue(Type::Int32Ty)); |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 675 | } else { |
| 676 | assert(NumElements == 2 && "Unhandled case!"); |
| 677 | // All users of the GEP must be loads. At each use of the GEP, insert |
| 678 | // two loads of the appropriate indexed GEP and select between them. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 679 | Value *IsOne = new ICmpInst(ICmpInst::ICMP_NE, I.getOperand(), |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 680 | Constant::getNullValue(I.getOperand()->getType()), |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 681 | "isone", GEPI); |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 682 | // Insert the new GEP instructions, which are properly indexed. |
Chris Lattner | a731513 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 683 | SmallVector<Value*, 8> Indices(GEPI->op_begin()+1, GEPI->op_end()); |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 684 | Indices[1] = Constant::getNullValue(Type::Int32Ty); |
Chris Lattner | a731513 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 685 | Value *ZeroIdx = new GetElementPtrInst(GEPI->getOperand(0), |
| 686 | &Indices[0], Indices.size(), |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 687 | GEPI->getName()+".0", GEPI); |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 688 | Indices[1] = ConstantInt::get(Type::Int32Ty, 1); |
Chris Lattner | a731513 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 689 | Value *OneIdx = new GetElementPtrInst(GEPI->getOperand(0), |
| 690 | &Indices[0], Indices.size(), |
Chris Lattner | fe3f4e6 | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 691 | GEPI->getName()+".1", GEPI); |
| 692 | // Replace all loads of the variable index GEP with loads from both |
| 693 | // indexes and a select. |
| 694 | while (!GEPI->use_empty()) { |
| 695 | LoadInst *LI = cast<LoadInst>(GEPI->use_back()); |
| 696 | Value *Zero = new LoadInst(ZeroIdx, LI->getName()+".0", LI); |
| 697 | Value *One = new LoadInst(OneIdx , LI->getName()+".1", LI); |
| 698 | Value *R = new SelectInst(IsOne, One, Zero, LI->getName(), LI); |
| 699 | LI->replaceAllUsesWith(R); |
| 700 | LI->eraseFromParent(); |
| 701 | } |
| 702 | GEPI->eraseFromParent(); |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | } |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 707 | } |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 708 | |
| 709 | /// MergeInType - Add the 'In' type to the accumulated type so far. If the |
| 710 | /// types are incompatible, return true, otherwise update Accum and return |
| 711 | /// false. |
Chris Lattner | 3323ce1 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 712 | /// |
Chris Lattner | 8f7b775 | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 713 | /// There are three cases we handle here: |
| 714 | /// 1) An effectively-integer union, where the pieces are stored into as |
Chris Lattner | 3323ce1 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 715 | /// smaller integers (common with byte swap and other idioms). |
Chris Lattner | 8f7b775 | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 716 | /// 2) A union of vector types of the same size and potentially its elements. |
| 717 | /// Here we turn element accesses into insert/extract element operations. |
| 718 | /// 3) A union of scalar types, such as int/float or int/pointer. Here we |
| 719 | /// merge together into integers, allowing the xform to work with #1 as |
| 720 | /// well. |
Chris Lattner | 05f8272 | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 721 | static bool MergeInType(const Type *In, const Type *&Accum, |
| 722 | const TargetData &TD) { |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 723 | // If this is our first type, just use it. |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 724 | const VectorType *PTy; |
Chris Lattner | 3323ce1 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 725 | if (Accum == Type::VoidTy || In == Accum) { |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 726 | Accum = In; |
Chris Lattner | 8f7b775 | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 727 | } else if (In == Type::VoidTy) { |
| 728 | // Noop. |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 729 | } else if (In->isInteger() && Accum->isInteger()) { // integer union. |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 730 | // Otherwise pick whichever type is larger. |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 731 | if (cast<IntegerType>(In)->getBitWidth() > |
| 732 | cast<IntegerType>(Accum)->getBitWidth()) |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 733 | Accum = In; |
Chris Lattner | 05f8272 | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 734 | } else if (isa<PointerType>(In) && isa<PointerType>(Accum)) { |
Chris Lattner | 41b4422 | 2006-10-08 23:53:04 +0000 | [diff] [blame] | 735 | // Pointer unions just stay as one of the pointers. |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 736 | } else if (isa<VectorType>(In) || isa<VectorType>(Accum)) { |
| 737 | if ((PTy = dyn_cast<VectorType>(Accum)) && |
Chris Lattner | 8f7b775 | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 738 | PTy->getElementType() == In) { |
| 739 | // Accum is a vector, and we are accessing an element: ok. |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 740 | } else if ((PTy = dyn_cast<VectorType>(In)) && |
Chris Lattner | 8f7b775 | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 741 | PTy->getElementType() == Accum) { |
| 742 | // In is a vector, and accum is an element: ok, remember In. |
| 743 | Accum = In; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 744 | } else if ((PTy = dyn_cast<VectorType>(In)) && isa<VectorType>(Accum) && |
| 745 | PTy->getBitWidth() == cast<VectorType>(Accum)->getBitWidth()) { |
Chris Lattner | 8f7b775 | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 746 | // Two vectors of the same size: keep Accum. |
| 747 | } else { |
| 748 | // Cannot insert an short into a <4 x int> or handle |
| 749 | // <2 x int> -> <4 x int> |
| 750 | return true; |
| 751 | } |
Chris Lattner | 7c1dff9 | 2006-12-13 02:26:45 +0000 | [diff] [blame] | 752 | } else { |
Chris Lattner | 8f7b775 | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 753 | // Pointer/FP/Integer unions merge together as integers. |
| 754 | switch (Accum->getTypeID()) { |
| 755 | case Type::PointerTyID: Accum = TD.getIntPtrType(); break; |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 756 | case Type::FloatTyID: Accum = Type::Int32Ty; break; |
| 757 | case Type::DoubleTyID: Accum = Type::Int64Ty; break; |
Chris Lattner | 8f7b775 | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 758 | default: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 759 | assert(Accum->isInteger() && "Unknown FP type!"); |
Chris Lattner | 8f7b775 | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 760 | break; |
| 761 | } |
| 762 | |
| 763 | switch (In->getTypeID()) { |
| 764 | case Type::PointerTyID: In = TD.getIntPtrType(); break; |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 765 | case Type::FloatTyID: In = Type::Int32Ty; break; |
| 766 | case Type::DoubleTyID: In = Type::Int64Ty; break; |
Chris Lattner | 8f7b775 | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 767 | default: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 768 | assert(In->isInteger() && "Unknown FP type!"); |
Chris Lattner | 8f7b775 | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 769 | break; |
| 770 | } |
| 771 | return MergeInType(In, Accum, TD); |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 772 | } |
| 773 | return false; |
| 774 | } |
| 775 | |
| 776 | /// getUIntAtLeastAsBitAs - Return an unsigned integer type that is at least |
| 777 | /// as big as the specified type. If there is no suitable type, this returns |
| 778 | /// null. |
| 779 | const Type *getUIntAtLeastAsBitAs(unsigned NumBits) { |
| 780 | if (NumBits > 64) return 0; |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 781 | if (NumBits > 32) return Type::Int64Ty; |
| 782 | if (NumBits > 16) return Type::Int32Ty; |
| 783 | if (NumBits > 8) return Type::Int16Ty; |
| 784 | return Type::Int8Ty; |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /// CanConvertToScalar - V is a pointer. If we can convert the pointee to a |
| 788 | /// single scalar integer type, return that type. Further, if the use is not |
| 789 | /// a completely trivial use that mem2reg could promote, set IsNotTrivial. If |
| 790 | /// there are no uses of this pointer, return Type::VoidTy to differentiate from |
| 791 | /// failure. |
| 792 | /// |
| 793 | const Type *SROA::CanConvertToScalar(Value *V, bool &IsNotTrivial) { |
| 794 | const Type *UsedType = Type::VoidTy; // No uses, no forced type. |
| 795 | const TargetData &TD = getAnalysis<TargetData>(); |
| 796 | const PointerType *PTy = cast<PointerType>(V->getType()); |
| 797 | |
| 798 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) { |
| 799 | Instruction *User = cast<Instruction>(*UI); |
| 800 | |
| 801 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
Chris Lattner | 05f8272 | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 802 | if (MergeInType(LI->getType(), UsedType, TD)) |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 803 | return 0; |
| 804 | |
| 805 | } else if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 806 | // Storing the pointer, not into the value? |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 807 | if (SI->getOperand(0) == V) return 0; |
| 808 | |
Chris Lattner | 3323ce1 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 809 | // NOTE: We could handle storing of FP imms into integers here! |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 810 | |
Chris Lattner | 05f8272 | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 811 | if (MergeInType(SI->getOperand(0)->getType(), UsedType, TD)) |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 812 | return 0; |
Chris Lattner | 8f7b775 | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 813 | } else if (BitCastInst *CI = dyn_cast<BitCastInst>(User)) { |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 814 | IsNotTrivial = true; |
| 815 | const Type *SubTy = CanConvertToScalar(CI, IsNotTrivial); |
Chris Lattner | 05f8272 | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 816 | if (!SubTy || MergeInType(SubTy, UsedType, TD)) return 0; |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 817 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) { |
| 818 | // Check to see if this is stepping over an element: GEP Ptr, int C |
| 819 | if (GEP->getNumOperands() == 2 && isa<ConstantInt>(GEP->getOperand(1))) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 820 | unsigned Idx = cast<ConstantInt>(GEP->getOperand(1))->getZExtValue(); |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 821 | unsigned ElSize = TD.getTypeSize(PTy->getElementType()); |
| 822 | unsigned BitOffset = Idx*ElSize*8; |
| 823 | if (BitOffset > 64 || !isPowerOf2_32(ElSize)) return 0; |
| 824 | |
| 825 | IsNotTrivial = true; |
| 826 | const Type *SubElt = CanConvertToScalar(GEP, IsNotTrivial); |
| 827 | if (SubElt == 0) return 0; |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 828 | if (SubElt != Type::VoidTy && SubElt->isInteger()) { |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 829 | const Type *NewTy = |
Chris Lattner | 41b4422 | 2006-10-08 23:53:04 +0000 | [diff] [blame] | 830 | getUIntAtLeastAsBitAs(TD.getTypeSize(SubElt)*8+BitOffset); |
Chris Lattner | 05f8272 | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 831 | if (NewTy == 0 || MergeInType(NewTy, UsedType, TD)) return 0; |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 832 | continue; |
| 833 | } |
| 834 | } else if (GEP->getNumOperands() == 3 && |
| 835 | isa<ConstantInt>(GEP->getOperand(1)) && |
| 836 | isa<ConstantInt>(GEP->getOperand(2)) && |
Zhou Sheng | aafe4e2 | 2007-04-19 05:39:12 +0000 | [diff] [blame^] | 837 | cast<ConstantInt>(GEP->getOperand(1))->isZero()) { |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 838 | // We are stepping into an element, e.g. a structure or an array: |
| 839 | // GEP Ptr, int 0, uint C |
| 840 | const Type *AggTy = PTy->getElementType(); |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 841 | unsigned Idx = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue(); |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 842 | |
| 843 | if (const ArrayType *ATy = dyn_cast<ArrayType>(AggTy)) { |
| 844 | if (Idx >= ATy->getNumElements()) return 0; // Out of range. |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 845 | } else if (const VectorType *VectorTy = dyn_cast<VectorType>(AggTy)) { |
Chris Lattner | 3323ce1 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 846 | // Getting an element of the packed vector. |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 847 | if (Idx >= VectorTy->getNumElements()) return 0; // Out of range. |
Chris Lattner | 3323ce1 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 848 | |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 849 | // Merge in the vector type. |
| 850 | if (MergeInType(VectorTy, UsedType, TD)) return 0; |
Chris Lattner | 3323ce1 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 851 | |
| 852 | const Type *SubTy = CanConvertToScalar(GEP, IsNotTrivial); |
| 853 | if (SubTy == 0) return 0; |
| 854 | |
Chris Lattner | 05f8272 | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 855 | if (SubTy != Type::VoidTy && MergeInType(SubTy, UsedType, TD)) |
Chris Lattner | 3323ce1 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 856 | return 0; |
| 857 | |
| 858 | // We'll need to change this to an insert/extract element operation. |
| 859 | IsNotTrivial = true; |
| 860 | continue; // Everything looks ok |
| 861 | |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 862 | } else if (isa<StructType>(AggTy)) { |
| 863 | // Structs are always ok. |
| 864 | } else { |
| 865 | return 0; |
| 866 | } |
| 867 | const Type *NTy = getUIntAtLeastAsBitAs(TD.getTypeSize(AggTy)*8); |
Chris Lattner | 05f8272 | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 868 | if (NTy == 0 || MergeInType(NTy, UsedType, TD)) return 0; |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 869 | const Type *SubTy = CanConvertToScalar(GEP, IsNotTrivial); |
| 870 | if (SubTy == 0) return 0; |
Chris Lattner | 05f8272 | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 871 | if (SubTy != Type::VoidTy && MergeInType(SubTy, UsedType, TD)) |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 872 | return 0; |
| 873 | continue; // Everything looks ok |
| 874 | } |
| 875 | return 0; |
| 876 | } else { |
| 877 | // Cannot handle this! |
| 878 | return 0; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | return UsedType; |
| 883 | } |
| 884 | |
| 885 | /// ConvertToScalar - The specified alloca passes the CanConvertToScalar |
| 886 | /// predicate and is non-trivial. Convert it to something that can be trivially |
| 887 | /// promoted into a register by mem2reg. |
| 888 | void SROA::ConvertToScalar(AllocationInst *AI, const Type *ActualTy) { |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 889 | DOUT << "CONVERT TO SCALAR: " << *AI << " TYPE = " |
| 890 | << *ActualTy << "\n"; |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 891 | ++NumConverted; |
| 892 | |
| 893 | BasicBlock *EntryBlock = AI->getParent(); |
Dan Gohman | dcb291f | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 894 | assert(EntryBlock == &EntryBlock->getParent()->getEntryBlock() && |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 895 | "Not in the entry block!"); |
| 896 | EntryBlock->getInstList().remove(AI); // Take the alloca out of the program. |
| 897 | |
| 898 | // Create and insert the alloca. |
Chris Lattner | 3323ce1 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 899 | AllocaInst *NewAI = new AllocaInst(ActualTy, 0, AI->getName(), |
| 900 | EntryBlock->begin()); |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 901 | ConvertUsesToScalar(AI, NewAI, 0); |
| 902 | delete AI; |
| 903 | } |
| 904 | |
| 905 | |
| 906 | /// ConvertUsesToScalar - Convert all of the users of Ptr to use the new alloca |
Chris Lattner | 3323ce1 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 907 | /// directly. This happens when we are converting an "integer union" to a |
| 908 | /// single integer scalar, or when we are converting a "vector union" to a |
| 909 | /// vector with insert/extractelement instructions. |
| 910 | /// |
| 911 | /// Offset is an offset from the original alloca, in bits that need to be |
| 912 | /// shifted to the right. By the end of this, there should be no uses of Ptr. |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 913 | void SROA::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, unsigned Offset) { |
Chris Lattner | 41b4422 | 2006-10-08 23:53:04 +0000 | [diff] [blame] | 914 | const TargetData &TD = getAnalysis<TargetData>(); |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 915 | while (!Ptr->use_empty()) { |
| 916 | Instruction *User = cast<Instruction>(Ptr->use_back()); |
| 917 | |
| 918 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
| 919 | // The load is a bit extract from NewAI shifted right by Offset bits. |
| 920 | Value *NV = new LoadInst(NewAI, LI->getName(), LI); |
Chris Lattner | daa012d | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 921 | if (NV->getType() == LI->getType()) { |
| 922 | // We win, no conversion needed. |
| 923 | } else if (const VectorType *PTy = dyn_cast<VectorType>(NV->getType())) { |
| 924 | // If the result alloca is a vector type, this is either an element |
| 925 | // access or a bitcast to another vector type. |
| 926 | if (isa<VectorType>(LI->getType())) { |
Chris Lattner | 8f7b775 | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 927 | NV = new BitCastInst(NV, LI->getType(), LI->getName(), LI); |
| 928 | } else { |
Chris Lattner | daa012d | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 929 | // Must be an element access. |
| 930 | unsigned Elt = Offset/(TD.getTypeSize(PTy->getElementType())*8); |
| 931 | NV = new ExtractElementInst( |
| 932 | NV, ConstantInt::get(Type::Int32Ty, Elt), "tmp", LI); |
| 933 | } |
| 934 | } else if (isa<PointerType>(NV->getType())) { |
| 935 | assert(isa<PointerType>(LI->getType())); |
| 936 | // Must be ptr->ptr cast. Anything else would result in NV being |
| 937 | // an integer. |
| 938 | NV = new BitCastInst(NV, LI->getType(), LI->getName(), LI); |
| 939 | } else { |
| 940 | const IntegerType *NTy = cast<IntegerType>(NV->getType()); |
| 941 | unsigned LIBitWidth = TD.getTypeSizeInBits(LI->getType()); |
| 942 | |
| 943 | // If this is a big-endian system and the load is narrower than the |
| 944 | // full alloca type, we need to do a shift to get the right bits. |
| 945 | int ShAmt = 0; |
| 946 | if (TD.isBigEndian()) { |
| 947 | ShAmt = NTy->getBitWidth()-LIBitWidth-Offset; |
| 948 | } else { |
| 949 | ShAmt = Offset; |
| 950 | } |
| 951 | |
| 952 | // Note: we support negative bitwidths (with shl) which are not defined. |
| 953 | // We do this to support (f.e.) loads off the end of a structure where |
| 954 | // only some bits are used. |
| 955 | if (ShAmt > 0 && (unsigned)ShAmt < NTy->getBitWidth()) |
| 956 | NV = BinaryOperator::createLShr(NV, |
| 957 | ConstantInt::get(NV->getType(),ShAmt), |
| 958 | LI->getName(), LI); |
| 959 | else if (ShAmt < 0 && (unsigned)-ShAmt < NTy->getBitWidth()) |
| 960 | NV = BinaryOperator::createShl(NV, |
| 961 | ConstantInt::get(NV->getType(),-ShAmt), |
| 962 | LI->getName(), LI); |
| 963 | |
| 964 | // Finally, unconditionally truncate the integer to the right width. |
| 965 | if (LIBitWidth < NTy->getBitWidth()) |
| 966 | NV = new TruncInst(NV, IntegerType::get(LIBitWidth), |
| 967 | LI->getName(), LI); |
| 968 | |
| 969 | // If the result is an integer, this is a trunc or bitcast. |
| 970 | if (isa<IntegerType>(LI->getType())) { |
| 971 | assert(NV->getType() == LI->getType() && "Truncate wasn't enough?"); |
| 972 | } else if (LI->getType()->isFloatingPoint()) { |
Chris Lattner | 3210403 | 2007-04-11 03:27:24 +0000 | [diff] [blame] | 973 | // Just do a bitcast, we know the sizes match up. |
Chris Lattner | daa012d | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 974 | NV = new BitCastInst(NV, LI->getType(), LI->getName(), LI); |
| 975 | } else { |
| 976 | // Otherwise must be a pointer. |
| 977 | NV = new IntToPtrInst(NV, LI->getType(), LI->getName(), LI); |
Chris Lattner | 3323ce1 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 978 | } |
| 979 | } |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 980 | LI->replaceAllUsesWith(NV); |
| 981 | LI->eraseFromParent(); |
| 982 | } else if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 983 | assert(SI->getOperand(0) != Ptr && "Consistency error!"); |
| 984 | |
| 985 | // Convert the stored type to the actual type, shift it left to insert |
| 986 | // then 'or' into place. |
| 987 | Value *SV = SI->getOperand(0); |
Chris Lattner | 3323ce1 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 988 | const Type *AllocaType = NewAI->getType()->getElementType(); |
Chris Lattner | daa012d | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 989 | if (SV->getType() == AllocaType) { |
| 990 | // All is well. |
| 991 | } else if (const VectorType *PTy = dyn_cast<VectorType>(AllocaType)) { |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 992 | Value *Old = new LoadInst(NewAI, NewAI->getName()+".in", SI); |
Chris Lattner | daa012d | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 993 | |
| 994 | // If the result alloca is a vector type, this is either an element |
| 995 | // access or a bitcast to another vector type. |
| 996 | if (isa<VectorType>(SV->getType())) { |
| 997 | SV = new BitCastInst(SV, AllocaType, SV->getName(), SI); |
| 998 | } else { |
| 999 | // Must be an element insertion. |
| 1000 | unsigned Elt = Offset/(TD.getTypeSize(PTy->getElementType())*8); |
| 1001 | SV = new InsertElementInst(Old, SV, |
| 1002 | ConstantInt::get(Type::Int32Ty, Elt), |
| 1003 | "tmp", SI); |
| 1004 | } |
Chris Lattner | 5ee4d07 | 2007-04-11 15:45:25 +0000 | [diff] [blame] | 1005 | } else if (isa<PointerType>(AllocaType)) { |
| 1006 | // If the alloca type is a pointer, then all the elements must be |
| 1007 | // pointers. |
| 1008 | if (SV->getType() != AllocaType) |
| 1009 | SV = new BitCastInst(SV, AllocaType, SV->getName(), SI); |
Chris Lattner | daa012d | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 1010 | } else { |
| 1011 | Value *Old = new LoadInst(NewAI, NewAI->getName()+".in", SI); |
| 1012 | |
| 1013 | // If SV is a float, convert it to the appropriate integer type. |
| 1014 | // If it is a pointer, do the same, and also handle ptr->ptr casts |
| 1015 | // here. |
| 1016 | unsigned SrcWidth = TD.getTypeSizeInBits(SV->getType()); |
| 1017 | unsigned DestWidth = AllocaType->getPrimitiveSizeInBits(); |
| 1018 | if (SV->getType()->isFloatingPoint()) |
| 1019 | SV = new BitCastInst(SV, IntegerType::get(SrcWidth), |
| 1020 | SV->getName(), SI); |
Chris Lattner | 5ee4d07 | 2007-04-11 15:45:25 +0000 | [diff] [blame] | 1021 | else if (isa<PointerType>(SV->getType())) |
| 1022 | SV = new PtrToIntInst(SV, TD.getIntPtrType(), SV->getName(), SI); |
Chris Lattner | daa012d | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 1023 | |
| 1024 | // Always zero extend the value if needed. |
| 1025 | if (SV->getType() != AllocaType) |
| 1026 | SV = new ZExtInst(SV, AllocaType, SV->getName(), SI); |
| 1027 | |
| 1028 | // If this is a big-endian system and the store is narrower than the |
| 1029 | // full alloca type, we need to do a shift to get the right bits. |
| 1030 | int ShAmt = 0; |
| 1031 | if (TD.isBigEndian()) { |
| 1032 | ShAmt = DestWidth-SrcWidth-Offset; |
Chris Lattner | 3323ce1 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 1033 | } else { |
Chris Lattner | daa012d | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 1034 | ShAmt = Offset; |
| 1035 | } |
| 1036 | |
| 1037 | // Note: we support negative bitwidths (with shr) which are not defined. |
| 1038 | // We do this to support (f.e.) stores off the end of a structure where |
| 1039 | // only some bits in the structure are set. |
| 1040 | APInt Mask(APInt::getLowBitsSet(DestWidth, SrcWidth)); |
| 1041 | if (ShAmt > 0 && (unsigned)ShAmt < DestWidth) { |
| 1042 | SV = BinaryOperator::createShl(SV, |
| 1043 | ConstantInt::get(SV->getType(), ShAmt), |
| 1044 | SV->getName(), SI); |
| 1045 | Mask <<= ShAmt; |
| 1046 | } else if (ShAmt < 0 && (unsigned)-ShAmt < DestWidth) { |
| 1047 | SV = BinaryOperator::createLShr(SV, |
| 1048 | ConstantInt::get(SV->getType(),-ShAmt), |
| 1049 | SV->getName(), SI); |
| 1050 | Mask = Mask.lshr(ShAmt); |
| 1051 | } |
| 1052 | |
| 1053 | // Mask out the bits we are about to insert from the old value, and or |
| 1054 | // in the new bits. |
| 1055 | if (SrcWidth != DestWidth) { |
| 1056 | assert(DestWidth > SrcWidth); |
| 1057 | Old = BinaryOperator::createAnd(Old, ConstantInt::get(~Mask), |
| 1058 | Old->getName()+".mask", SI); |
| 1059 | SV = BinaryOperator::createOr(Old, SV, SV->getName()+".ins", SI); |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | new StoreInst(SV, NewAI, SI); |
| 1063 | SI->eraseFromParent(); |
| 1064 | |
Chris Lattner | daa012d | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 1065 | } else if (BitCastInst *CI = dyn_cast<BitCastInst>(User)) { |
| 1066 | ConvertUsesToScalar(CI, NewAI, Offset); |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1067 | CI->eraseFromParent(); |
| 1068 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) { |
| 1069 | const PointerType *AggPtrTy = |
| 1070 | cast<PointerType>(GEP->getOperand(0)->getType()); |
| 1071 | const TargetData &TD = getAnalysis<TargetData>(); |
| 1072 | unsigned AggSizeInBits = TD.getTypeSize(AggPtrTy->getElementType())*8; |
| 1073 | |
| 1074 | // Check to see if this is stepping over an element: GEP Ptr, int C |
| 1075 | unsigned NewOffset = Offset; |
| 1076 | if (GEP->getNumOperands() == 2) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1077 | unsigned Idx = cast<ConstantInt>(GEP->getOperand(1))->getZExtValue(); |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1078 | unsigned BitOffset = Idx*AggSizeInBits; |
| 1079 | |
Chris Lattner | daa012d | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 1080 | NewOffset += BitOffset; |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1081 | } else if (GEP->getNumOperands() == 3) { |
| 1082 | // We know that operand #2 is zero. |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1083 | unsigned Idx = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue(); |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1084 | const Type *AggTy = AggPtrTy->getElementType(); |
| 1085 | if (const SequentialType *SeqTy = dyn_cast<SequentialType>(AggTy)) { |
| 1086 | unsigned ElSizeBits = TD.getTypeSize(SeqTy->getElementType())*8; |
| 1087 | |
Chris Lattner | daa012d | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 1088 | NewOffset += ElSizeBits*Idx; |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1089 | } else if (const StructType *STy = dyn_cast<StructType>(AggTy)) { |
Chris Lattner | c473d8e | 2007-02-10 19:55:17 +0000 | [diff] [blame] | 1090 | unsigned EltBitOffset = |
| 1091 | TD.getStructLayout(STy)->getElementOffset(Idx)*8; |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | daa012d | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 1093 | NewOffset += EltBitOffset; |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1094 | } else { |
| 1095 | assert(0 && "Unsupported operation!"); |
| 1096 | abort(); |
| 1097 | } |
| 1098 | } else { |
| 1099 | assert(0 && "Unsupported operation!"); |
| 1100 | abort(); |
| 1101 | } |
| 1102 | ConvertUsesToScalar(GEP, NewAI, NewOffset); |
| 1103 | GEP->eraseFromParent(); |
| 1104 | } else { |
| 1105 | assert(0 && "Unsupported operation!"); |
| 1106 | abort(); |
| 1107 | } |
| 1108 | } |
| 1109 | } |