Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1 | //===- ScalarReplAggregates.cpp - Scalar Replacement of Aggregates --------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | ed7b41e | 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 | 38aec32 | 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 | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 22 | #define DEBUG_TYPE "scalarrepl" |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
| 25 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 26 | #include "llvm/Function.h" |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 27 | #include "llvm/GlobalVariable.h" |
Misha Brukman | d8e1eea | 2004-07-29 17:05:13 +0000 | [diff] [blame] | 28 | #include "llvm/Instructions.h" |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 29 | #include "llvm/IntrinsicInst.h" |
| 30 | #include "llvm/Pass.h" |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/Dominators.h" |
| 32 | #include "llvm/Target/TargetData.h" |
| 33 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 35 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 36 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 1ccd185 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/Statistic.h" |
| 40 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | d866473 | 2003-12-02 17:43:55 +0000 | [diff] [blame] | 41 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 43 | STATISTIC(NumReplaced, "Number of allocas broken up"); |
| 44 | STATISTIC(NumPromoted, "Number of allocas promoted"); |
| 45 | STATISTIC(NumConverted, "Number of aggregates converted to scalar"); |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 46 | STATISTIC(NumGlobals, "Number of allocas copied from constant global"); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 48 | namespace { |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 49 | struct VISIBILITY_HIDDEN SROA : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 50 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | c2bbfc1 | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 51 | explicit SROA(signed T = -1) : FunctionPass((intptr_t)&ID) { |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 52 | if (T == -1) |
Chris Lattner | b0e71ed | 2007-08-02 21:33:36 +0000 | [diff] [blame] | 53 | SRThreshold = 128; |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 54 | else |
| 55 | SRThreshold = T; |
| 56 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 57 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 58 | bool runOnFunction(Function &F); |
| 59 | |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 60 | bool performScalarRepl(Function &F); |
| 61 | bool performPromotion(Function &F); |
| 62 | |
Chris Lattner | a15854c | 2003-08-31 00:45:13 +0000 | [diff] [blame] | 63 | // getAnalysisUsage - This pass does not require any passes, but we know it |
| 64 | // will not alter the CFG, so say so. |
| 65 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Devang Patel | 326821e | 2007-06-07 21:57:03 +0000 | [diff] [blame] | 66 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 67 | AU.addRequired<DominanceFrontier>(); |
| 68 | AU.addRequired<TargetData>(); |
Chris Lattner | a15854c | 2003-08-31 00:45:13 +0000 | [diff] [blame] | 69 | AU.setPreservesCFG(); |
| 70 | } |
| 71 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 72 | private: |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 73 | /// AllocaInfo - When analyzing uses of an alloca instruction, this captures |
| 74 | /// information about the uses. All these fields are initialized to false |
| 75 | /// and set to true when something is learned. |
| 76 | struct AllocaInfo { |
| 77 | /// isUnsafe - This is set to true if the alloca cannot be SROA'd. |
| 78 | bool isUnsafe : 1; |
| 79 | |
| 80 | /// needsCanon - This is set to true if there is some use of the alloca |
| 81 | /// that requires canonicalization. |
| 82 | bool needsCanon : 1; |
| 83 | |
| 84 | /// isMemCpySrc - This is true if this aggregate is memcpy'd from. |
| 85 | bool isMemCpySrc : 1; |
| 86 | |
Zhou Sheng | 33b0b8d | 2007-07-06 06:01:16 +0000 | [diff] [blame] | 87 | /// isMemCpyDst - This is true if this aggregate is memcpy'd into. |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 88 | bool isMemCpyDst : 1; |
| 89 | |
| 90 | AllocaInfo() |
| 91 | : isUnsafe(false), needsCanon(false), |
| 92 | isMemCpySrc(false), isMemCpyDst(false) {} |
| 93 | }; |
| 94 | |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 95 | unsigned SRThreshold; |
| 96 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 97 | void MarkUnsafe(AllocaInfo &I) { I.isUnsafe = true; } |
| 98 | |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 99 | int isSafeAllocaToScalarRepl(AllocationInst *AI); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 100 | |
| 101 | void isSafeUseOfAllocation(Instruction *User, AllocationInst *AI, |
| 102 | AllocaInfo &Info); |
| 103 | void isSafeElementUse(Value *Ptr, bool isFirstElt, AllocationInst *AI, |
| 104 | AllocaInfo &Info); |
| 105 | void isSafeMemIntrinsicOnAllocation(MemIntrinsic *MI, AllocationInst *AI, |
| 106 | unsigned OpNo, AllocaInfo &Info); |
| 107 | void isSafeUseOfBitCastedAllocation(BitCastInst *User, AllocationInst *AI, |
| 108 | AllocaInfo &Info); |
| 109 | |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 110 | void DoScalarReplacement(AllocationInst *AI, |
| 111 | std::vector<AllocationInst*> &WorkList); |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 112 | void CanonicalizeAllocaUsers(AllocationInst *AI); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 113 | AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocationInst *Base); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 115 | void RewriteBitCastUserOfAlloca(Instruction *BCInst, AllocationInst *AI, |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 116 | SmallVector<AllocaInst*, 32> &NewElts); |
| 117 | |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 118 | const Type *CanConvertToScalar(Value *V, bool &IsNotTrivial); |
| 119 | void ConvertToScalar(AllocationInst *AI, const Type *Ty); |
| 120 | void ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, unsigned Offset); |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 121 | Value *ConvertUsesOfLoadToScalar(LoadInst *LI, AllocaInst *NewAI, |
| 122 | unsigned Offset); |
| 123 | Value *ConvertUsesOfStoreToScalar(StoreInst *SI, AllocaInst *NewAI, |
| 124 | unsigned Offset); |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 125 | static Instruction *isOnlyCopiedFromConstantGlobal(AllocationInst *AI); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 128 | char SROA::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 129 | RegisterPass<SROA> X("scalarrepl", "Scalar Replacement of Aggregates"); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 132 | // Public interface to the ScalarReplAggregates pass |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 133 | FunctionPass *llvm::createScalarReplAggregatesPass(signed int Threshold) { |
| 134 | return new SROA(Threshold); |
| 135 | } |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 136 | |
| 137 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 138 | bool SROA::runOnFunction(Function &F) { |
Chris Lattner | fe7ea0d | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 139 | bool Changed = performPromotion(F); |
| 140 | while (1) { |
| 141 | bool LocalChange = performScalarRepl(F); |
| 142 | if (!LocalChange) break; // No need to repromote if no scalarrepl |
| 143 | Changed = true; |
| 144 | LocalChange = performPromotion(F); |
| 145 | if (!LocalChange) break; // No need to re-scalarrepl if no promotion |
| 146 | } |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 147 | |
| 148 | return Changed; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | bool SROA::performPromotion(Function &F) { |
| 153 | std::vector<AllocaInst*> Allocas; |
Devang Patel | 326821e | 2007-06-07 21:57:03 +0000 | [diff] [blame] | 154 | DominatorTree &DT = getAnalysis<DominatorTree>(); |
Chris Lattner | 43f820d | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 155 | DominanceFrontier &DF = getAnalysis<DominanceFrontier>(); |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 02a3be0 | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 157 | BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 158 | |
Chris Lattner | fe7ea0d | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 159 | bool Changed = false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 161 | while (1) { |
| 162 | Allocas.clear(); |
| 163 | |
| 164 | // Find allocas that are safe to promote, by looking at all instructions in |
| 165 | // the entry node |
| 166 | for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) |
| 167 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? |
Devang Patel | 41968df | 2007-04-25 17:15:20 +0000 | [diff] [blame] | 168 | if (isAllocaPromotable(AI)) |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 169 | Allocas.push_back(AI); |
| 170 | |
| 171 | if (Allocas.empty()) break; |
| 172 | |
Devang Patel | 326821e | 2007-06-07 21:57:03 +0000 | [diff] [blame] | 173 | PromoteMemToReg(Allocas, DT, DF); |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 174 | NumPromoted += Allocas.size(); |
| 175 | Changed = true; |
| 176 | } |
| 177 | |
| 178 | return Changed; |
| 179 | } |
| 180 | |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 181 | // performScalarRepl - This algorithm is a simple worklist driven algorithm, |
| 182 | // which runs on all of the malloc/alloca instructions in the function, removing |
| 183 | // them if they are only used by getelementptr instructions. |
| 184 | // |
| 185 | bool SROA::performScalarRepl(Function &F) { |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 186 | std::vector<AllocationInst*> WorkList; |
| 187 | |
| 188 | // Scan the entry basic block, adding any alloca's and mallocs to the worklist |
Chris Lattner | 02a3be0 | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 189 | BasicBlock &BB = F.getEntryBlock(); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 190 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) |
| 191 | if (AllocationInst *A = dyn_cast<AllocationInst>(I)) |
| 192 | WorkList.push_back(A); |
| 193 | |
Chris Lattner | 7139406 | 2007-05-24 18:43:04 +0000 | [diff] [blame] | 194 | const TargetData &TD = getAnalysis<TargetData>(); |
| 195 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 196 | // Process the worklist |
| 197 | bool Changed = false; |
| 198 | while (!WorkList.empty()) { |
| 199 | AllocationInst *AI = WorkList.back(); |
| 200 | WorkList.pop_back(); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 201 | |
Chris Lattner | add2bd7 | 2006-12-22 23:14:42 +0000 | [diff] [blame] | 202 | // Handle dead allocas trivially. These can be formed by SROA'ing arrays |
| 203 | // with unused elements. |
| 204 | if (AI->use_empty()) { |
| 205 | AI->eraseFromParent(); |
| 206 | continue; |
| 207 | } |
| 208 | |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 209 | // If we can turn this aggregate value (potentially with casts) into a |
| 210 | // simple scalar value that can be mem2reg'd into a register value. |
| 211 | bool IsNotTrivial = false; |
| 212 | if (const Type *ActualType = CanConvertToScalar(AI, IsNotTrivial)) |
Chris Lattner | df4f226 | 2006-04-20 20:48:50 +0000 | [diff] [blame] | 213 | if (IsNotTrivial && ActualType != Type::VoidTy) { |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 214 | ConvertToScalar(AI, ActualType); |
| 215 | Changed = true; |
| 216 | continue; |
| 217 | } |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 219 | // Check to see if we can perform the core SROA transformation. We cannot |
| 220 | // transform the allocation instruction if it is an array allocation |
| 221 | // (allocations OF arrays are ok though), and an allocation of a scalar |
| 222 | // value cannot be decomposed at all. |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 223 | if (!AI->isArrayAllocation() && |
| 224 | (isa<StructType>(AI->getAllocatedType()) || |
Chris Lattner | 7139406 | 2007-05-24 18:43:04 +0000 | [diff] [blame] | 225 | isa<ArrayType>(AI->getAllocatedType())) && |
| 226 | AI->getAllocatedType()->isSized() && |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 227 | TD.getABITypeSize(AI->getAllocatedType()) < SRThreshold) { |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 228 | // Check that all of the users of the allocation are capable of being |
| 229 | // transformed. |
| 230 | switch (isSafeAllocaToScalarRepl(AI)) { |
| 231 | default: assert(0 && "Unexpected value!"); |
| 232 | case 0: // Not safe to scalar replace. |
| 233 | break; |
| 234 | case 1: // Safe, but requires cleanup/canonicalizations first |
| 235 | CanonicalizeAllocaUsers(AI); |
| 236 | // FALL THROUGH. |
| 237 | case 3: // Safe to scalar replace. |
| 238 | DoScalarReplacement(AI, WorkList); |
| 239 | Changed = true; |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 240 | continue; |
| 241 | } |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 242 | } |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 243 | |
| 244 | // Check to see if this allocation is only modified by a memcpy/memmove from |
| 245 | // a constant global. If this is the case, we can change all users to use |
| 246 | // the constant global instead. This is commonly produced by the CFE by |
| 247 | // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A' |
| 248 | // is only subsequently read. |
| 249 | if (Instruction *TheCopy = isOnlyCopiedFromConstantGlobal(AI)) { |
| 250 | DOUT << "Found alloca equal to global: " << *AI; |
| 251 | DOUT << " memcpy = " << *TheCopy; |
| 252 | Constant *TheSrc = cast<Constant>(TheCopy->getOperand(2)); |
| 253 | AI->replaceAllUsesWith(ConstantExpr::getBitCast(TheSrc, AI->getType())); |
| 254 | TheCopy->eraseFromParent(); // Don't mutate the global. |
| 255 | AI->eraseFromParent(); |
| 256 | ++NumGlobals; |
| 257 | Changed = true; |
| 258 | continue; |
| 259 | } |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 260 | |
| 261 | // Otherwise, couldn't process this. |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | return Changed; |
| 265 | } |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 266 | |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 267 | /// DoScalarReplacement - This alloca satisfied the isSafeAllocaToScalarRepl |
| 268 | /// predicate, do SROA now. |
| 269 | void SROA::DoScalarReplacement(AllocationInst *AI, |
| 270 | std::vector<AllocationInst*> &WorkList) { |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 271 | DOUT << "Found inst to SROA: " << *AI; |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 272 | SmallVector<AllocaInst*, 32> ElementAllocas; |
| 273 | if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) { |
| 274 | ElementAllocas.reserve(ST->getNumContainedTypes()); |
| 275 | for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) { |
| 276 | AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0, |
| 277 | AI->getAlignment(), |
| 278 | AI->getName() + "." + utostr(i), AI); |
| 279 | ElementAllocas.push_back(NA); |
| 280 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 281 | } |
| 282 | } else { |
| 283 | const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType()); |
| 284 | ElementAllocas.reserve(AT->getNumElements()); |
| 285 | const Type *ElTy = AT->getElementType(); |
| 286 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
| 287 | AllocaInst *NA = new AllocaInst(ElTy, 0, AI->getAlignment(), |
| 288 | AI->getName() + "." + utostr(i), AI); |
| 289 | ElementAllocas.push_back(NA); |
| 290 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // Now that we have created the alloca instructions that we want to use, |
| 295 | // expand the getelementptr instructions to use them. |
| 296 | // |
| 297 | while (!AI->use_empty()) { |
| 298 | Instruction *User = cast<Instruction>(AI->use_back()); |
| 299 | if (BitCastInst *BCInst = dyn_cast<BitCastInst>(User)) { |
| 300 | RewriteBitCastUserOfAlloca(BCInst, AI, ElementAllocas); |
| 301 | BCInst->eraseFromParent(); |
| 302 | continue; |
| 303 | } |
| 304 | |
| 305 | GetElementPtrInst *GEPI = cast<GetElementPtrInst>(User); |
| 306 | // We now know that the GEP is of the form: GEP <ptr>, 0, <cst> |
| 307 | unsigned Idx = |
| 308 | (unsigned)cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue(); |
| 309 | |
| 310 | assert(Idx < ElementAllocas.size() && "Index out of range?"); |
| 311 | AllocaInst *AllocaToUse = ElementAllocas[Idx]; |
| 312 | |
| 313 | Value *RepValue; |
| 314 | if (GEPI->getNumOperands() == 3) { |
| 315 | // Do not insert a new getelementptr instruction with zero indices, only |
| 316 | // to have it optimized out later. |
| 317 | RepValue = AllocaToUse; |
| 318 | } else { |
| 319 | // We are indexing deeply into the structure, so we still need a |
| 320 | // getelement ptr instruction to finish the indexing. This may be |
| 321 | // expanded itself once the worklist is rerun. |
| 322 | // |
| 323 | SmallVector<Value*, 8> NewArgs; |
| 324 | NewArgs.push_back(Constant::getNullValue(Type::Int32Ty)); |
| 325 | NewArgs.append(GEPI->op_begin()+3, GEPI->op_end()); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 326 | RepValue = GetElementPtrInst::Create(AllocaToUse, NewArgs.begin(), |
| 327 | NewArgs.end(), "", GEPI); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 328 | RepValue->takeName(GEPI); |
| 329 | } |
| 330 | |
| 331 | // If this GEP is to the start of the aggregate, check for memcpys. |
| 332 | if (Idx == 0) { |
| 333 | bool IsStartOfAggregateGEP = true; |
| 334 | for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i) { |
| 335 | if (!isa<ConstantInt>(GEPI->getOperand(i))) { |
| 336 | IsStartOfAggregateGEP = false; |
| 337 | break; |
| 338 | } |
| 339 | if (!cast<ConstantInt>(GEPI->getOperand(i))->isZero()) { |
| 340 | IsStartOfAggregateGEP = false; |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (IsStartOfAggregateGEP) |
| 346 | RewriteBitCastUserOfAlloca(GEPI, AI, ElementAllocas); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | // Move all of the users over to the new GEP. |
| 351 | GEPI->replaceAllUsesWith(RepValue); |
| 352 | // Delete the old GEP |
| 353 | GEPI->eraseFromParent(); |
| 354 | } |
| 355 | |
| 356 | // Finally, delete the Alloca instruction |
| 357 | AI->eraseFromParent(); |
| 358 | NumReplaced++; |
| 359 | } |
| 360 | |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 361 | |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 362 | /// isSafeElementUse - Check to see if this use is an allowed use for a |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 363 | /// getelementptr instruction of an array aggregate allocation. isFirstElt |
| 364 | /// indicates whether Ptr is known to the start of the aggregate. |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 365 | /// |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 366 | void SROA::isSafeElementUse(Value *Ptr, bool isFirstElt, AllocationInst *AI, |
| 367 | AllocaInfo &Info) { |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 368 | for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end(); |
| 369 | I != E; ++I) { |
| 370 | Instruction *User = cast<Instruction>(*I); |
| 371 | switch (User->getOpcode()) { |
| 372 | case Instruction::Load: break; |
| 373 | case Instruction::Store: |
| 374 | // Store is ok if storing INTO the pointer, not storing the pointer |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 375 | if (User->getOperand(0) == Ptr) return MarkUnsafe(Info); |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 376 | break; |
| 377 | case Instruction::GetElementPtr: { |
| 378 | GetElementPtrInst *GEP = cast<GetElementPtrInst>(User); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 379 | bool AreAllZeroIndices = isFirstElt; |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 380 | if (GEP->getNumOperands() > 1) { |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 381 | if (!isa<ConstantInt>(GEP->getOperand(1)) || |
| 382 | !cast<ConstantInt>(GEP->getOperand(1))->isZero()) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 383 | // Using pointer arithmetic to navigate the array. |
| 384 | return MarkUnsafe(Info); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 385 | |
| 386 | if (AreAllZeroIndices) { |
| 387 | for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) { |
| 388 | if (!isa<ConstantInt>(GEP->getOperand(i)) || |
| 389 | !cast<ConstantInt>(GEP->getOperand(i))->isZero()) { |
| 390 | AreAllZeroIndices = false; |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | } |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 395 | } |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 396 | isSafeElementUse(GEP, AreAllZeroIndices, AI, Info); |
| 397 | if (Info.isUnsafe) return; |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 398 | break; |
| 399 | } |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 400 | case Instruction::BitCast: |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 401 | if (isFirstElt) { |
| 402 | isSafeUseOfBitCastedAllocation(cast<BitCastInst>(User), AI, Info); |
| 403 | if (Info.isUnsafe) return; |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 404 | break; |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 405 | } |
| 406 | DOUT << " Transformation preventing inst: " << *User; |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 407 | return MarkUnsafe(Info); |
| 408 | case Instruction::Call: |
| 409 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) { |
| 410 | if (isFirstElt) { |
| 411 | isSafeMemIntrinsicOnAllocation(MI, AI, I.getOperandNo(), Info); |
| 412 | if (Info.isUnsafe) return; |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | DOUT << " Transformation preventing inst: " << *User; |
| 417 | return MarkUnsafe(Info); |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 418 | default: |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 419 | DOUT << " Transformation preventing inst: " << *User; |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 420 | return MarkUnsafe(Info); |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 423 | return; // All users look ok :) |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 426 | /// AllUsersAreLoads - Return true if all users of this value are loads. |
| 427 | static bool AllUsersAreLoads(Value *Ptr) { |
| 428 | for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end(); |
| 429 | I != E; ++I) |
| 430 | if (cast<Instruction>(*I)->getOpcode() != Instruction::Load) |
| 431 | return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 432 | return true; |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 435 | /// isSafeUseOfAllocation - Check to see if this user is an allowed use for an |
| 436 | /// aggregate allocation. |
| 437 | /// |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 438 | void SROA::isSafeUseOfAllocation(Instruction *User, AllocationInst *AI, |
| 439 | AllocaInfo &Info) { |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 440 | if (BitCastInst *C = dyn_cast<BitCastInst>(User)) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 441 | return isSafeUseOfBitCastedAllocation(C, AI, Info); |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 443 | GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User); |
| 444 | if (GEPI == 0) |
| 445 | return MarkUnsafe(Info); |
| 446 | |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 447 | gep_type_iterator I = gep_type_begin(GEPI), E = gep_type_end(GEPI); |
| 448 | |
Chris Lattner | 25de486 | 2006-03-08 01:05:29 +0000 | [diff] [blame] | 449 | // The GEP is not safe to transform if not of the form "GEP <ptr>, 0, <cst>". |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 450 | if (I == E || |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 451 | I.getOperand() != Constant::getNullValue(I.getOperand()->getType())) { |
| 452 | return MarkUnsafe(Info); |
| 453 | } |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 454 | |
| 455 | ++I; |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 456 | if (I == E) return MarkUnsafe(Info); // ran out of GEP indices?? |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 457 | |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 458 | bool IsAllZeroIndices = true; |
| 459 | |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 460 | // If this is a use of an array allocation, do a bit more checking for sanity. |
| 461 | if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) { |
| 462 | uint64_t NumElements = AT->getNumElements(); |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 463 | |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 464 | if (ConstantInt *Idx = dyn_cast<ConstantInt>(I.getOperand())) { |
| 465 | IsAllZeroIndices &= Idx->isZero(); |
| 466 | |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 467 | // Check to make sure that index falls within the array. If not, |
| 468 | // something funny is going on, so we won't do the optimization. |
| 469 | // |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 470 | if (Idx->getZExtValue() >= NumElements) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 471 | return MarkUnsafe(Info); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 472 | |
Chris Lattner | 25de486 | 2006-03-08 01:05:29 +0000 | [diff] [blame] | 473 | // We cannot scalar repl this level of the array unless any array |
| 474 | // sub-indices are in-range constants. In particular, consider: |
| 475 | // A[0][i]. We cannot know that the user isn't doing invalid things like |
| 476 | // allowing i to index an out-of-range subscript that accesses A[1]. |
| 477 | // |
| 478 | // Scalar replacing *just* the outer index of the array is probably not |
| 479 | // going to be a win anyway, so just give up. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 480 | for (++I; I != E && (isa<ArrayType>(*I) || isa<VectorType>(*I)); ++I) { |
Chris Lattner | d925150 | 2006-11-07 22:42:47 +0000 | [diff] [blame] | 481 | uint64_t NumElements; |
| 482 | if (const ArrayType *SubArrayTy = dyn_cast<ArrayType>(*I)) |
| 483 | NumElements = SubArrayTy->getNumElements(); |
| 484 | else |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 485 | NumElements = cast<VectorType>(*I)->getNumElements(); |
Chris Lattner | d925150 | 2006-11-07 22:42:47 +0000 | [diff] [blame] | 486 | |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 487 | ConstantInt *IdxVal = dyn_cast<ConstantInt>(I.getOperand()); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 488 | if (!IdxVal) return MarkUnsafe(Info); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 489 | if (IdxVal->getZExtValue() >= NumElements) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 490 | return MarkUnsafe(Info); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 491 | IsAllZeroIndices &= IdxVal->isZero(); |
Chris Lattner | 25de486 | 2006-03-08 01:05:29 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 494 | } else { |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 495 | IsAllZeroIndices = 0; |
| 496 | |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 497 | // If this is an array index and the index is not constant, we cannot |
| 498 | // promote... that is unless the array has exactly one or two elements in |
| 499 | // it, in which case we CAN promote it, but we have to canonicalize this |
| 500 | // out if this is the only problem. |
Chris Lattner | 25de486 | 2006-03-08 01:05:29 +0000 | [diff] [blame] | 501 | if ((NumElements == 1 || NumElements == 2) && |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 502 | AllUsersAreLoads(GEPI)) { |
| 503 | Info.needsCanon = true; |
| 504 | return; // Canonicalization required! |
| 505 | } |
| 506 | return MarkUnsafe(Info); |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 507 | } |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 508 | } |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 509 | |
| 510 | // If there are any non-simple uses of this getelementptr, make sure to reject |
| 511 | // them. |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 512 | return isSafeElementUse(GEPI, IsAllZeroIndices, AI, Info); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /// isSafeMemIntrinsicOnAllocation - Return true if the specified memory |
| 516 | /// intrinsic can be promoted by SROA. At this point, we know that the operand |
| 517 | /// of the memintrinsic is a pointer to the beginning of the allocation. |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 518 | void SROA::isSafeMemIntrinsicOnAllocation(MemIntrinsic *MI, AllocationInst *AI, |
| 519 | unsigned OpNo, AllocaInfo &Info) { |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 520 | // If not constant length, give up. |
| 521 | ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength()); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 522 | if (!Length) return MarkUnsafe(Info); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 523 | |
| 524 | // If not the whole aggregate, give up. |
| 525 | const TargetData &TD = getAnalysis<TargetData>(); |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 526 | if (Length->getZExtValue() != |
| 527 | TD.getABITypeSize(AI->getType()->getElementType())) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 528 | return MarkUnsafe(Info); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 529 | |
| 530 | // We only know about memcpy/memset/memmove. |
| 531 | if (!isa<MemCpyInst>(MI) && !isa<MemSetInst>(MI) && !isa<MemMoveInst>(MI)) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 532 | return MarkUnsafe(Info); |
| 533 | |
| 534 | // Otherwise, we can transform it. Determine whether this is a memcpy/set |
| 535 | // into or out of the aggregate. |
| 536 | if (OpNo == 1) |
| 537 | Info.isMemCpyDst = true; |
| 538 | else { |
| 539 | assert(OpNo == 2); |
| 540 | Info.isMemCpySrc = true; |
| 541 | } |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 544 | /// isSafeUseOfBitCastedAllocation - Return true if all users of this bitcast |
| 545 | /// are |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 546 | void SROA::isSafeUseOfBitCastedAllocation(BitCastInst *BC, AllocationInst *AI, |
| 547 | AllocaInfo &Info) { |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 548 | for (Value::use_iterator UI = BC->use_begin(), E = BC->use_end(); |
| 549 | UI != E; ++UI) { |
| 550 | if (BitCastInst *BCU = dyn_cast<BitCastInst>(UI)) { |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 551 | isSafeUseOfBitCastedAllocation(BCU, AI, Info); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 552 | } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(UI)) { |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 553 | isSafeMemIntrinsicOnAllocation(MI, AI, UI.getOperandNo(), Info); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 554 | } else { |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 555 | return MarkUnsafe(Info); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 556 | } |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 557 | if (Info.isUnsafe) return; |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 558 | } |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 561 | /// RewriteBitCastUserOfAlloca - BCInst (transitively) bitcasts AI, or indexes |
| 562 | /// to its first element. Transform users of the cast to use the new values |
| 563 | /// instead. |
| 564 | void SROA::RewriteBitCastUserOfAlloca(Instruction *BCInst, AllocationInst *AI, |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 565 | SmallVector<AllocaInst*, 32> &NewElts) { |
| 566 | Constant *Zero = Constant::getNullValue(Type::Int32Ty); |
| 567 | const TargetData &TD = getAnalysis<TargetData>(); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 568 | |
| 569 | Value::use_iterator UI = BCInst->use_begin(), UE = BCInst->use_end(); |
| 570 | while (UI != UE) { |
| 571 | if (BitCastInst *BCU = dyn_cast<BitCastInst>(*UI)) { |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 572 | RewriteBitCastUserOfAlloca(BCU, AI, NewElts); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 573 | ++UI; |
| 574 | BCU->eraseFromParent(); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 575 | continue; |
| 576 | } |
| 577 | |
| 578 | // Otherwise, must be memcpy/memmove/memset of the entire aggregate. Split |
| 579 | // into one per element. |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 580 | MemIntrinsic *MI = dyn_cast<MemIntrinsic>(*UI); |
| 581 | |
| 582 | // If it's not a mem intrinsic, it must be some other user of a gep of the |
| 583 | // first pointer. Just leave these alone. |
| 584 | if (!MI) { |
| 585 | ++UI; |
| 586 | continue; |
| 587 | } |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 588 | |
| 589 | // If this is a memcpy/memmove, construct the other pointer as the |
| 590 | // appropriate type. |
| 591 | Value *OtherPtr = 0; |
| 592 | if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(MI)) { |
| 593 | if (BCInst == MCI->getRawDest()) |
| 594 | OtherPtr = MCI->getRawSource(); |
| 595 | else { |
| 596 | assert(BCInst == MCI->getRawSource()); |
| 597 | OtherPtr = MCI->getRawDest(); |
| 598 | } |
| 599 | } else if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
| 600 | if (BCInst == MMI->getRawDest()) |
| 601 | OtherPtr = MMI->getRawSource(); |
| 602 | else { |
| 603 | assert(BCInst == MMI->getRawSource()); |
| 604 | OtherPtr = MMI->getRawDest(); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | // If there is an other pointer, we want to convert it to the same pointer |
| 609 | // type as AI has, so we can GEP through it. |
| 610 | if (OtherPtr) { |
| 611 | // It is likely that OtherPtr is a bitcast, if so, remove it. |
| 612 | if (BitCastInst *BC = dyn_cast<BitCastInst>(OtherPtr)) |
| 613 | OtherPtr = BC->getOperand(0); |
| 614 | if (ConstantExpr *BCE = dyn_cast<ConstantExpr>(OtherPtr)) |
| 615 | if (BCE->getOpcode() == Instruction::BitCast) |
| 616 | OtherPtr = BCE->getOperand(0); |
| 617 | |
| 618 | // If the pointer is not the right type, insert a bitcast to the right |
| 619 | // type. |
| 620 | if (OtherPtr->getType() != AI->getType()) |
| 621 | OtherPtr = new BitCastInst(OtherPtr, AI->getType(), OtherPtr->getName(), |
| 622 | MI); |
| 623 | } |
| 624 | |
| 625 | // Process each element of the aggregate. |
| 626 | Value *TheFn = MI->getOperand(0); |
| 627 | const Type *BytePtrTy = MI->getRawDest()->getType(); |
| 628 | bool SROADest = MI->getRawDest() == BCInst; |
| 629 | |
| 630 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 631 | // If this is a memcpy/memmove, emit a GEP of the other element address. |
| 632 | Value *OtherElt = 0; |
| 633 | if (OtherPtr) { |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 634 | Value *Idx[2]; |
| 635 | Idx[0] = Zero; |
| 636 | Idx[1] = ConstantInt::get(Type::Int32Ty, i); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 637 | OtherElt = GetElementPtrInst::Create(OtherPtr, Idx, Idx + 2, |
| 638 | OtherPtr->getNameStr()+"."+utostr(i), |
| 639 | MI); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | Value *EltPtr = NewElts[i]; |
Chris Lattner | c14d3ca | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 643 | const Type *EltTy =cast<PointerType>(EltPtr->getType())->getElementType(); |
| 644 | |
| 645 | // If we got down to a scalar, insert a load or store as appropriate. |
| 646 | if (EltTy->isFirstClassType()) { |
| 647 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
| 648 | Value *Elt = new LoadInst(SROADest ? OtherElt : EltPtr, "tmp", |
| 649 | MI); |
| 650 | new StoreInst(Elt, SROADest ? EltPtr : OtherElt, MI); |
| 651 | continue; |
| 652 | } else { |
| 653 | assert(isa<MemSetInst>(MI)); |
| 654 | |
| 655 | // If the stored element is zero (common case), just store a null |
| 656 | // constant. |
| 657 | Constant *StoreVal; |
| 658 | if (ConstantInt *CI = dyn_cast<ConstantInt>(MI->getOperand(2))) { |
| 659 | if (CI->isZero()) { |
| 660 | StoreVal = Constant::getNullValue(EltTy); // 0.0, null, 0, <0,0> |
| 661 | } else { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 662 | // If EltTy is a vector type, get the element type. |
Chris Lattner | c14d3ca | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 663 | const Type *ValTy = EltTy; |
| 664 | if (const VectorType *VTy = dyn_cast<VectorType>(ValTy)) |
| 665 | ValTy = VTy->getElementType(); |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 666 | |
Chris Lattner | c14d3ca | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 667 | // Construct an integer with the right value. |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 668 | unsigned EltSize = TD.getTypeSizeInBits(ValTy); |
| 669 | APInt OneVal(EltSize, CI->getZExtValue()); |
Chris Lattner | c14d3ca | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 670 | APInt TotalVal(OneVal); |
| 671 | // Set each byte. |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 672 | for (unsigned i = 0; 8*i < EltSize; ++i) { |
Chris Lattner | c14d3ca | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 673 | TotalVal = TotalVal.shl(8); |
| 674 | TotalVal |= OneVal; |
| 675 | } |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 676 | |
Chris Lattner | c14d3ca | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 677 | // Convert the integer value to the appropriate type. |
| 678 | StoreVal = ConstantInt::get(TotalVal); |
| 679 | if (isa<PointerType>(ValTy)) |
| 680 | StoreVal = ConstantExpr::getIntToPtr(StoreVal, ValTy); |
| 681 | else if (ValTy->isFloatingPoint()) |
| 682 | StoreVal = ConstantExpr::getBitCast(StoreVal, ValTy); |
| 683 | assert(StoreVal->getType() == ValTy && "Type mismatch!"); |
| 684 | |
| 685 | // If the requested value was a vector constant, create it. |
| 686 | if (EltTy != ValTy) { |
| 687 | unsigned NumElts = cast<VectorType>(ValTy)->getNumElements(); |
| 688 | SmallVector<Constant*, 16> Elts(NumElts, StoreVal); |
| 689 | StoreVal = ConstantVector::get(&Elts[0], NumElts); |
| 690 | } |
| 691 | } |
| 692 | new StoreInst(StoreVal, EltPtr, MI); |
| 693 | continue; |
| 694 | } |
| 695 | // Otherwise, if we're storing a byte variable, use a memset call for |
| 696 | // this element. |
| 697 | } |
| 698 | } |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 699 | |
| 700 | // Cast the element pointer to BytePtrTy. |
| 701 | if (EltPtr->getType() != BytePtrTy) |
| 702 | EltPtr = new BitCastInst(EltPtr, BytePtrTy, EltPtr->getNameStr(), MI); |
Chris Lattner | c14d3ca | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 703 | |
| 704 | // Cast the other pointer (if we have one) to BytePtrTy. |
| 705 | if (OtherElt && OtherElt->getType() != BytePtrTy) |
| 706 | OtherElt = new BitCastInst(OtherElt, BytePtrTy,OtherElt->getNameStr(), |
| 707 | MI); |
| 708 | |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 709 | unsigned EltSize = TD.getABITypeSize(EltTy); |
Chris Lattner | c14d3ca | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 710 | |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 711 | // Finally, insert the meminst for this element. |
| 712 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { |
| 713 | Value *Ops[] = { |
| 714 | SROADest ? EltPtr : OtherElt, // Dest ptr |
| 715 | SROADest ? OtherElt : EltPtr, // Src ptr |
| 716 | ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size |
| 717 | Zero // Align |
| 718 | }; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 719 | CallInst::Create(TheFn, Ops, Ops + 4, "", MI); |
Chris Lattner | c14d3ca | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 720 | } else { |
| 721 | assert(isa<MemSetInst>(MI)); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 722 | Value *Ops[] = { |
| 723 | EltPtr, MI->getOperand(2), // Dest, Value, |
| 724 | ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size |
| 725 | Zero // Align |
| 726 | }; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 727 | CallInst::Create(TheFn, Ops, Ops + 4, "", MI); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 728 | } |
Chris Lattner | c14d3ca | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 729 | } |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 730 | |
| 731 | // Finally, MI is now dead, as we've modified its actions to occur on all of |
| 732 | // the elements of the aggregate. |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 733 | ++UI; |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 734 | MI->eraseFromParent(); |
| 735 | } |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 738 | /// HasPadding - Return true if the specified type has any structure or |
| 739 | /// alignment padding, false otherwise. |
Duncan Sands | 18b0ca8 | 2007-11-05 00:35:07 +0000 | [diff] [blame] | 740 | static bool HasPadding(const Type *Ty, const TargetData &TD, |
| 741 | bool inPacked = false) { |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 742 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 743 | const StructLayout *SL = TD.getStructLayout(STy); |
| 744 | unsigned PrevFieldBitOffset = 0; |
| 745 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 746 | unsigned FieldBitOffset = SL->getElementOffsetInBits(i); |
| 747 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 748 | // Padding in sub-elements? |
Duncan Sands | 18b0ca8 | 2007-11-05 00:35:07 +0000 | [diff] [blame] | 749 | if (HasPadding(STy->getElementType(i), TD, STy->isPacked())) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 750 | return true; |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 751 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 752 | // Check to see if there is any padding between this element and the |
| 753 | // previous one. |
| 754 | if (i) { |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 755 | unsigned PrevFieldEnd = |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 756 | PrevFieldBitOffset+TD.getTypeSizeInBits(STy->getElementType(i-1)); |
| 757 | if (PrevFieldEnd < FieldBitOffset) |
| 758 | return true; |
| 759 | } |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 760 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 761 | PrevFieldBitOffset = FieldBitOffset; |
| 762 | } |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 763 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 764 | // Check for tail padding. |
| 765 | if (unsigned EltCount = STy->getNumElements()) { |
| 766 | unsigned PrevFieldEnd = PrevFieldBitOffset + |
| 767 | TD.getTypeSizeInBits(STy->getElementType(EltCount-1)); |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 768 | if (PrevFieldEnd < SL->getSizeInBits()) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 769 | return true; |
| 770 | } |
| 771 | |
| 772 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
Duncan Sands | 18b0ca8 | 2007-11-05 00:35:07 +0000 | [diff] [blame] | 773 | return HasPadding(ATy->getElementType(), TD, false); |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 774 | } else if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) { |
Duncan Sands | 18b0ca8 | 2007-11-05 00:35:07 +0000 | [diff] [blame] | 775 | return HasPadding(VTy->getElementType(), TD, false); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 776 | } |
Duncan Sands | 18b0ca8 | 2007-11-05 00:35:07 +0000 | [diff] [blame] | 777 | return inPacked ? |
| 778 | false : TD.getTypeSizeInBits(Ty) != TD.getABITypeSizeInBits(Ty); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 779 | } |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 780 | |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 781 | /// isSafeStructAllocaToScalarRepl - Check to see if the specified allocation of |
| 782 | /// an aggregate can be broken down into elements. Return 0 if not, 3 if safe, |
| 783 | /// or 1 if safe after canonicalization has been performed. |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 784 | /// |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 785 | int SROA::isSafeAllocaToScalarRepl(AllocationInst *AI) { |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 786 | // Loop over the use list of the alloca. We can only transform it if all of |
| 787 | // the users are safe to transform. |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 788 | AllocaInfo Info; |
| 789 | |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 790 | for (Value::use_iterator I = AI->use_begin(), E = AI->use_end(); |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 791 | I != E; ++I) { |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 792 | isSafeUseOfAllocation(cast<Instruction>(*I), AI, Info); |
| 793 | if (Info.isUnsafe) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 794 | DOUT << "Cannot transform: " << *AI << " due to user: " << **I; |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 795 | return 0; |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 796 | } |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 797 | } |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 798 | |
| 799 | // Okay, we know all the users are promotable. If the aggregate is a memcpy |
| 800 | // source and destination, we have to be careful. In particular, the memcpy |
| 801 | // could be moving around elements that live in structure padding of the LLVM |
| 802 | // types, but may actually be used. In these cases, we refuse to promote the |
| 803 | // struct. |
| 804 | if (Info.isMemCpySrc && Info.isMemCpyDst && |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 805 | HasPadding(AI->getType()->getElementType(), getAnalysis<TargetData>())) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 806 | return 0; |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 807 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 808 | // If we require cleanup, return 1, otherwise return 3. |
| 809 | return Info.needsCanon ? 1 : 3; |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | /// CanonicalizeAllocaUsers - If SROA reported that it can promote the specified |
| 813 | /// allocation, but only if cleaned up, perform the cleanups required. |
| 814 | void SROA::CanonicalizeAllocaUsers(AllocationInst *AI) { |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 815 | // At this point, we know that the end result will be SROA'd and promoted, so |
| 816 | // we can insert ugly code if required so long as sroa+mem2reg will clean it |
| 817 | // up. |
| 818 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 819 | UI != E; ) { |
Chris Lattner | a9d1a84 | 2007-03-19 18:25:57 +0000 | [diff] [blame] | 820 | GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI++); |
| 821 | if (!GEPI) continue; |
Reid Spencer | 96326f9 | 2004-11-15 17:29:41 +0000 | [diff] [blame] | 822 | gep_type_iterator I = gep_type_begin(GEPI); |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 823 | ++I; |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 824 | |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 825 | if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) { |
| 826 | uint64_t NumElements = AT->getNumElements(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 827 | |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 828 | if (!isa<ConstantInt>(I.getOperand())) { |
| 829 | if (NumElements == 1) { |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 830 | GEPI->setOperand(2, Constant::getNullValue(Type::Int32Ty)); |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 831 | } else { |
| 832 | assert(NumElements == 2 && "Unhandled case!"); |
| 833 | // All users of the GEP must be loads. At each use of the GEP, insert |
| 834 | // two loads of the appropriate indexed GEP and select between them. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 835 | Value *IsOne = new ICmpInst(ICmpInst::ICMP_NE, I.getOperand(), |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 836 | Constant::getNullValue(I.getOperand()->getType()), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 837 | "isone", GEPI); |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 838 | // Insert the new GEP instructions, which are properly indexed. |
Chris Lattner | 1ccd185 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 839 | SmallVector<Value*, 8> Indices(GEPI->op_begin()+1, GEPI->op_end()); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 840 | Indices[1] = Constant::getNullValue(Type::Int32Ty); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 841 | Value *ZeroIdx = GetElementPtrInst::Create(GEPI->getOperand(0), |
| 842 | Indices.begin(), |
| 843 | Indices.end(), |
| 844 | GEPI->getName()+".0", GEPI); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 845 | Indices[1] = ConstantInt::get(Type::Int32Ty, 1); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 846 | Value *OneIdx = GetElementPtrInst::Create(GEPI->getOperand(0), |
| 847 | Indices.begin(), |
| 848 | Indices.end(), |
| 849 | GEPI->getName()+".1", GEPI); |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 850 | // Replace all loads of the variable index GEP with loads from both |
| 851 | // indexes and a select. |
| 852 | while (!GEPI->use_empty()) { |
| 853 | LoadInst *LI = cast<LoadInst>(GEPI->use_back()); |
| 854 | Value *Zero = new LoadInst(ZeroIdx, LI->getName()+".0", LI); |
| 855 | Value *One = new LoadInst(OneIdx , LI->getName()+".1", LI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 856 | Value *R = SelectInst::Create(IsOne, One, Zero, LI->getName(), LI); |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 857 | LI->replaceAllUsesWith(R); |
| 858 | LI->eraseFromParent(); |
| 859 | } |
| 860 | GEPI->eraseFromParent(); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | } |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 865 | } |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 866 | |
| 867 | /// MergeInType - Add the 'In' type to the accumulated type so far. If the |
| 868 | /// types are incompatible, return true, otherwise update Accum and return |
| 869 | /// false. |
Chris Lattner | de6df88 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 870 | /// |
Chris Lattner | d22dbdf | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 871 | /// There are three cases we handle here: |
| 872 | /// 1) An effectively-integer union, where the pieces are stored into as |
Chris Lattner | de6df88 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 873 | /// smaller integers (common with byte swap and other idioms). |
Chris Lattner | d22dbdf | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 874 | /// 2) A union of vector types of the same size and potentially its elements. |
| 875 | /// Here we turn element accesses into insert/extract element operations. |
| 876 | /// 3) A union of scalar types, such as int/float or int/pointer. Here we |
| 877 | /// merge together into integers, allowing the xform to work with #1 as |
| 878 | /// well. |
Chris Lattner | 5b121cc | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 879 | static bool MergeInType(const Type *In, const Type *&Accum, |
| 880 | const TargetData &TD) { |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 881 | // If this is our first type, just use it. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 882 | const VectorType *PTy; |
Chris Lattner | de6df88 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 883 | if (Accum == Type::VoidTy || In == Accum) { |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 884 | Accum = In; |
Chris Lattner | d22dbdf | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 885 | } else if (In == Type::VoidTy) { |
| 886 | // Noop. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 887 | } else if (In->isInteger() && Accum->isInteger()) { // integer union. |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 888 | // Otherwise pick whichever type is larger. |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 889 | if (cast<IntegerType>(In)->getBitWidth() > |
| 890 | cast<IntegerType>(Accum)->getBitWidth()) |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 891 | Accum = In; |
Chris Lattner | 5b121cc | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 892 | } else if (isa<PointerType>(In) && isa<PointerType>(Accum)) { |
Chris Lattner | c836333 | 2006-10-08 23:53:04 +0000 | [diff] [blame] | 893 | // Pointer unions just stay as one of the pointers. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 894 | } else if (isa<VectorType>(In) || isa<VectorType>(Accum)) { |
| 895 | if ((PTy = dyn_cast<VectorType>(Accum)) && |
Chris Lattner | d22dbdf | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 896 | PTy->getElementType() == In) { |
| 897 | // Accum is a vector, and we are accessing an element: ok. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 898 | } else if ((PTy = dyn_cast<VectorType>(In)) && |
Chris Lattner | d22dbdf | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 899 | PTy->getElementType() == Accum) { |
| 900 | // In is a vector, and accum is an element: ok, remember In. |
| 901 | Accum = In; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 902 | } else if ((PTy = dyn_cast<VectorType>(In)) && isa<VectorType>(Accum) && |
| 903 | PTy->getBitWidth() == cast<VectorType>(Accum)->getBitWidth()) { |
Chris Lattner | d22dbdf | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 904 | // Two vectors of the same size: keep Accum. |
| 905 | } else { |
| 906 | // Cannot insert an short into a <4 x int> or handle |
| 907 | // <2 x int> -> <4 x int> |
| 908 | return true; |
| 909 | } |
Chris Lattner | 21c362d | 2006-12-13 02:26:45 +0000 | [diff] [blame] | 910 | } else { |
Chris Lattner | d22dbdf | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 911 | // Pointer/FP/Integer unions merge together as integers. |
| 912 | switch (Accum->getTypeID()) { |
| 913 | case Type::PointerTyID: Accum = TD.getIntPtrType(); break; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 914 | case Type::FloatTyID: Accum = Type::Int32Ty; break; |
| 915 | case Type::DoubleTyID: Accum = Type::Int64Ty; break; |
Dale Johannesen | ef0ab93 | 2007-09-28 00:21:38 +0000 | [diff] [blame] | 916 | case Type::X86_FP80TyID: return true; |
| 917 | case Type::FP128TyID: return true; |
| 918 | case Type::PPC_FP128TyID: return true; |
Chris Lattner | d22dbdf | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 919 | default: |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 920 | assert(Accum->isInteger() && "Unknown FP type!"); |
Chris Lattner | d22dbdf | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 921 | break; |
| 922 | } |
| 923 | |
| 924 | switch (In->getTypeID()) { |
| 925 | case Type::PointerTyID: In = TD.getIntPtrType(); break; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 926 | case Type::FloatTyID: In = Type::Int32Ty; break; |
| 927 | case Type::DoubleTyID: In = Type::Int64Ty; break; |
Dale Johannesen | ef0ab93 | 2007-09-28 00:21:38 +0000 | [diff] [blame] | 928 | case Type::X86_FP80TyID: return true; |
| 929 | case Type::FP128TyID: return true; |
| 930 | case Type::PPC_FP128TyID: return true; |
Chris Lattner | d22dbdf | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 931 | default: |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 932 | assert(In->isInteger() && "Unknown FP type!"); |
Chris Lattner | d22dbdf | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 933 | break; |
| 934 | } |
| 935 | return MergeInType(In, Accum, TD); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 936 | } |
| 937 | return false; |
| 938 | } |
| 939 | |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 940 | /// getUIntAtLeastAsBigAs - Return an unsigned integer type that is at least |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 941 | /// as big as the specified type. If there is no suitable type, this returns |
| 942 | /// null. |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 943 | const Type *getUIntAtLeastAsBigAs(unsigned NumBits) { |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 944 | if (NumBits > 64) return 0; |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 945 | if (NumBits > 32) return Type::Int64Ty; |
| 946 | if (NumBits > 16) return Type::Int32Ty; |
| 947 | if (NumBits > 8) return Type::Int16Ty; |
| 948 | return Type::Int8Ty; |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | /// CanConvertToScalar - V is a pointer. If we can convert the pointee to a |
| 952 | /// single scalar integer type, return that type. Further, if the use is not |
| 953 | /// a completely trivial use that mem2reg could promote, set IsNotTrivial. If |
| 954 | /// there are no uses of this pointer, return Type::VoidTy to differentiate from |
| 955 | /// failure. |
| 956 | /// |
| 957 | const Type *SROA::CanConvertToScalar(Value *V, bool &IsNotTrivial) { |
| 958 | const Type *UsedType = Type::VoidTy; // No uses, no forced type. |
| 959 | const TargetData &TD = getAnalysis<TargetData>(); |
| 960 | const PointerType *PTy = cast<PointerType>(V->getType()); |
| 961 | |
| 962 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) { |
| 963 | Instruction *User = cast<Instruction>(*UI); |
| 964 | |
| 965 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
Chris Lattner | 5b121cc | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 966 | if (MergeInType(LI->getType(), UsedType, TD)) |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 967 | return 0; |
| 968 | |
| 969 | } else if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 970 | // Storing the pointer, not into the value? |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 971 | if (SI->getOperand(0) == V) return 0; |
| 972 | |
Chris Lattner | de6df88 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 973 | // NOTE: We could handle storing of FP imms into integers here! |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 974 | |
Chris Lattner | 5b121cc | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 975 | if (MergeInType(SI->getOperand(0)->getType(), UsedType, TD)) |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 976 | return 0; |
Chris Lattner | d22dbdf | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 977 | } else if (BitCastInst *CI = dyn_cast<BitCastInst>(User)) { |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 978 | IsNotTrivial = true; |
| 979 | const Type *SubTy = CanConvertToScalar(CI, IsNotTrivial); |
Chris Lattner | 5b121cc | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 980 | if (!SubTy || MergeInType(SubTy, UsedType, TD)) return 0; |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 981 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) { |
| 982 | // Check to see if this is stepping over an element: GEP Ptr, int C |
| 983 | if (GEP->getNumOperands() == 2 && isa<ConstantInt>(GEP->getOperand(1))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 984 | unsigned Idx = cast<ConstantInt>(GEP->getOperand(1))->getZExtValue(); |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 985 | unsigned ElSize = TD.getABITypeSize(PTy->getElementType()); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 986 | unsigned BitOffset = Idx*ElSize*8; |
| 987 | if (BitOffset > 64 || !isPowerOf2_32(ElSize)) return 0; |
| 988 | |
| 989 | IsNotTrivial = true; |
| 990 | const Type *SubElt = CanConvertToScalar(GEP, IsNotTrivial); |
| 991 | if (SubElt == 0) return 0; |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 992 | if (SubElt != Type::VoidTy && SubElt->isInteger()) { |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 993 | const Type *NewTy = |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 994 | getUIntAtLeastAsBigAs(TD.getABITypeSizeInBits(SubElt)+BitOffset); |
Chris Lattner | 5b121cc | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 995 | if (NewTy == 0 || MergeInType(NewTy, UsedType, TD)) return 0; |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 996 | continue; |
| 997 | } |
| 998 | } else if (GEP->getNumOperands() == 3 && |
| 999 | isa<ConstantInt>(GEP->getOperand(1)) && |
| 1000 | isa<ConstantInt>(GEP->getOperand(2)) && |
Zhou Sheng | 843f0767 | 2007-04-19 05:39:12 +0000 | [diff] [blame] | 1001 | cast<ConstantInt>(GEP->getOperand(1))->isZero()) { |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1002 | // We are stepping into an element, e.g. a structure or an array: |
| 1003 | // GEP Ptr, int 0, uint C |
| 1004 | const Type *AggTy = PTy->getElementType(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1005 | unsigned Idx = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue(); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1006 | |
| 1007 | if (const ArrayType *ATy = dyn_cast<ArrayType>(AggTy)) { |
| 1008 | if (Idx >= ATy->getNumElements()) return 0; // Out of range. |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1009 | } else if (const VectorType *VectorTy = dyn_cast<VectorType>(AggTy)) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1010 | // Getting an element of the vector. |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1011 | if (Idx >= VectorTy->getNumElements()) return 0; // Out of range. |
Chris Lattner | de6df88 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 1012 | |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1013 | // Merge in the vector type. |
| 1014 | if (MergeInType(VectorTy, UsedType, TD)) return 0; |
Chris Lattner | de6df88 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 1015 | |
| 1016 | const Type *SubTy = CanConvertToScalar(GEP, IsNotTrivial); |
| 1017 | if (SubTy == 0) return 0; |
| 1018 | |
Chris Lattner | 5b121cc | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 1019 | if (SubTy != Type::VoidTy && MergeInType(SubTy, UsedType, TD)) |
Chris Lattner | de6df88 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 1020 | return 0; |
| 1021 | |
| 1022 | // We'll need to change this to an insert/extract element operation. |
| 1023 | IsNotTrivial = true; |
| 1024 | continue; // Everything looks ok |
| 1025 | |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1026 | } else if (isa<StructType>(AggTy)) { |
| 1027 | // Structs are always ok. |
| 1028 | } else { |
| 1029 | return 0; |
| 1030 | } |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1031 | const Type *NTy = getUIntAtLeastAsBigAs(TD.getABITypeSizeInBits(AggTy)); |
Chris Lattner | 5b121cc | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 1032 | if (NTy == 0 || MergeInType(NTy, UsedType, TD)) return 0; |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1033 | const Type *SubTy = CanConvertToScalar(GEP, IsNotTrivial); |
| 1034 | if (SubTy == 0) return 0; |
Chris Lattner | 5b121cc | 2006-10-08 23:28:04 +0000 | [diff] [blame] | 1035 | if (SubTy != Type::VoidTy && MergeInType(SubTy, UsedType, TD)) |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1036 | return 0; |
| 1037 | continue; // Everything looks ok |
| 1038 | } |
| 1039 | return 0; |
| 1040 | } else { |
| 1041 | // Cannot handle this! |
| 1042 | return 0; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | return UsedType; |
| 1047 | } |
| 1048 | |
| 1049 | /// ConvertToScalar - The specified alloca passes the CanConvertToScalar |
| 1050 | /// predicate and is non-trivial. Convert it to something that can be trivially |
| 1051 | /// promoted into a register by mem2reg. |
| 1052 | void SROA::ConvertToScalar(AllocationInst *AI, const Type *ActualTy) { |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 1053 | DOUT << "CONVERT TO SCALAR: " << *AI << " TYPE = " |
| 1054 | << *ActualTy << "\n"; |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1055 | ++NumConverted; |
| 1056 | |
| 1057 | BasicBlock *EntryBlock = AI->getParent(); |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 1058 | assert(EntryBlock == &EntryBlock->getParent()->getEntryBlock() && |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1059 | "Not in the entry block!"); |
| 1060 | EntryBlock->getInstList().remove(AI); // Take the alloca out of the program. |
| 1061 | |
| 1062 | // Create and insert the alloca. |
Chris Lattner | de6df88 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 1063 | AllocaInst *NewAI = new AllocaInst(ActualTy, 0, AI->getName(), |
| 1064 | EntryBlock->begin()); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1065 | ConvertUsesToScalar(AI, NewAI, 0); |
| 1066 | delete AI; |
| 1067 | } |
| 1068 | |
| 1069 | |
| 1070 | /// ConvertUsesToScalar - Convert all of the users of Ptr to use the new alloca |
Chris Lattner | de6df88 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 1071 | /// directly. This happens when we are converting an "integer union" to a |
| 1072 | /// single integer scalar, or when we are converting a "vector union" to a |
| 1073 | /// vector with insert/extractelement instructions. |
| 1074 | /// |
| 1075 | /// Offset is an offset from the original alloca, in bits that need to be |
| 1076 | /// shifted to the right. By the end of this, there should be no uses of Ptr. |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1077 | void SROA::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, unsigned Offset) { |
| 1078 | while (!Ptr->use_empty()) { |
| 1079 | Instruction *User = cast<Instruction>(Ptr->use_back()); |
| 1080 | |
| 1081 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1082 | Value *NV = ConvertUsesOfLoadToScalar(LI, NewAI, Offset); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1083 | LI->replaceAllUsesWith(NV); |
| 1084 | LI->eraseFromParent(); |
| 1085 | } else if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 1086 | assert(SI->getOperand(0) != Ptr && "Consistency error!"); |
| 1087 | |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1088 | Value *SV = ConvertUsesOfStoreToScalar(SI, NewAI, Offset); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1089 | new StoreInst(SV, NewAI, SI); |
| 1090 | SI->eraseFromParent(); |
| 1091 | |
Chris Lattner | f4b1818 | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 1092 | } else if (BitCastInst *CI = dyn_cast<BitCastInst>(User)) { |
Chris Lattner | b10e0da | 2008-01-30 00:39:15 +0000 | [diff] [blame] | 1093 | ConvertUsesToScalar(CI, NewAI, Offset); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1094 | CI->eraseFromParent(); |
| 1095 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) { |
| 1096 | const PointerType *AggPtrTy = |
| 1097 | cast<PointerType>(GEP->getOperand(0)->getType()); |
| 1098 | const TargetData &TD = getAnalysis<TargetData>(); |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1099 | unsigned AggSizeInBits = |
| 1100 | TD.getABITypeSizeInBits(AggPtrTy->getElementType()); |
| 1101 | |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1102 | // Check to see if this is stepping over an element: GEP Ptr, int C |
| 1103 | unsigned NewOffset = Offset; |
| 1104 | if (GEP->getNumOperands() == 2) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1105 | unsigned Idx = cast<ConstantInt>(GEP->getOperand(1))->getZExtValue(); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1106 | unsigned BitOffset = Idx*AggSizeInBits; |
| 1107 | |
Chris Lattner | f4b1818 | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 1108 | NewOffset += BitOffset; |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1109 | } else if (GEP->getNumOperands() == 3) { |
| 1110 | // We know that operand #2 is zero. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1111 | unsigned Idx = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue(); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1112 | const Type *AggTy = AggPtrTy->getElementType(); |
| 1113 | if (const SequentialType *SeqTy = dyn_cast<SequentialType>(AggTy)) { |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1114 | unsigned ElSizeBits = |
| 1115 | TD.getABITypeSizeInBits(SeqTy->getElementType()); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1116 | |
Chris Lattner | f4b1818 | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 1117 | NewOffset += ElSizeBits*Idx; |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1118 | } else if (const StructType *STy = dyn_cast<StructType>(AggTy)) { |
Chris Lattner | b1919e2 | 2007-02-10 19:55:17 +0000 | [diff] [blame] | 1119 | unsigned EltBitOffset = |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1120 | TD.getStructLayout(STy)->getElementOffsetInBits(Idx); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1121 | |
Chris Lattner | f4b1818 | 2007-04-11 00:57:54 +0000 | [diff] [blame] | 1122 | NewOffset += EltBitOffset; |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1123 | } else { |
| 1124 | assert(0 && "Unsupported operation!"); |
| 1125 | abort(); |
| 1126 | } |
| 1127 | } else { |
| 1128 | assert(0 && "Unsupported operation!"); |
| 1129 | abort(); |
| 1130 | } |
| 1131 | ConvertUsesToScalar(GEP, NewAI, NewOffset); |
| 1132 | GEP->eraseFromParent(); |
| 1133 | } else { |
| 1134 | assert(0 && "Unsupported operation!"); |
| 1135 | abort(); |
| 1136 | } |
| 1137 | } |
| 1138 | } |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1139 | |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1140 | /// ConvertUsesOfLoadToScalar - Convert all of the users the specified load to |
| 1141 | /// use the new alloca directly, returning the value that should replace the |
| 1142 | /// load. This happens when we are converting an "integer union" to a |
| 1143 | /// single integer scalar, or when we are converting a "vector union" to a |
| 1144 | /// vector with insert/extractelement instructions. |
| 1145 | /// |
| 1146 | /// Offset is an offset from the original alloca, in bits that need to be |
| 1147 | /// shifted to the right. By the end of this, there should be no uses of Ptr. |
| 1148 | Value *SROA::ConvertUsesOfLoadToScalar(LoadInst *LI, AllocaInst *NewAI, |
| 1149 | unsigned Offset) { |
| 1150 | // The load is a bit extract from NewAI shifted right by Offset bits. |
| 1151 | Value *NV = new LoadInst(NewAI, LI->getName(), LI); |
| 1152 | |
| 1153 | if (NV->getType() == LI->getType() && Offset == 0) { |
| 1154 | // We win, no conversion needed. |
| 1155 | return NV; |
| 1156 | } |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1157 | |
| 1158 | // If the result type of the 'union' is a pointer, then this must be ptr->ptr |
| 1159 | // cast. Anything else would result in NV being an integer. |
| 1160 | if (isa<PointerType>(NV->getType())) { |
| 1161 | assert(isa<PointerType>(LI->getType())); |
| 1162 | return new BitCastInst(NV, LI->getType(), LI->getName(), LI); |
| 1163 | } |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1164 | |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1165 | if (const VectorType *VTy = dyn_cast<VectorType>(NV->getType())) { |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1166 | // If the result alloca is a vector type, this is either an element |
| 1167 | // access or a bitcast to another vector type. |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1168 | if (isa<VectorType>(LI->getType())) |
| 1169 | return new BitCastInst(NV, LI->getType(), LI->getName(), LI); |
| 1170 | |
| 1171 | // Otherwise it must be an element access. |
| 1172 | const TargetData &TD = getAnalysis<TargetData>(); |
| 1173 | unsigned Elt = 0; |
| 1174 | if (Offset) { |
| 1175 | unsigned EltSize = TD.getABITypeSizeInBits(VTy->getElementType()); |
| 1176 | Elt = Offset/EltSize; |
| 1177 | Offset -= EltSize*Elt; |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1178 | } |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1179 | NV = new ExtractElementInst(NV, ConstantInt::get(Type::Int32Ty, Elt), |
| 1180 | "tmp", LI); |
| 1181 | |
| 1182 | // If we're done, return this element. |
| 1183 | if (NV->getType() == LI->getType() && Offset == 0) |
| 1184 | return NV; |
| 1185 | } |
| 1186 | |
| 1187 | const IntegerType *NTy = cast<IntegerType>(NV->getType()); |
| 1188 | |
| 1189 | // If this is a big-endian system and the load is narrower than the |
| 1190 | // full alloca type, we need to do a shift to get the right bits. |
| 1191 | int ShAmt = 0; |
| 1192 | const TargetData &TD = getAnalysis<TargetData>(); |
| 1193 | if (TD.isBigEndian()) { |
| 1194 | // On big-endian machines, the lowest bit is stored at the bit offset |
| 1195 | // from the pointer given by getTypeStoreSizeInBits. This matters for |
| 1196 | // integers with a bitwidth that is not a multiple of 8. |
| 1197 | ShAmt = TD.getTypeStoreSizeInBits(NTy) - |
| 1198 | TD.getTypeStoreSizeInBits(LI->getType()) - Offset; |
| 1199 | } else { |
| 1200 | ShAmt = Offset; |
| 1201 | } |
| 1202 | |
| 1203 | // Note: we support negative bitwidths (with shl) which are not defined. |
| 1204 | // We do this to support (f.e.) loads off the end of a structure where |
| 1205 | // only some bits are used. |
| 1206 | if (ShAmt > 0 && (unsigned)ShAmt < NTy->getBitWidth()) |
| 1207 | NV = BinaryOperator::createLShr(NV, |
| 1208 | ConstantInt::get(NV->getType(),ShAmt), |
| 1209 | LI->getName(), LI); |
| 1210 | else if (ShAmt < 0 && (unsigned)-ShAmt < NTy->getBitWidth()) |
| 1211 | NV = BinaryOperator::createShl(NV, |
| 1212 | ConstantInt::get(NV->getType(),-ShAmt), |
| 1213 | LI->getName(), LI); |
| 1214 | |
| 1215 | // Finally, unconditionally truncate the integer to the right width. |
| 1216 | unsigned LIBitWidth = TD.getTypeSizeInBits(LI->getType()); |
| 1217 | if (LIBitWidth < NTy->getBitWidth()) |
| 1218 | NV = new TruncInst(NV, IntegerType::get(LIBitWidth), |
| 1219 | LI->getName(), LI); |
| 1220 | |
| 1221 | // If the result is an integer, this is a trunc or bitcast. |
| 1222 | if (isa<IntegerType>(LI->getType())) { |
| 1223 | // Should be done. |
| 1224 | } else if (LI->getType()->isFloatingPoint()) { |
| 1225 | // Just do a bitcast, we know the sizes match up. |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1226 | NV = new BitCastInst(NV, LI->getType(), LI->getName(), LI); |
| 1227 | } else { |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1228 | // Otherwise must be a pointer. |
| 1229 | NV = new IntToPtrInst(NV, LI->getType(), LI->getName(), LI); |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1230 | } |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1231 | assert(NV->getType() == LI->getType() && "Didn't convert right?"); |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1232 | return NV; |
| 1233 | } |
| 1234 | |
| 1235 | |
| 1236 | /// ConvertUsesOfStoreToScalar - Convert the specified store to a load+store |
| 1237 | /// pair of the new alloca directly, returning the value that should be stored |
| 1238 | /// to the alloca. This happens when we are converting an "integer union" to a |
| 1239 | /// single integer scalar, or when we are converting a "vector union" to a |
| 1240 | /// vector with insert/extractelement instructions. |
| 1241 | /// |
| 1242 | /// Offset is an offset from the original alloca, in bits that need to be |
| 1243 | /// shifted to the right. By the end of this, there should be no uses of Ptr. |
| 1244 | Value *SROA::ConvertUsesOfStoreToScalar(StoreInst *SI, AllocaInst *NewAI, |
| 1245 | unsigned Offset) { |
| 1246 | |
| 1247 | // Convert the stored type to the actual type, shift it left to insert |
| 1248 | // then 'or' into place. |
| 1249 | Value *SV = SI->getOperand(0); |
| 1250 | const Type *AllocaType = NewAI->getType()->getElementType(); |
| 1251 | if (SV->getType() == AllocaType && Offset == 0) { |
| 1252 | // All is well. |
| 1253 | } else if (const VectorType *PTy = dyn_cast<VectorType>(AllocaType)) { |
| 1254 | Value *Old = new LoadInst(NewAI, NewAI->getName()+".in", SI); |
| 1255 | |
| 1256 | // If the result alloca is a vector type, this is either an element |
| 1257 | // access or a bitcast to another vector type. |
| 1258 | if (isa<VectorType>(SV->getType())) { |
| 1259 | SV = new BitCastInst(SV, AllocaType, SV->getName(), SI); |
| 1260 | } else { |
| 1261 | // Must be an element insertion. |
| 1262 | const TargetData &TD = getAnalysis<TargetData>(); |
| 1263 | unsigned Elt = Offset/TD.getABITypeSizeInBits(PTy->getElementType()); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 1264 | SV = InsertElementInst::Create(Old, SV, |
| 1265 | ConstantInt::get(Type::Int32Ty, Elt), |
| 1266 | "tmp", SI); |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } else if (isa<PointerType>(AllocaType)) { |
| 1269 | // If the alloca type is a pointer, then all the elements must be |
| 1270 | // pointers. |
| 1271 | if (SV->getType() != AllocaType) |
| 1272 | SV = new BitCastInst(SV, AllocaType, SV->getName(), SI); |
| 1273 | } else { |
| 1274 | Value *Old = new LoadInst(NewAI, NewAI->getName()+".in", SI); |
| 1275 | |
| 1276 | // If SV is a float, convert it to the appropriate integer type. |
| 1277 | // If it is a pointer, do the same, and also handle ptr->ptr casts |
| 1278 | // here. |
| 1279 | const TargetData &TD = getAnalysis<TargetData>(); |
| 1280 | unsigned SrcWidth = TD.getTypeSizeInBits(SV->getType()); |
| 1281 | unsigned DestWidth = TD.getTypeSizeInBits(AllocaType); |
| 1282 | unsigned SrcStoreWidth = TD.getTypeStoreSizeInBits(SV->getType()); |
| 1283 | unsigned DestStoreWidth = TD.getTypeStoreSizeInBits(AllocaType); |
| 1284 | if (SV->getType()->isFloatingPoint()) |
| 1285 | SV = new BitCastInst(SV, IntegerType::get(SrcWidth), |
| 1286 | SV->getName(), SI); |
| 1287 | else if (isa<PointerType>(SV->getType())) |
| 1288 | SV = new PtrToIntInst(SV, TD.getIntPtrType(), SV->getName(), SI); |
| 1289 | |
| 1290 | // Always zero extend the value if needed. |
| 1291 | if (SV->getType() != AllocaType) |
| 1292 | SV = new ZExtInst(SV, AllocaType, SV->getName(), SI); |
| 1293 | |
| 1294 | // If this is a big-endian system and the store is narrower than the |
| 1295 | // full alloca type, we need to do a shift to get the right bits. |
| 1296 | int ShAmt = 0; |
| 1297 | if (TD.isBigEndian()) { |
| 1298 | // On big-endian machines, the lowest bit is stored at the bit offset |
| 1299 | // from the pointer given by getTypeStoreSizeInBits. This matters for |
| 1300 | // integers with a bitwidth that is not a multiple of 8. |
| 1301 | ShAmt = DestStoreWidth - SrcStoreWidth - Offset; |
| 1302 | } else { |
| 1303 | ShAmt = Offset; |
| 1304 | } |
| 1305 | |
| 1306 | // Note: we support negative bitwidths (with shr) which are not defined. |
| 1307 | // We do this to support (f.e.) stores off the end of a structure where |
| 1308 | // only some bits in the structure are set. |
| 1309 | APInt Mask(APInt::getLowBitsSet(DestWidth, SrcWidth)); |
| 1310 | if (ShAmt > 0 && (unsigned)ShAmt < DestWidth) { |
| 1311 | SV = BinaryOperator::createShl(SV, |
| 1312 | ConstantInt::get(SV->getType(), ShAmt), |
| 1313 | SV->getName(), SI); |
| 1314 | Mask <<= ShAmt; |
| 1315 | } else if (ShAmt < 0 && (unsigned)-ShAmt < DestWidth) { |
| 1316 | SV = BinaryOperator::createLShr(SV, |
| 1317 | ConstantInt::get(SV->getType(),-ShAmt), |
| 1318 | SV->getName(), SI); |
| 1319 | Mask = Mask.lshr(ShAmt); |
| 1320 | } |
| 1321 | |
| 1322 | // Mask out the bits we are about to insert from the old value, and or |
| 1323 | // in the new bits. |
| 1324 | if (SrcWidth != DestWidth) { |
| 1325 | assert(DestWidth > SrcWidth); |
| 1326 | Old = BinaryOperator::createAnd(Old, ConstantInt::get(~Mask), |
| 1327 | Old->getName()+".mask", SI); |
| 1328 | SV = BinaryOperator::createOr(Old, SV, SV->getName()+".ins", SI); |
| 1329 | } |
| 1330 | } |
| 1331 | return SV; |
| 1332 | } |
| 1333 | |
| 1334 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1335 | |
| 1336 | /// PointsToConstantGlobal - Return true if V (possibly indirectly) points to |
| 1337 | /// some part of a constant global variable. This intentionally only accepts |
| 1338 | /// constant expressions because we don't can't rewrite arbitrary instructions. |
| 1339 | static bool PointsToConstantGlobal(Value *V) { |
| 1340 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 1341 | return GV->isConstant(); |
| 1342 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 1343 | if (CE->getOpcode() == Instruction::BitCast || |
| 1344 | CE->getOpcode() == Instruction::GetElementPtr) |
| 1345 | return PointsToConstantGlobal(CE->getOperand(0)); |
| 1346 | return false; |
| 1347 | } |
| 1348 | |
| 1349 | /// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived) |
| 1350 | /// pointer to an alloca. Ignore any reads of the pointer, return false if we |
| 1351 | /// see any stores or other unknown uses. If we see pointer arithmetic, keep |
| 1352 | /// track of whether it moves the pointer (with isOffset) but otherwise traverse |
| 1353 | /// the uses. If we see a memcpy/memmove that targets an unoffseted pointer to |
| 1354 | /// the alloca, and if the source pointer is a pointer to a constant global, we |
| 1355 | /// can optimize this. |
| 1356 | static bool isOnlyCopiedFromConstantGlobal(Value *V, Instruction *&TheCopy, |
| 1357 | bool isOffset) { |
| 1358 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) { |
| 1359 | if (isa<LoadInst>(*UI)) { |
| 1360 | // Ignore loads, they are always ok. |
| 1361 | continue; |
| 1362 | } |
| 1363 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI)) { |
| 1364 | // If uses of the bitcast are ok, we are ok. |
| 1365 | if (!isOnlyCopiedFromConstantGlobal(BCI, TheCopy, isOffset)) |
| 1366 | return false; |
| 1367 | continue; |
| 1368 | } |
| 1369 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) { |
| 1370 | // If the GEP has all zero indices, it doesn't offset the pointer. If it |
| 1371 | // doesn't, it does. |
| 1372 | if (!isOnlyCopiedFromConstantGlobal(GEP, TheCopy, |
| 1373 | isOffset || !GEP->hasAllZeroIndices())) |
| 1374 | return false; |
| 1375 | continue; |
| 1376 | } |
| 1377 | |
| 1378 | // If this is isn't our memcpy/memmove, reject it as something we can't |
| 1379 | // handle. |
| 1380 | if (!isa<MemCpyInst>(*UI) && !isa<MemMoveInst>(*UI)) |
| 1381 | return false; |
| 1382 | |
| 1383 | // If we already have seen a copy, reject the second one. |
| 1384 | if (TheCopy) return false; |
| 1385 | |
| 1386 | // If the pointer has been offset from the start of the alloca, we can't |
| 1387 | // safely handle this. |
| 1388 | if (isOffset) return false; |
| 1389 | |
| 1390 | // If the memintrinsic isn't using the alloca as the dest, reject it. |
| 1391 | if (UI.getOperandNo() != 1) return false; |
| 1392 | |
| 1393 | MemIntrinsic *MI = cast<MemIntrinsic>(*UI); |
| 1394 | |
| 1395 | // If the source of the memcpy/move is not a constant global, reject it. |
| 1396 | if (!PointsToConstantGlobal(MI->getOperand(2))) |
| 1397 | return false; |
| 1398 | |
| 1399 | // Otherwise, the transform is safe. Remember the copy instruction. |
| 1400 | TheCopy = MI; |
| 1401 | } |
| 1402 | return true; |
| 1403 | } |
| 1404 | |
| 1405 | /// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only |
| 1406 | /// modified by a copy from a constant global. If we can prove this, we can |
| 1407 | /// replace any uses of the alloca with uses of the global directly. |
| 1408 | Instruction *SROA::isOnlyCopiedFromConstantGlobal(AllocationInst *AI) { |
| 1409 | Instruction *TheCopy = 0; |
| 1410 | if (::isOnlyCopiedFromConstantGlobal(AI, TheCopy, false)) |
| 1411 | return TheCopy; |
| 1412 | return 0; |
| 1413 | } |