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" |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 30 | #include "llvm/LLVMContext.h" |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/Dominators.h" |
| 33 | #include "llvm/Target/TargetData.h" |
| 34 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 37 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 38 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 65a6502 | 2009-02-03 19:41:50 +0000 | [diff] [blame] | 39 | #include "llvm/Support/IRBuilder.h" |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 1ccd185 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | d866473 | 2003-12-02 17:43:55 +0000 | [diff] [blame] | 44 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 46 | STATISTIC(NumReplaced, "Number of allocas broken up"); |
| 47 | STATISTIC(NumPromoted, "Number of allocas promoted"); |
| 48 | STATISTIC(NumConverted, "Number of aggregates converted to scalar"); |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 49 | STATISTIC(NumGlobals, "Number of allocas copied from constant global"); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 51 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 52 | struct SROA : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 53 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 54 | explicit SROA(signed T = -1) : FunctionPass(&ID) { |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 55 | if (T == -1) |
Chris Lattner | b0e71ed | 2007-08-02 21:33:36 +0000 | [diff] [blame] | 56 | SRThreshold = 128; |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 57 | else |
| 58 | SRThreshold = T; |
| 59 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 60 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 61 | bool runOnFunction(Function &F); |
| 62 | |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 63 | bool performScalarRepl(Function &F); |
| 64 | bool performPromotion(Function &F); |
| 65 | |
Chris Lattner | a15854c | 2003-08-31 00:45:13 +0000 | [diff] [blame] | 66 | // getAnalysisUsage - This pass does not require any passes, but we know it |
| 67 | // will not alter the CFG, so say so. |
| 68 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Devang Patel | 326821e | 2007-06-07 21:57:03 +0000 | [diff] [blame] | 69 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 70 | AU.addRequired<DominanceFrontier>(); |
Chris Lattner | a15854c | 2003-08-31 00:45:13 +0000 | [diff] [blame] | 71 | AU.setPreservesCFG(); |
| 72 | } |
| 73 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 74 | private: |
Chris Lattner | 56c3852 | 2009-01-07 06:34:28 +0000 | [diff] [blame] | 75 | TargetData *TD; |
| 76 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 77 | /// AllocaInfo - When analyzing uses of an alloca instruction, this captures |
| 78 | /// information about the uses. All these fields are initialized to false |
| 79 | /// and set to true when something is learned. |
| 80 | struct AllocaInfo { |
| 81 | /// isUnsafe - This is set to true if the alloca cannot be SROA'd. |
| 82 | bool isUnsafe : 1; |
| 83 | |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 84 | /// needsCleanup - This is set to true if there is some use of the alloca |
| 85 | /// that requires cleanup. |
| 86 | bool needsCleanup : 1; |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 87 | |
| 88 | /// isMemCpySrc - This is true if this aggregate is memcpy'd from. |
| 89 | bool isMemCpySrc : 1; |
| 90 | |
Zhou Sheng | 33b0b8d | 2007-07-06 06:01:16 +0000 | [diff] [blame] | 91 | /// isMemCpyDst - This is true if this aggregate is memcpy'd into. |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 92 | bool isMemCpyDst : 1; |
| 93 | |
| 94 | AllocaInfo() |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 95 | : isUnsafe(false), needsCleanup(false), |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 96 | isMemCpySrc(false), isMemCpyDst(false) {} |
| 97 | }; |
| 98 | |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 99 | unsigned SRThreshold; |
| 100 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 101 | void MarkUnsafe(AllocaInfo &I) { I.isUnsafe = true; } |
| 102 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 103 | int isSafeAllocaToScalarRepl(AllocaInst *AI); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 104 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 105 | void isSafeUseOfAllocation(Instruction *User, AllocaInst *AI, |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 106 | AllocaInfo &Info); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 107 | void isSafeElementUse(Value *Ptr, bool isFirstElt, AllocaInst *AI, |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 108 | AllocaInfo &Info); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 109 | void isSafeMemIntrinsicOnAllocation(MemIntrinsic *MI, AllocaInst *AI, |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 110 | unsigned OpNo, AllocaInfo &Info); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 111 | void isSafeUseOfBitCastedAllocation(BitCastInst *User, AllocaInst *AI, |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 112 | AllocaInfo &Info); |
| 113 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 114 | void DoScalarReplacement(AllocaInst *AI, |
| 115 | std::vector<AllocaInst*> &WorkList); |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 116 | void CleanupGEP(GetElementPtrInst *GEP); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 117 | void CleanupAllocaUsers(AllocaInst *AI); |
| 118 | AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocaInst *Base); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 119 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 120 | void RewriteBitCastUserOfAlloca(Instruction *BCInst, AllocaInst *AI, |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 121 | SmallVector<AllocaInst*, 32> &NewElts); |
| 122 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 123 | void RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 124 | AllocaInst *AI, |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 125 | SmallVector<AllocaInst*, 32> &NewElts); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 126 | void RewriteStoreUserOfWholeAlloca(StoreInst *SI, AllocaInst *AI, |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 127 | SmallVector<AllocaInst*, 32> &NewElts); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 128 | void RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocaInst *AI, |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 129 | SmallVector<AllocaInst*, 32> &NewElts); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 131 | bool CanConvertToScalar(Value *V, bool &IsNotTrivial, const Type *&VecTy, |
Chris Lattner | 1a3257b | 2009-02-03 18:15:05 +0000 | [diff] [blame] | 132 | bool &SawVec, uint64_t Offset, unsigned AllocaSize); |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 133 | void ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset); |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 134 | Value *ConvertScalar_ExtractValue(Value *NV, const Type *ToType, |
Chris Lattner | 9bc67da | 2009-02-03 19:45:44 +0000 | [diff] [blame] | 135 | uint64_t Offset, IRBuilder<> &Builder); |
Chris Lattner | 9b872db | 2009-02-03 19:30:11 +0000 | [diff] [blame] | 136 | Value *ConvertScalar_InsertValue(Value *StoredVal, Value *ExistingVal, |
Chris Lattner | 65a6502 | 2009-02-03 19:41:50 +0000 | [diff] [blame] | 137 | uint64_t Offset, IRBuilder<> &Builder); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 138 | static Instruction *isOnlyCopiedFromConstantGlobal(AllocaInst *AI); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 139 | }; |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 142 | char SROA::ID = 0; |
| 143 | static RegisterPass<SROA> X("scalarrepl", "Scalar Replacement of Aggregates"); |
| 144 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 145 | // Public interface to the ScalarReplAggregates pass |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 146 | FunctionPass *llvm::createScalarReplAggregatesPass(signed int Threshold) { |
| 147 | return new SROA(Threshold); |
| 148 | } |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 149 | |
| 150 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 151 | bool SROA::runOnFunction(Function &F) { |
Dan Gohman | e4af1cf | 2009-08-19 18:22:18 +0000 | [diff] [blame] | 152 | TD = getAnalysisIfAvailable<TargetData>(); |
| 153 | |
Chris Lattner | fe7ea0d | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 154 | bool Changed = performPromotion(F); |
Dan Gohman | e4af1cf | 2009-08-19 18:22:18 +0000 | [diff] [blame] | 155 | |
| 156 | // FIXME: ScalarRepl currently depends on TargetData more than it |
| 157 | // theoretically needs to. It should be refactored in order to support |
| 158 | // target-independent IR. Until this is done, just skip the actual |
| 159 | // scalar-replacement portion of this pass. |
| 160 | if (!TD) return Changed; |
| 161 | |
Chris Lattner | fe7ea0d | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 162 | while (1) { |
| 163 | bool LocalChange = performScalarRepl(F); |
| 164 | if (!LocalChange) break; // No need to repromote if no scalarrepl |
| 165 | Changed = true; |
| 166 | LocalChange = performPromotion(F); |
| 167 | if (!LocalChange) break; // No need to re-scalarrepl if no promotion |
| 168 | } |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 169 | |
| 170 | return Changed; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | bool SROA::performPromotion(Function &F) { |
| 175 | std::vector<AllocaInst*> Allocas; |
Devang Patel | 326821e | 2007-06-07 21:57:03 +0000 | [diff] [blame] | 176 | DominatorTree &DT = getAnalysis<DominatorTree>(); |
Chris Lattner | 43f820d | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 177 | DominanceFrontier &DF = getAnalysis<DominanceFrontier>(); |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 02a3be0 | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 179 | BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 180 | |
Chris Lattner | fe7ea0d | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 181 | bool Changed = false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 183 | while (1) { |
| 184 | Allocas.clear(); |
| 185 | |
| 186 | // Find allocas that are safe to promote, by looking at all instructions in |
| 187 | // the entry node |
| 188 | for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) |
| 189 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? |
Devang Patel | 41968df | 2007-04-25 17:15:20 +0000 | [diff] [blame] | 190 | if (isAllocaPromotable(AI)) |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 191 | Allocas.push_back(AI); |
| 192 | |
| 193 | if (Allocas.empty()) break; |
| 194 | |
Nick Lewycky | ce2c51b | 2009-11-23 03:50:44 +0000 | [diff] [blame] | 195 | PromoteMemToReg(Allocas, DT, DF); |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 196 | NumPromoted += Allocas.size(); |
| 197 | Changed = true; |
| 198 | } |
| 199 | |
| 200 | return Changed; |
| 201 | } |
| 202 | |
Chris Lattner | 963a97f | 2008-06-22 17:46:21 +0000 | [diff] [blame] | 203 | /// getNumSAElements - Return the number of elements in the specific struct or |
| 204 | /// array. |
| 205 | static uint64_t getNumSAElements(const Type *T) { |
| 206 | if (const StructType *ST = dyn_cast<StructType>(T)) |
| 207 | return ST->getNumElements(); |
| 208 | return cast<ArrayType>(T)->getNumElements(); |
| 209 | } |
| 210 | |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 211 | // performScalarRepl - This algorithm is a simple worklist driven algorithm, |
| 212 | // which runs on all of the malloc/alloca instructions in the function, removing |
| 213 | // them if they are only used by getelementptr instructions. |
| 214 | // |
| 215 | bool SROA::performScalarRepl(Function &F) { |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 216 | std::vector<AllocaInst*> WorkList; |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 217 | |
| 218 | // 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] | 219 | BasicBlock &BB = F.getEntryBlock(); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 220 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 221 | if (AllocaInst *A = dyn_cast<AllocaInst>(I)) |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 222 | WorkList.push_back(A); |
| 223 | |
| 224 | // Process the worklist |
| 225 | bool Changed = false; |
| 226 | while (!WorkList.empty()) { |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 227 | AllocaInst *AI = WorkList.back(); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 228 | WorkList.pop_back(); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 229 | |
Chris Lattner | add2bd7 | 2006-12-22 23:14:42 +0000 | [diff] [blame] | 230 | // Handle dead allocas trivially. These can be formed by SROA'ing arrays |
| 231 | // with unused elements. |
| 232 | if (AI->use_empty()) { |
| 233 | AI->eraseFromParent(); |
| 234 | continue; |
| 235 | } |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 236 | |
| 237 | // If this alloca is impossible for us to promote, reject it early. |
| 238 | if (AI->isArrayAllocation() || !AI->getAllocatedType()->isSized()) |
| 239 | continue; |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 240 | |
| 241 | // Check to see if this allocation is only modified by a memcpy/memmove from |
| 242 | // a constant global. If this is the case, we can change all users to use |
| 243 | // the constant global instead. This is commonly produced by the CFE by |
| 244 | // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A' |
| 245 | // is only subsequently read. |
| 246 | if (Instruction *TheCopy = isOnlyCopiedFromConstantGlobal(AI)) { |
Nick Lewycky | 5913625 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 247 | DEBUG(errs() << "Found alloca equal to global: " << *AI << '\n'); |
| 248 | DEBUG(errs() << " memcpy = " << *TheCopy << '\n'); |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 249 | Constant *TheSrc = cast<Constant>(TheCopy->getOperand(2)); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 250 | AI->replaceAllUsesWith(ConstantExpr::getBitCast(TheSrc, AI->getType())); |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 251 | TheCopy->eraseFromParent(); // Don't mutate the global. |
| 252 | AI->eraseFromParent(); |
| 253 | ++NumGlobals; |
| 254 | Changed = true; |
| 255 | continue; |
| 256 | } |
Chris Lattner | 15c8277 | 2009-02-02 20:44:45 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 258 | // Check to see if we can perform the core SROA transformation. We cannot |
| 259 | // transform the allocation instruction if it is an array allocation |
| 260 | // (allocations OF arrays are ok though), and an allocation of a scalar |
| 261 | // value cannot be decomposed at all. |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 262 | uint64_t AllocaSize = TD->getTypeAllocSize(AI->getAllocatedType()); |
Bill Wendling | 5a377cb | 2009-03-03 12:12:58 +0000 | [diff] [blame] | 263 | |
Nick Lewycky | d3aa25e | 2009-08-17 05:37:31 +0000 | [diff] [blame] | 264 | // Do not promote [0 x %struct]. |
| 265 | if (AllocaSize == 0) continue; |
| 266 | |
Bill Wendling | 5a377cb | 2009-03-03 12:12:58 +0000 | [diff] [blame] | 267 | // Do not promote any struct whose size is too big. |
Bill Wendling | 3aaf5d9 | 2009-03-03 19:18:49 +0000 | [diff] [blame] | 268 | if (AllocaSize > SRThreshold) continue; |
Nick Lewycky | d3aa25e | 2009-08-17 05:37:31 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 270 | if ((isa<StructType>(AI->getAllocatedType()) || |
| 271 | isa<ArrayType>(AI->getAllocatedType())) && |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 272 | // Do not promote any struct into more than "32" separate vars. |
Evan Cheng | 67fca63 | 2009-03-06 00:56:43 +0000 | [diff] [blame] | 273 | getNumSAElements(AI->getAllocatedType()) <= SRThreshold/4) { |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 274 | // Check that all of the users of the allocation are capable of being |
| 275 | // transformed. |
| 276 | switch (isSafeAllocaToScalarRepl(AI)) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 277 | default: llvm_unreachable("Unexpected value!"); |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 278 | case 0: // Not safe to scalar replace. |
| 279 | break; |
| 280 | case 1: // Safe, but requires cleanup/canonicalizations first |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 281 | CleanupAllocaUsers(AI); |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 282 | // FALL THROUGH. |
| 283 | case 3: // Safe to scalar replace. |
| 284 | DoScalarReplacement(AI, WorkList); |
| 285 | Changed = true; |
| 286 | continue; |
| 287 | } |
| 288 | } |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 289 | |
| 290 | // If we can turn this aggregate value (potentially with casts) into a |
| 291 | // simple scalar value that can be mem2reg'd into a register value. |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 292 | // IsNotTrivial tracks whether this is something that mem2reg could have |
| 293 | // promoted itself. If so, we don't want to transform it needlessly. Note |
| 294 | // that we can't just check based on the type: the alloca may be of an i32 |
| 295 | // but that has pointer arithmetic to set byte 3 of it or something. |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 296 | bool IsNotTrivial = false; |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 297 | const Type *VectorTy = 0; |
Chris Lattner | 1a3257b | 2009-02-03 18:15:05 +0000 | [diff] [blame] | 298 | bool HadAVector = false; |
| 299 | if (CanConvertToScalar(AI, IsNotTrivial, VectorTy, HadAVector, |
Chris Lattner | 0ff83ab | 2009-03-04 19:22:30 +0000 | [diff] [blame] | 300 | 0, unsigned(AllocaSize)) && IsNotTrivial) { |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 301 | AllocaInst *NewAI; |
Chris Lattner | 1a3257b | 2009-02-03 18:15:05 +0000 | [diff] [blame] | 302 | // If we were able to find a vector type that can handle this with |
| 303 | // insert/extract elements, and if there was at least one use that had |
| 304 | // a vector type, promote this to a vector. We don't want to promote |
| 305 | // random stuff that doesn't use vectors (e.g. <9 x double>) because then |
| 306 | // we just get a lot of insert/extracts. If at least one vector is |
| 307 | // involved, then we probably really do have a union of vector/array. |
| 308 | if (VectorTy && isa<VectorType>(VectorTy) && HadAVector) { |
Nick Lewycky | 5913625 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 309 | DEBUG(errs() << "CONVERT TO VECTOR: " << *AI << "\n TYPE = " |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 310 | << *VectorTy << '\n'); |
Chris Lattner | 15c8277 | 2009-02-02 20:44:45 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 312 | // Create and insert the vector alloca. |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 313 | NewAI = new AllocaInst(VectorTy, 0, "", AI->getParent()->begin()); |
Chris Lattner | 15c8277 | 2009-02-02 20:44:45 +0000 | [diff] [blame] | 314 | ConvertUsesToScalar(AI, NewAI, 0); |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 315 | } else { |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 316 | DEBUG(errs() << "CONVERT TO SCALAR INTEGER: " << *AI << "\n"); |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 317 | |
| 318 | // Create and insert the integer alloca. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 319 | const Type *NewTy = IntegerType::get(AI->getContext(), AllocaSize*8); |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 320 | NewAI = new AllocaInst(NewTy, 0, "", AI->getParent()->begin()); |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 321 | ConvertUsesToScalar(AI, NewAI, 0); |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 322 | } |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 323 | NewAI->takeName(AI); |
| 324 | AI->eraseFromParent(); |
| 325 | ++NumConverted; |
| 326 | Changed = true; |
| 327 | continue; |
| 328 | } |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 330 | // Otherwise, couldn't process this alloca. |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return Changed; |
| 334 | } |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 335 | |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 336 | /// DoScalarReplacement - This alloca satisfied the isSafeAllocaToScalarRepl |
| 337 | /// predicate, do SROA now. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 338 | void SROA::DoScalarReplacement(AllocaInst *AI, |
| 339 | std::vector<AllocaInst*> &WorkList) { |
Chris Lattner | ff11470 | 2009-09-15 05:14:57 +0000 | [diff] [blame] | 340 | DEBUG(errs() << "Found inst to SROA: " << *AI << '\n'); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 341 | SmallVector<AllocaInst*, 32> ElementAllocas; |
| 342 | if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) { |
| 343 | ElementAllocas.reserve(ST->getNumContainedTypes()); |
| 344 | for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) { |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 345 | AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0, |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 346 | AI->getAlignment(), |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 347 | AI->getName() + "." + Twine(i), AI); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 348 | ElementAllocas.push_back(NA); |
| 349 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 350 | } |
| 351 | } else { |
| 352 | const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType()); |
| 353 | ElementAllocas.reserve(AT->getNumElements()); |
| 354 | const Type *ElTy = AT->getElementType(); |
| 355 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 356 | AllocaInst *NA = new AllocaInst(ElTy, 0, AI->getAlignment(), |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 357 | AI->getName() + "." + Twine(i), AI); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 358 | ElementAllocas.push_back(NA); |
| 359 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Now that we have created the alloca instructions that we want to use, |
| 364 | // expand the getelementptr instructions to use them. |
| 365 | // |
| 366 | while (!AI->use_empty()) { |
| 367 | Instruction *User = cast<Instruction>(AI->use_back()); |
| 368 | if (BitCastInst *BCInst = dyn_cast<BitCastInst>(User)) { |
| 369 | RewriteBitCastUserOfAlloca(BCInst, AI, ElementAllocas); |
| 370 | BCInst->eraseFromParent(); |
| 371 | continue; |
| 372 | } |
| 373 | |
Chris Lattner | 2a6a645 | 2008-06-23 17:11:23 +0000 | [diff] [blame] | 374 | // Replace: |
| 375 | // %res = load { i32, i32 }* %alloc |
| 376 | // with: |
| 377 | // %load.0 = load i32* %alloc.0 |
| 378 | // %insert.0 insertvalue { i32, i32 } zeroinitializer, i32 %load.0, 0 |
| 379 | // %load.1 = load i32* %alloc.1 |
| 380 | // %insert = insertvalue { i32, i32 } %insert.0, i32 %load.1, 1 |
Matthijs Kooijman | 0251814 | 2008-06-05 12:51:53 +0000 | [diff] [blame] | 381 | // (Also works for arrays instead of structs) |
| 382 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 383 | Value *Insert = UndefValue::get(LI->getType()); |
Matthijs Kooijman | 0251814 | 2008-06-05 12:51:53 +0000 | [diff] [blame] | 384 | for (unsigned i = 0, e = ElementAllocas.size(); i != e; ++i) { |
| 385 | Value *Load = new LoadInst(ElementAllocas[i], "load", LI); |
| 386 | Insert = InsertValueInst::Create(Insert, Load, i, "insert", LI); |
| 387 | } |
| 388 | LI->replaceAllUsesWith(Insert); |
| 389 | LI->eraseFromParent(); |
| 390 | continue; |
| 391 | } |
| 392 | |
Chris Lattner | 2a6a645 | 2008-06-23 17:11:23 +0000 | [diff] [blame] | 393 | // Replace: |
| 394 | // store { i32, i32 } %val, { i32, i32 }* %alloc |
| 395 | // with: |
| 396 | // %val.0 = extractvalue { i32, i32 } %val, 0 |
| 397 | // store i32 %val.0, i32* %alloc.0 |
| 398 | // %val.1 = extractvalue { i32, i32 } %val, 1 |
| 399 | // store i32 %val.1, i32* %alloc.1 |
Matthijs Kooijman | 0251814 | 2008-06-05 12:51:53 +0000 | [diff] [blame] | 400 | // (Also works for arrays instead of structs) |
| 401 | if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 402 | Value *Val = SI->getOperand(0); |
| 403 | for (unsigned i = 0, e = ElementAllocas.size(); i != e; ++i) { |
| 404 | Value *Extract = ExtractValueInst::Create(Val, i, Val->getName(), SI); |
| 405 | new StoreInst(Extract, ElementAllocas[i], SI); |
| 406 | } |
| 407 | SI->eraseFromParent(); |
| 408 | continue; |
| 409 | } |
| 410 | |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 411 | GetElementPtrInst *GEPI = cast<GetElementPtrInst>(User); |
| 412 | // We now know that the GEP is of the form: GEP <ptr>, 0, <cst> |
| 413 | unsigned Idx = |
| 414 | (unsigned)cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue(); |
| 415 | |
| 416 | assert(Idx < ElementAllocas.size() && "Index out of range?"); |
| 417 | AllocaInst *AllocaToUse = ElementAllocas[Idx]; |
| 418 | |
| 419 | Value *RepValue; |
| 420 | if (GEPI->getNumOperands() == 3) { |
| 421 | // Do not insert a new getelementptr instruction with zero indices, only |
| 422 | // to have it optimized out later. |
| 423 | RepValue = AllocaToUse; |
| 424 | } else { |
| 425 | // We are indexing deeply into the structure, so we still need a |
| 426 | // getelement ptr instruction to finish the indexing. This may be |
| 427 | // expanded itself once the worklist is rerun. |
| 428 | // |
| 429 | SmallVector<Value*, 8> NewArgs; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 430 | NewArgs.push_back(Constant::getNullValue( |
| 431 | Type::getInt32Ty(AI->getContext()))); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 432 | NewArgs.append(GEPI->op_begin()+3, GEPI->op_end()); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 433 | RepValue = GetElementPtrInst::Create(AllocaToUse, NewArgs.begin(), |
| 434 | NewArgs.end(), "", GEPI); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 435 | RepValue->takeName(GEPI); |
| 436 | } |
| 437 | |
| 438 | // If this GEP is to the start of the aggregate, check for memcpys. |
Chris Lattner | d356a7e | 2009-01-07 06:25:07 +0000 | [diff] [blame] | 439 | if (Idx == 0 && GEPI->hasAllZeroIndices()) |
| 440 | RewriteBitCastUserOfAlloca(GEPI, AI, ElementAllocas); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 441 | |
| 442 | // Move all of the users over to the new GEP. |
| 443 | GEPI->replaceAllUsesWith(RepValue); |
| 444 | // Delete the old GEP |
| 445 | GEPI->eraseFromParent(); |
| 446 | } |
| 447 | |
| 448 | // Finally, delete the Alloca instruction |
| 449 | AI->eraseFromParent(); |
| 450 | NumReplaced++; |
| 451 | } |
| 452 | |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 453 | /// 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] | 454 | /// getelementptr instruction of an array aggregate allocation. isFirstElt |
| 455 | /// indicates whether Ptr is known to the start of the aggregate. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 456 | void SROA::isSafeElementUse(Value *Ptr, bool isFirstElt, AllocaInst *AI, |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 457 | AllocaInfo &Info) { |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 458 | for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end(); |
| 459 | I != E; ++I) { |
| 460 | Instruction *User = cast<Instruction>(*I); |
| 461 | switch (User->getOpcode()) { |
| 462 | case Instruction::Load: break; |
| 463 | case Instruction::Store: |
| 464 | // Store is ok if storing INTO the pointer, not storing the pointer |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 465 | if (User->getOperand(0) == Ptr) return MarkUnsafe(Info); |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 466 | break; |
| 467 | case Instruction::GetElementPtr: { |
| 468 | GetElementPtrInst *GEP = cast<GetElementPtrInst>(User); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 469 | bool AreAllZeroIndices = isFirstElt; |
Chris Lattner | 8976e59 | 2009-11-27 16:37:41 +0000 | [diff] [blame] | 470 | if (GEP->getNumOperands() > 1 && |
| 471 | (!isa<ConstantInt>(GEP->getOperand(1)) || |
| 472 | !cast<ConstantInt>(GEP->getOperand(1))->isZero())) |
| 473 | // Using pointer arithmetic to navigate the array. |
| 474 | return MarkUnsafe(Info); |
| 475 | |
| 476 | // Verify that any array subscripts are in range. |
| 477 | for (gep_type_iterator GEPIt = gep_type_begin(GEP), |
| 478 | E = gep_type_end(GEP); GEPIt != E; ++GEPIt) { |
| 479 | // Ignore struct elements, no extra checking needed for these. |
| 480 | if (isa<StructType>(*GEPIt)) |
| 481 | continue; |
| 482 | |
| 483 | // This GEP indexes an array. Verify that this is an in-range |
| 484 | // constant integer. Specifically, consider A[0][i]. We cannot know that |
| 485 | // the user isn't doing invalid things like allowing i to index an |
| 486 | // out-of-range subscript that accesses A[1]. Because of this, we have |
| 487 | // to reject SROA of any accesses into structs where any of the |
| 488 | // components are variables. |
| 489 | ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPIt.getOperand()); |
| 490 | if (!IdxVal) return MarkUnsafe(Info); |
| 491 | |
| 492 | // Are all indices still zero? |
| 493 | AreAllZeroIndices &= IdxVal->isZero(); |
| 494 | |
| 495 | if (const ArrayType *AT = dyn_cast<ArrayType>(*GEPIt)) { |
| 496 | if (IdxVal->getZExtValue() >= AT->getNumElements()) |
| 497 | return MarkUnsafe(Info); |
| 498 | } else if (const VectorType *VT = dyn_cast<VectorType>(*GEPIt)) { |
| 499 | if (IdxVal->getZExtValue() >= VT->getNumElements()) |
| 500 | return MarkUnsafe(Info); |
| 501 | } |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 502 | } |
Chris Lattner | 8976e59 | 2009-11-27 16:37:41 +0000 | [diff] [blame] | 503 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 504 | isSafeElementUse(GEP, AreAllZeroIndices, AI, Info); |
| 505 | if (Info.isUnsafe) return; |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 506 | break; |
| 507 | } |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 508 | case Instruction::BitCast: |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 509 | if (isFirstElt) { |
| 510 | isSafeUseOfBitCastedAllocation(cast<BitCastInst>(User), AI, Info); |
| 511 | if (Info.isUnsafe) return; |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 512 | break; |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 513 | } |
Nick Lewycky | 5913625 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 514 | DEBUG(errs() << " Transformation preventing inst: " << *User << '\n'); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 515 | return MarkUnsafe(Info); |
| 516 | case Instruction::Call: |
| 517 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) { |
| 518 | if (isFirstElt) { |
| 519 | isSafeMemIntrinsicOnAllocation(MI, AI, I.getOperandNo(), Info); |
| 520 | if (Info.isUnsafe) return; |
| 521 | break; |
| 522 | } |
| 523 | } |
Nick Lewycky | 5913625 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 524 | DEBUG(errs() << " Transformation preventing inst: " << *User << '\n'); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 525 | return MarkUnsafe(Info); |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 526 | default: |
Nick Lewycky | 5913625 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 527 | DEBUG(errs() << " Transformation preventing inst: " << *User << '\n'); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 528 | return MarkUnsafe(Info); |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 531 | return; // All users look ok :) |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 534 | /// AllUsersAreLoads - Return true if all users of this value are loads. |
| 535 | static bool AllUsersAreLoads(Value *Ptr) { |
| 536 | for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end(); |
| 537 | I != E; ++I) |
| 538 | if (cast<Instruction>(*I)->getOpcode() != Instruction::Load) |
| 539 | return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 540 | return true; |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 543 | /// isSafeUseOfAllocation - Check to see if this user is an allowed use for an |
| 544 | /// aggregate allocation. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 545 | void SROA::isSafeUseOfAllocation(Instruction *User, AllocaInst *AI, |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 546 | AllocaInfo &Info) { |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 547 | if (BitCastInst *C = dyn_cast<BitCastInst>(User)) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 548 | return isSafeUseOfBitCastedAllocation(C, AI, Info); |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 549 | |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 550 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) |
| 551 | if (!LI->isVolatile()) |
| 552 | return;// Loads (returning a first class aggregrate) are always rewritable |
Matthijs Kooijman | 0251814 | 2008-06-05 12:51:53 +0000 | [diff] [blame] | 553 | |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 554 | if (StoreInst *SI = dyn_cast<StoreInst>(User)) |
| 555 | if (!SI->isVolatile() && SI->getOperand(0) != AI) |
| 556 | return;// Store is ok if storing INTO the pointer, not storing the pointer |
Matthijs Kooijman | 0251814 | 2008-06-05 12:51:53 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 558 | GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User); |
| 559 | if (GEPI == 0) |
| 560 | return MarkUnsafe(Info); |
| 561 | |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 562 | gep_type_iterator I = gep_type_begin(GEPI), E = gep_type_end(GEPI); |
| 563 | |
Chris Lattner | 25de486 | 2006-03-08 01:05:29 +0000 | [diff] [blame] | 564 | // 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] | 565 | if (I == E || |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 566 | I.getOperand() != Constant::getNullValue(I.getOperand()->getType())) { |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 567 | return MarkUnsafe(Info); |
| 568 | } |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 569 | |
| 570 | ++I; |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 571 | if (I == E) return MarkUnsafe(Info); // ran out of GEP indices?? |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 572 | |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 573 | bool IsAllZeroIndices = true; |
| 574 | |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 575 | // If the first index is a non-constant index into an array, see if we can |
| 576 | // handle it as a special case. |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 577 | if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) { |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 578 | if (!isa<ConstantInt>(I.getOperand())) { |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 579 | IsAllZeroIndices = 0; |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 580 | uint64_t NumElements = AT->getNumElements(); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 581 | |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 582 | // If this is an array index and the index is not constant, we cannot |
| 583 | // promote... that is unless the array has exactly one or two elements in |
| 584 | // it, in which case we CAN promote it, but we have to canonicalize this |
| 585 | // out if this is the only problem. |
Chris Lattner | 25de486 | 2006-03-08 01:05:29 +0000 | [diff] [blame] | 586 | if ((NumElements == 1 || NumElements == 2) && |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 587 | AllUsersAreLoads(GEPI)) { |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 588 | Info.needsCleanup = true; |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 589 | return; // Canonicalization required! |
| 590 | } |
| 591 | return MarkUnsafe(Info); |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 592 | } |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 593 | } |
Matthijs Kooijman | 5fac55f | 2008-10-06 16:23:31 +0000 | [diff] [blame] | 594 | |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 595 | // Walk through the GEP type indices, checking the types that this indexes |
| 596 | // into. |
| 597 | for (; I != E; ++I) { |
| 598 | // Ignore struct elements, no extra checking needed for these. |
| 599 | if (isa<StructType>(*I)) |
| 600 | continue; |
| 601 | |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 602 | ConstantInt *IdxVal = dyn_cast<ConstantInt>(I.getOperand()); |
| 603 | if (!IdxVal) return MarkUnsafe(Info); |
Matthijs Kooijman | 5fac55f | 2008-10-06 16:23:31 +0000 | [diff] [blame] | 604 | |
| 605 | // Are all indices still zero? |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 606 | IsAllZeroIndices &= IdxVal->isZero(); |
Matthijs Kooijman | 5fac55f | 2008-10-06 16:23:31 +0000 | [diff] [blame] | 607 | |
| 608 | if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) { |
| 609 | // This GEP indexes an array. Verify that this is an in-range constant |
| 610 | // integer. Specifically, consider A[0][i]. We cannot know that the user |
| 611 | // isn't doing invalid things like allowing i to index an out-of-range |
| 612 | // subscript that accesses A[1]. Because of this, we have to reject SROA |
Bob Wilson | d614a1f | 2009-12-04 21:51:35 +0000 | [diff] [blame] | 613 | // of any accesses into structs where any of the components are variables. |
Matthijs Kooijman | 5fac55f | 2008-10-06 16:23:31 +0000 | [diff] [blame] | 614 | if (IdxVal->getZExtValue() >= AT->getNumElements()) |
| 615 | return MarkUnsafe(Info); |
Dale Johannesen | c0bc547 | 2008-11-04 20:54:03 +0000 | [diff] [blame] | 616 | } else if (const VectorType *VT = dyn_cast<VectorType>(*I)) { |
| 617 | if (IdxVal->getZExtValue() >= VT->getNumElements()) |
| 618 | return MarkUnsafe(Info); |
Matthijs Kooijman | 5fac55f | 2008-10-06 16:23:31 +0000 | [diff] [blame] | 619 | } |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 622 | // If there are any non-simple uses of this getelementptr, make sure to reject |
| 623 | // them. |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 624 | return isSafeElementUse(GEPI, IsAllZeroIndices, AI, Info); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Bob Wilson | 39fdd69 | 2009-12-04 21:57:37 +0000 | [diff] [blame^] | 627 | /// isSafeMemIntrinsicOnAllocation - Check if the specified memory |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 628 | /// intrinsic can be promoted by SROA. At this point, we know that the operand |
| 629 | /// of the memintrinsic is a pointer to the beginning of the allocation. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 630 | void SROA::isSafeMemIntrinsicOnAllocation(MemIntrinsic *MI, AllocaInst *AI, |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 631 | unsigned OpNo, AllocaInfo &Info) { |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 632 | // If not constant length, give up. |
| 633 | ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength()); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 634 | if (!Length) return MarkUnsafe(Info); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 635 | |
| 636 | // If not the whole aggregate, give up. |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 637 | if (Length->getZExtValue() != |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 638 | TD->getTypeAllocSize(AI->getType()->getElementType())) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 639 | return MarkUnsafe(Info); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 640 | |
| 641 | // We only know about memcpy/memset/memmove. |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 642 | if (!isa<MemIntrinsic>(MI)) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 643 | return MarkUnsafe(Info); |
| 644 | |
| 645 | // Otherwise, we can transform it. Determine whether this is a memcpy/set |
| 646 | // into or out of the aggregate. |
| 647 | if (OpNo == 1) |
| 648 | Info.isMemCpyDst = true; |
| 649 | else { |
| 650 | assert(OpNo == 2); |
| 651 | Info.isMemCpySrc = true; |
| 652 | } |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Bob Wilson | 39fdd69 | 2009-12-04 21:57:37 +0000 | [diff] [blame^] | 655 | /// isSafeUseOfBitCastedAllocation - Check if all users of this bitcast |
| 656 | /// from an alloca are safe for SROA of that alloca. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 657 | void SROA::isSafeUseOfBitCastedAllocation(BitCastInst *BC, AllocaInst *AI, |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 658 | AllocaInfo &Info) { |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 659 | for (Value::use_iterator UI = BC->use_begin(), E = BC->use_end(); |
| 660 | UI != E; ++UI) { |
| 661 | if (BitCastInst *BCU = dyn_cast<BitCastInst>(UI)) { |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 662 | isSafeUseOfBitCastedAllocation(BCU, AI, Info); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 663 | } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(UI)) { |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 664 | isSafeMemIntrinsicOnAllocation(MI, AI, UI.getOperandNo(), Info); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 665 | } else if (StoreInst *SI = dyn_cast<StoreInst>(UI)) { |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 666 | if (SI->isVolatile()) |
| 667 | return MarkUnsafe(Info); |
| 668 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 669 | // If storing the entire alloca in one chunk through a bitcasted pointer |
| 670 | // to integer, we can transform it. This happens (for example) when you |
| 671 | // cast a {i32,i32}* to i64* and store through it. This is similar to the |
| 672 | // memcpy case and occurs in various "byval" cases and emulated memcpys. |
| 673 | if (isa<IntegerType>(SI->getOperand(0)->getType()) && |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 674 | TD->getTypeAllocSize(SI->getOperand(0)->getType()) == |
| 675 | TD->getTypeAllocSize(AI->getType()->getElementType())) { |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 676 | Info.isMemCpyDst = true; |
| 677 | continue; |
| 678 | } |
| 679 | return MarkUnsafe(Info); |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 680 | } else if (LoadInst *LI = dyn_cast<LoadInst>(UI)) { |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 681 | if (LI->isVolatile()) |
| 682 | return MarkUnsafe(Info); |
| 683 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 684 | // If loading the entire alloca in one chunk through a bitcasted pointer |
| 685 | // to integer, we can transform it. This happens (for example) when you |
| 686 | // cast a {i32,i32}* to i64* and load through it. This is similar to the |
| 687 | // memcpy case and occurs in various "byval" cases and emulated memcpys. |
| 688 | if (isa<IntegerType>(LI->getType()) && |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 689 | TD->getTypeAllocSize(LI->getType()) == |
| 690 | TD->getTypeAllocSize(AI->getType()->getElementType())) { |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 691 | Info.isMemCpySrc = true; |
| 692 | continue; |
| 693 | } |
| 694 | return MarkUnsafe(Info); |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 695 | } else if (isa<DbgInfoIntrinsic>(UI)) { |
| 696 | // If one user is DbgInfoIntrinsic then check if all users are |
| 697 | // DbgInfoIntrinsics. |
| 698 | if (OnlyUsedByDbgInfoIntrinsics(BC)) { |
| 699 | Info.needsCleanup = true; |
| 700 | return; |
| 701 | } |
| 702 | else |
| 703 | MarkUnsafe(Info); |
| 704 | } |
| 705 | else { |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 706 | return MarkUnsafe(Info); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 707 | } |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 708 | if (Info.isUnsafe) return; |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 709 | } |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 712 | /// RewriteBitCastUserOfAlloca - BCInst (transitively) bitcasts AI, or indexes |
| 713 | /// to its first element. Transform users of the cast to use the new values |
| 714 | /// instead. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 715 | void SROA::RewriteBitCastUserOfAlloca(Instruction *BCInst, AllocaInst *AI, |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 716 | SmallVector<AllocaInst*, 32> &NewElts) { |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 717 | Value::use_iterator UI = BCInst->use_begin(), UE = BCInst->use_end(); |
| 718 | while (UI != UE) { |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 719 | Instruction *User = cast<Instruction>(*UI++); |
| 720 | if (BitCastInst *BCU = dyn_cast<BitCastInst>(User)) { |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 721 | RewriteBitCastUserOfAlloca(BCU, AI, NewElts); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 722 | if (BCU->use_empty()) BCU->eraseFromParent(); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 723 | continue; |
| 724 | } |
| 725 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 726 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) { |
| 727 | // This must be memcpy/memmove/memset of the entire aggregate. |
| 728 | // Split into one per element. |
| 729 | RewriteMemIntrinUserOfAlloca(MI, BCInst, AI, NewElts); |
Chris Lattner | 8bf9911 | 2007-03-19 00:16:43 +0000 | [diff] [blame] | 730 | continue; |
| 731 | } |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 732 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 733 | if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 734 | // If this is a store of the entire alloca from an integer, rewrite it. |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 735 | RewriteStoreUserOfWholeAlloca(SI, AI, NewElts); |
| 736 | continue; |
| 737 | } |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 738 | |
| 739 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
| 740 | // If this is a load of the entire alloca to an integer, rewrite it. |
| 741 | RewriteLoadUserOfWholeAlloca(LI, AI, NewElts); |
| 742 | continue; |
| 743 | } |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 744 | |
| 745 | // Otherwise it must be some other user of a gep of the first pointer. Just |
| 746 | // leave these alone. |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 747 | continue; |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 748 | } |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /// RewriteMemIntrinUserOfAlloca - MI is a memcpy/memset/memmove from or to AI. |
| 752 | /// Rewrite it to copy or set the elements of the scalarized memory. |
| 753 | void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 754 | AllocaInst *AI, |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 755 | SmallVector<AllocaInst*, 32> &NewElts) { |
| 756 | |
| 757 | // If this is a memcpy/memmove, construct the other pointer as the |
Chris Lattner | 88fe1ad | 2009-03-04 19:23:25 +0000 | [diff] [blame] | 758 | // appropriate type. The "Other" pointer is the pointer that goes to memory |
| 759 | // that doesn't have anything to do with the alloca that we are promoting. For |
| 760 | // memset, this Value* stays null. |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 761 | Value *OtherPtr = 0; |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 762 | LLVMContext &Context = MI->getContext(); |
Chris Lattner | dfe964c | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 763 | unsigned MemAlignment = MI->getAlignment(); |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 764 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { // memmove/memcopy |
| 765 | if (BCInst == MTI->getRawDest()) |
| 766 | OtherPtr = MTI->getRawSource(); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 767 | else { |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 768 | assert(BCInst == MTI->getRawSource()); |
| 769 | OtherPtr = MTI->getRawDest(); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | // If there is an other pointer, we want to convert it to the same pointer |
| 774 | // type as AI has, so we can GEP through it safely. |
| 775 | if (OtherPtr) { |
| 776 | // It is likely that OtherPtr is a bitcast, if so, remove it. |
| 777 | if (BitCastInst *BC = dyn_cast<BitCastInst>(OtherPtr)) |
| 778 | OtherPtr = BC->getOperand(0); |
| 779 | // All zero GEPs are effectively bitcasts. |
| 780 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(OtherPtr)) |
| 781 | if (GEP->hasAllZeroIndices()) |
| 782 | OtherPtr = GEP->getOperand(0); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 783 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 784 | if (ConstantExpr *BCE = dyn_cast<ConstantExpr>(OtherPtr)) |
| 785 | if (BCE->getOpcode() == Instruction::BitCast) |
| 786 | OtherPtr = BCE->getOperand(0); |
| 787 | |
| 788 | // If the pointer is not the right type, insert a bitcast to the right |
| 789 | // type. |
| 790 | if (OtherPtr->getType() != AI->getType()) |
| 791 | OtherPtr = new BitCastInst(OtherPtr, AI->getType(), OtherPtr->getName(), |
| 792 | MI); |
| 793 | } |
| 794 | |
| 795 | // Process each element of the aggregate. |
| 796 | Value *TheFn = MI->getOperand(0); |
| 797 | const Type *BytePtrTy = MI->getRawDest()->getType(); |
| 798 | bool SROADest = MI->getRawDest() == BCInst; |
| 799 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 800 | Constant *Zero = Constant::getNullValue(Type::getInt32Ty(MI->getContext())); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 801 | |
| 802 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 803 | // If this is a memcpy/memmove, emit a GEP of the other element address. |
| 804 | Value *OtherElt = 0; |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 805 | unsigned OtherEltAlign = MemAlignment; |
| 806 | |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 807 | if (OtherPtr) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 808 | Value *Idx[2] = { Zero, |
| 809 | ConstantInt::get(Type::getInt32Ty(MI->getContext()), i) }; |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 810 | OtherElt = GetElementPtrInst::Create(OtherPtr, Idx, Idx + 2, |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 811 | OtherPtr->getNameStr()+"."+Twine(i), |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 812 | MI); |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 813 | uint64_t EltOffset; |
| 814 | const PointerType *OtherPtrTy = cast<PointerType>(OtherPtr->getType()); |
| 815 | if (const StructType *ST = |
| 816 | dyn_cast<StructType>(OtherPtrTy->getElementType())) { |
| 817 | EltOffset = TD->getStructLayout(ST)->getElementOffset(i); |
| 818 | } else { |
| 819 | const Type *EltTy = |
| 820 | cast<SequentialType>(OtherPtr->getType())->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 821 | EltOffset = TD->getTypeAllocSize(EltTy)*i; |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | // The alignment of the other pointer is the guaranteed alignment of the |
| 825 | // element, which is affected by both the known alignment of the whole |
| 826 | // mem intrinsic and the alignment of the element. If the alignment of |
| 827 | // the memcpy (f.e.) is 32 but the element is at a 4-byte offset, then the |
| 828 | // known alignment is just 4 bytes. |
| 829 | OtherEltAlign = (unsigned)MinAlign(OtherEltAlign, EltOffset); |
Chris Lattner | c14d3ca | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 830 | } |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 831 | |
| 832 | Value *EltPtr = NewElts[i]; |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 833 | const Type *EltTy = cast<PointerType>(EltPtr->getType())->getElementType(); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 834 | |
| 835 | // If we got down to a scalar, insert a load or store as appropriate. |
| 836 | if (EltTy->isSingleValueType()) { |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 837 | if (isa<MemTransferInst>(MI)) { |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 838 | if (SROADest) { |
| 839 | // From Other to Alloca. |
| 840 | Value *Elt = new LoadInst(OtherElt, "tmp", false, OtherEltAlign, MI); |
| 841 | new StoreInst(Elt, EltPtr, MI); |
| 842 | } else { |
| 843 | // From Alloca to Other. |
| 844 | Value *Elt = new LoadInst(EltPtr, "tmp", MI); |
| 845 | new StoreInst(Elt, OtherElt, false, OtherEltAlign, MI); |
| 846 | } |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 847 | continue; |
| 848 | } |
| 849 | assert(isa<MemSetInst>(MI)); |
| 850 | |
| 851 | // If the stored element is zero (common case), just store a null |
| 852 | // constant. |
| 853 | Constant *StoreVal; |
| 854 | if (ConstantInt *CI = dyn_cast<ConstantInt>(MI->getOperand(2))) { |
| 855 | if (CI->isZero()) { |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 856 | StoreVal = Constant::getNullValue(EltTy); // 0.0, null, 0, <0,0> |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 857 | } else { |
| 858 | // If EltTy is a vector type, get the element type. |
Dan Gohman | 44118f0 | 2009-06-16 00:20:26 +0000 | [diff] [blame] | 859 | const Type *ValTy = EltTy->getScalarType(); |
| 860 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 861 | // Construct an integer with the right value. |
| 862 | unsigned EltSize = TD->getTypeSizeInBits(ValTy); |
| 863 | APInt OneVal(EltSize, CI->getZExtValue()); |
| 864 | APInt TotalVal(OneVal); |
| 865 | // Set each byte. |
| 866 | for (unsigned i = 0; 8*i < EltSize; ++i) { |
| 867 | TotalVal = TotalVal.shl(8); |
| 868 | TotalVal |= OneVal; |
| 869 | } |
| 870 | |
| 871 | // Convert the integer value to the appropriate type. |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 872 | StoreVal = ConstantInt::get(Context, TotalVal); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 873 | if (isa<PointerType>(ValTy)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 874 | StoreVal = ConstantExpr::getIntToPtr(StoreVal, ValTy); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 875 | else if (ValTy->isFloatingPoint()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 876 | StoreVal = ConstantExpr::getBitCast(StoreVal, ValTy); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 877 | assert(StoreVal->getType() == ValTy && "Type mismatch!"); |
| 878 | |
| 879 | // If the requested value was a vector constant, create it. |
| 880 | if (EltTy != ValTy) { |
| 881 | unsigned NumElts = cast<VectorType>(ValTy)->getNumElements(); |
| 882 | SmallVector<Constant*, 16> Elts(NumElts, StoreVal); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 883 | StoreVal = ConstantVector::get(&Elts[0], NumElts); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | new StoreInst(StoreVal, EltPtr, MI); |
| 887 | continue; |
| 888 | } |
| 889 | // Otherwise, if we're storing a byte variable, use a memset call for |
| 890 | // this element. |
| 891 | } |
| 892 | |
| 893 | // Cast the element pointer to BytePtrTy. |
| 894 | if (EltPtr->getType() != BytePtrTy) |
| 895 | EltPtr = new BitCastInst(EltPtr, BytePtrTy, EltPtr->getNameStr(), MI); |
| 896 | |
| 897 | // Cast the other pointer (if we have one) to BytePtrTy. |
| 898 | if (OtherElt && OtherElt->getType() != BytePtrTy) |
| 899 | OtherElt = new BitCastInst(OtherElt, BytePtrTy,OtherElt->getNameStr(), |
| 900 | MI); |
| 901 | |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 902 | unsigned EltSize = TD->getTypeAllocSize(EltTy); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 903 | |
| 904 | // Finally, insert the meminst for this element. |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 905 | if (isa<MemTransferInst>(MI)) { |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 906 | Value *Ops[] = { |
| 907 | SROADest ? EltPtr : OtherElt, // Dest ptr |
| 908 | SROADest ? OtherElt : EltPtr, // Src ptr |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 909 | ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 910 | // Align |
| 911 | ConstantInt::get(Type::getInt32Ty(MI->getContext()), OtherEltAlign) |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 912 | }; |
| 913 | CallInst::Create(TheFn, Ops, Ops + 4, "", MI); |
| 914 | } else { |
| 915 | assert(isa<MemSetInst>(MI)); |
| 916 | Value *Ops[] = { |
| 917 | EltPtr, MI->getOperand(2), // Dest, Value, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 918 | ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 919 | Zero // Align |
| 920 | }; |
| 921 | CallInst::Create(TheFn, Ops, Ops + 4, "", MI); |
| 922 | } |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 923 | } |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 924 | MI->eraseFromParent(); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 925 | } |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 926 | |
Bob Wilson | 39fdd69 | 2009-12-04 21:57:37 +0000 | [diff] [blame^] | 927 | /// RewriteStoreUserOfWholeAlloca - We found a store of an integer that |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 928 | /// overwrites the entire allocation. Extract out the pieces of the stored |
| 929 | /// integer and store them individually. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 930 | void SROA::RewriteStoreUserOfWholeAlloca(StoreInst *SI, AllocaInst *AI, |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 931 | SmallVector<AllocaInst*, 32> &NewElts){ |
| 932 | // Extract each element out of the integer according to its structure offset |
| 933 | // and store the element value to the individual alloca. |
| 934 | Value *SrcVal = SI->getOperand(0); |
| 935 | const Type *AllocaEltTy = AI->getType()->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 936 | uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 937 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 938 | // If this isn't a store of an integer to the whole alloca, it may be a store |
| 939 | // to the first element. Just ignore the store in this case and normal SROA |
Eli Friedman | 41b33f4 | 2009-06-01 09:14:32 +0000 | [diff] [blame] | 940 | // will handle it. |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 941 | if (!isa<IntegerType>(SrcVal->getType()) || |
Eli Friedman | 41b33f4 | 2009-06-01 09:14:32 +0000 | [diff] [blame] | 942 | TD->getTypeAllocSizeInBits(SrcVal->getType()) != AllocaSizeBits) |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 943 | return; |
Eli Friedman | 41b33f4 | 2009-06-01 09:14:32 +0000 | [diff] [blame] | 944 | // Handle tail padding by extending the operand |
| 945 | if (TD->getTypeSizeInBits(SrcVal->getType()) != AllocaSizeBits) |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 946 | SrcVal = new ZExtInst(SrcVal, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 947 | IntegerType::get(SI->getContext(), AllocaSizeBits), |
| 948 | "", SI); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 949 | |
Nick Lewycky | 5913625 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 950 | DEBUG(errs() << "PROMOTING STORE TO WHOLE ALLOCA: " << *AI << '\n' << *SI |
| 951 | << '\n'); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 952 | |
| 953 | // There are two forms here: AI could be an array or struct. Both cases |
| 954 | // have different ways to compute the element offset. |
| 955 | if (const StructType *EltSTy = dyn_cast<StructType>(AllocaEltTy)) { |
| 956 | const StructLayout *Layout = TD->getStructLayout(EltSTy); |
| 957 | |
| 958 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 959 | // Get the number of bits to shift SrcVal to get the value. |
| 960 | const Type *FieldTy = EltSTy->getElementType(i); |
| 961 | uint64_t Shift = Layout->getElementOffsetInBits(i); |
| 962 | |
| 963 | if (TD->isBigEndian()) |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 964 | Shift = AllocaSizeBits-Shift-TD->getTypeAllocSizeInBits(FieldTy); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 965 | |
| 966 | Value *EltVal = SrcVal; |
| 967 | if (Shift) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 968 | Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 969 | EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal, |
| 970 | "sroa.store.elt", SI); |
| 971 | } |
| 972 | |
| 973 | // Truncate down to an integer of the right size. |
| 974 | uint64_t FieldSizeBits = TD->getTypeSizeInBits(FieldTy); |
Chris Lattner | 583dd60 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 975 | |
| 976 | // Ignore zero sized fields like {}, they obviously contain no data. |
| 977 | if (FieldSizeBits == 0) continue; |
| 978 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 979 | if (FieldSizeBits != AllocaSizeBits) |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 980 | EltVal = new TruncInst(EltVal, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 981 | IntegerType::get(SI->getContext(), FieldSizeBits), |
| 982 | "", SI); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 983 | Value *DestField = NewElts[i]; |
| 984 | if (EltVal->getType() == FieldTy) { |
| 985 | // Storing to an integer field of this size, just do it. |
| 986 | } else if (FieldTy->isFloatingPoint() || isa<VectorType>(FieldTy)) { |
| 987 | // Bitcast to the right element type (for fp/vector values). |
| 988 | EltVal = new BitCastInst(EltVal, FieldTy, "", SI); |
| 989 | } else { |
| 990 | // Otherwise, bitcast the dest pointer (for aggregates). |
| 991 | DestField = new BitCastInst(DestField, |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 992 | PointerType::getUnqual(EltVal->getType()), |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 993 | "", SI); |
| 994 | } |
| 995 | new StoreInst(EltVal, DestField, SI); |
| 996 | } |
| 997 | |
| 998 | } else { |
| 999 | const ArrayType *ATy = cast<ArrayType>(AllocaEltTy); |
| 1000 | const Type *ArrayEltTy = ATy->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1001 | uint64_t ElementOffset = TD->getTypeAllocSizeInBits(ArrayEltTy); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1002 | uint64_t ElementSizeBits = TD->getTypeSizeInBits(ArrayEltTy); |
| 1003 | |
| 1004 | uint64_t Shift; |
| 1005 | |
| 1006 | if (TD->isBigEndian()) |
| 1007 | Shift = AllocaSizeBits-ElementOffset; |
| 1008 | else |
| 1009 | Shift = 0; |
| 1010 | |
| 1011 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
Chris Lattner | 583dd60 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 1012 | // Ignore zero sized fields like {}, they obviously contain no data. |
| 1013 | if (ElementSizeBits == 0) continue; |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1014 | |
| 1015 | Value *EltVal = SrcVal; |
| 1016 | if (Shift) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1017 | Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1018 | EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal, |
| 1019 | "sroa.store.elt", SI); |
| 1020 | } |
| 1021 | |
| 1022 | // Truncate down to an integer of the right size. |
| 1023 | if (ElementSizeBits != AllocaSizeBits) |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1024 | EltVal = new TruncInst(EltVal, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1025 | IntegerType::get(SI->getContext(), |
| 1026 | ElementSizeBits),"",SI); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1027 | Value *DestField = NewElts[i]; |
| 1028 | if (EltVal->getType() == ArrayEltTy) { |
| 1029 | // Storing to an integer field of this size, just do it. |
| 1030 | } else if (ArrayEltTy->isFloatingPoint() || isa<VectorType>(ArrayEltTy)) { |
| 1031 | // Bitcast to the right element type (for fp/vector values). |
| 1032 | EltVal = new BitCastInst(EltVal, ArrayEltTy, "", SI); |
| 1033 | } else { |
| 1034 | // Otherwise, bitcast the dest pointer (for aggregates). |
| 1035 | DestField = new BitCastInst(DestField, |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1036 | PointerType::getUnqual(EltVal->getType()), |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1037 | "", SI); |
| 1038 | } |
| 1039 | new StoreInst(EltVal, DestField, SI); |
| 1040 | |
| 1041 | if (TD->isBigEndian()) |
| 1042 | Shift -= ElementOffset; |
| 1043 | else |
| 1044 | Shift += ElementOffset; |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | SI->eraseFromParent(); |
| 1049 | } |
| 1050 | |
Bob Wilson | 39fdd69 | 2009-12-04 21:57:37 +0000 | [diff] [blame^] | 1051 | /// RewriteLoadUserOfWholeAlloca - We found a load of the entire allocation to |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1052 | /// an integer. Load the individual pieces to form the aggregate value. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1053 | void SROA::RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocaInst *AI, |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1054 | SmallVector<AllocaInst*, 32> &NewElts) { |
| 1055 | // Extract each element out of the NewElts according to its structure offset |
| 1056 | // and form the result value. |
| 1057 | const Type *AllocaEltTy = AI->getType()->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1058 | uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy); |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1059 | |
| 1060 | // If this isn't a load of the whole alloca to an integer, it may be a load |
| 1061 | // of the first element. Just ignore the load in this case and normal SROA |
Eli Friedman | 41b33f4 | 2009-06-01 09:14:32 +0000 | [diff] [blame] | 1062 | // will handle it. |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1063 | if (!isa<IntegerType>(LI->getType()) || |
Eli Friedman | 41b33f4 | 2009-06-01 09:14:32 +0000 | [diff] [blame] | 1064 | TD->getTypeAllocSizeInBits(LI->getType()) != AllocaSizeBits) |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1065 | return; |
| 1066 | |
Nick Lewycky | 5913625 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 1067 | DEBUG(errs() << "PROMOTING LOAD OF WHOLE ALLOCA: " << *AI << '\n' << *LI |
| 1068 | << '\n'); |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1069 | |
| 1070 | // There are two forms here: AI could be an array or struct. Both cases |
| 1071 | // have different ways to compute the element offset. |
| 1072 | const StructLayout *Layout = 0; |
| 1073 | uint64_t ArrayEltBitOffset = 0; |
| 1074 | if (const StructType *EltSTy = dyn_cast<StructType>(AllocaEltTy)) { |
| 1075 | Layout = TD->getStructLayout(EltSTy); |
| 1076 | } else { |
| 1077 | const Type *ArrayEltTy = cast<ArrayType>(AllocaEltTy)->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1078 | ArrayEltBitOffset = TD->getTypeAllocSizeInBits(ArrayEltTy); |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1079 | } |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 1080 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 1081 | Value *ResultVal = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1082 | Constant::getNullValue(IntegerType::get(LI->getContext(), AllocaSizeBits)); |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1083 | |
| 1084 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 1085 | // Load the value from the alloca. If the NewElt is an aggregate, cast |
| 1086 | // the pointer to an integer of the same size before doing the load. |
| 1087 | Value *SrcField = NewElts[i]; |
| 1088 | const Type *FieldTy = |
| 1089 | cast<PointerType>(SrcField->getType())->getElementType(); |
Chris Lattner | 583dd60 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 1090 | uint64_t FieldSizeBits = TD->getTypeSizeInBits(FieldTy); |
| 1091 | |
| 1092 | // Ignore zero sized fields like {}, they obviously contain no data. |
| 1093 | if (FieldSizeBits == 0) continue; |
| 1094 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1095 | const IntegerType *FieldIntTy = IntegerType::get(LI->getContext(), |
| 1096 | FieldSizeBits); |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1097 | if (!isa<IntegerType>(FieldTy) && !FieldTy->isFloatingPoint() && |
| 1098 | !isa<VectorType>(FieldTy)) |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1099 | SrcField = new BitCastInst(SrcField, |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1100 | PointerType::getUnqual(FieldIntTy), |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1101 | "", LI); |
| 1102 | SrcField = new LoadInst(SrcField, "sroa.load.elt", LI); |
| 1103 | |
| 1104 | // If SrcField is a fp or vector of the right size but that isn't an |
| 1105 | // integer type, bitcast to an integer so we can shift it. |
| 1106 | if (SrcField->getType() != FieldIntTy) |
| 1107 | SrcField = new BitCastInst(SrcField, FieldIntTy, "", LI); |
| 1108 | |
| 1109 | // Zero extend the field to be the same size as the final alloca so that |
| 1110 | // we can shift and insert it. |
| 1111 | if (SrcField->getType() != ResultVal->getType()) |
| 1112 | SrcField = new ZExtInst(SrcField, ResultVal->getType(), "", LI); |
| 1113 | |
| 1114 | // Determine the number of bits to shift SrcField. |
| 1115 | uint64_t Shift; |
| 1116 | if (Layout) // Struct case. |
| 1117 | Shift = Layout->getElementOffsetInBits(i); |
| 1118 | else // Array case. |
| 1119 | Shift = i*ArrayEltBitOffset; |
| 1120 | |
| 1121 | if (TD->isBigEndian()) |
| 1122 | Shift = AllocaSizeBits-Shift-FieldIntTy->getBitWidth(); |
| 1123 | |
| 1124 | if (Shift) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1125 | Value *ShiftVal = ConstantInt::get(SrcField->getType(), Shift); |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1126 | SrcField = BinaryOperator::CreateShl(SrcField, ShiftVal, "", LI); |
| 1127 | } |
| 1128 | |
| 1129 | ResultVal = BinaryOperator::CreateOr(SrcField, ResultVal, "", LI); |
| 1130 | } |
Eli Friedman | 41b33f4 | 2009-06-01 09:14:32 +0000 | [diff] [blame] | 1131 | |
| 1132 | // Handle tail padding by truncating the result |
| 1133 | if (TD->getTypeSizeInBits(LI->getType()) != AllocaSizeBits) |
| 1134 | ResultVal = new TruncInst(ResultVal, LI->getType(), "", LI); |
| 1135 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1136 | LI->replaceAllUsesWith(ResultVal); |
| 1137 | LI->eraseFromParent(); |
| 1138 | } |
| 1139 | |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 1140 | |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1141 | /// HasPadding - Return true if the specified type has any structure or |
| 1142 | /// alignment padding, false otherwise. |
Duncan Sands | a0fcc08 | 2008-06-04 08:21:45 +0000 | [diff] [blame] | 1143 | static bool HasPadding(const Type *Ty, const TargetData &TD) { |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1144 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 1145 | const StructLayout *SL = TD.getStructLayout(STy); |
| 1146 | unsigned PrevFieldBitOffset = 0; |
| 1147 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1148 | unsigned FieldBitOffset = SL->getElementOffsetInBits(i); |
| 1149 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1150 | // Padding in sub-elements? |
Duncan Sands | a0fcc08 | 2008-06-04 08:21:45 +0000 | [diff] [blame] | 1151 | if (HasPadding(STy->getElementType(i), TD)) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1152 | return true; |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1153 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1154 | // Check to see if there is any padding between this element and the |
| 1155 | // previous one. |
| 1156 | if (i) { |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1157 | unsigned PrevFieldEnd = |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1158 | PrevFieldBitOffset+TD.getTypeSizeInBits(STy->getElementType(i-1)); |
| 1159 | if (PrevFieldEnd < FieldBitOffset) |
| 1160 | return true; |
| 1161 | } |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1162 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1163 | PrevFieldBitOffset = FieldBitOffset; |
| 1164 | } |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1165 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1166 | // Check for tail padding. |
| 1167 | if (unsigned EltCount = STy->getNumElements()) { |
| 1168 | unsigned PrevFieldEnd = PrevFieldBitOffset + |
| 1169 | TD.getTypeSizeInBits(STy->getElementType(EltCount-1)); |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1170 | if (PrevFieldEnd < SL->getSizeInBits()) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1171 | return true; |
| 1172 | } |
| 1173 | |
| 1174 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
Duncan Sands | a0fcc08 | 2008-06-04 08:21:45 +0000 | [diff] [blame] | 1175 | return HasPadding(ATy->getElementType(), TD); |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1176 | } else if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) { |
Duncan Sands | a0fcc08 | 2008-06-04 08:21:45 +0000 | [diff] [blame] | 1177 | return HasPadding(VTy->getElementType(), TD); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1178 | } |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1179 | return TD.getTypeSizeInBits(Ty) != TD.getTypeAllocSizeInBits(Ty); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1180 | } |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 1181 | |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 1182 | /// isSafeStructAllocaToScalarRepl - Check to see if the specified allocation of |
| 1183 | /// an aggregate can be broken down into elements. Return 0 if not, 3 if safe, |
| 1184 | /// or 1 if safe after canonicalization has been performed. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1185 | int SROA::isSafeAllocaToScalarRepl(AllocaInst *AI) { |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 1186 | // Loop over the use list of the alloca. We can only transform it if all of |
| 1187 | // the users are safe to transform. |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1188 | AllocaInfo Info; |
| 1189 | |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 1190 | for (Value::use_iterator I = AI->use_begin(), E = AI->use_end(); |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 1191 | I != E; ++I) { |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1192 | isSafeUseOfAllocation(cast<Instruction>(*I), AI, Info); |
| 1193 | if (Info.isUnsafe) { |
Nick Lewycky | 5913625 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 1194 | DEBUG(errs() << "Cannot transform: " << *AI << "\n due to user: " |
| 1195 | << **I << '\n'); |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 1196 | return 0; |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 1197 | } |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 1198 | } |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1199 | |
| 1200 | // Okay, we know all the users are promotable. If the aggregate is a memcpy |
| 1201 | // source and destination, we have to be careful. In particular, the memcpy |
| 1202 | // could be moving around elements that live in structure padding of the LLVM |
| 1203 | // types, but may actually be used. In these cases, we refuse to promote the |
| 1204 | // struct. |
| 1205 | if (Info.isMemCpySrc && Info.isMemCpyDst && |
Chris Lattner | 56c3852 | 2009-01-07 06:34:28 +0000 | [diff] [blame] | 1206 | HasPadding(AI->getType()->getElementType(), *TD)) |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1207 | return 0; |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1208 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1209 | // If we require cleanup, return 1, otherwise return 3. |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 1210 | return Info.needsCleanup ? 1 : 3; |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 1213 | /// CleanupGEP - GEP is used by an Alloca, which can be prompted after the GEP |
| 1214 | /// is canonicalized here. |
| 1215 | void SROA::CleanupGEP(GetElementPtrInst *GEPI) { |
| 1216 | gep_type_iterator I = gep_type_begin(GEPI); |
| 1217 | ++I; |
| 1218 | |
Devang Patel | 7afe8fa | 2009-02-10 19:28:07 +0000 | [diff] [blame] | 1219 | const ArrayType *AT = dyn_cast<ArrayType>(*I); |
| 1220 | if (!AT) |
| 1221 | return; |
| 1222 | |
| 1223 | uint64_t NumElements = AT->getNumElements(); |
| 1224 | |
| 1225 | if (isa<ConstantInt>(I.getOperand())) |
| 1226 | return; |
| 1227 | |
| 1228 | if (NumElements == 1) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1229 | GEPI->setOperand(2, |
| 1230 | Constant::getNullValue(Type::getInt32Ty(GEPI->getContext()))); |
Devang Patel | 7afe8fa | 2009-02-10 19:28:07 +0000 | [diff] [blame] | 1231 | return; |
| 1232 | } |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 1233 | |
Devang Patel | 7afe8fa | 2009-02-10 19:28:07 +0000 | [diff] [blame] | 1234 | assert(NumElements == 2 && "Unhandled case!"); |
| 1235 | // All users of the GEP must be loads. At each use of the GEP, insert |
| 1236 | // two loads of the appropriate indexed GEP and select between them. |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 1237 | Value *IsOne = new ICmpInst(GEPI, ICmpInst::ICMP_NE, I.getOperand(), |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1238 | Constant::getNullValue(I.getOperand()->getType()), |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 1239 | "isone"); |
Devang Patel | 7afe8fa | 2009-02-10 19:28:07 +0000 | [diff] [blame] | 1240 | // Insert the new GEP instructions, which are properly indexed. |
| 1241 | SmallVector<Value*, 8> Indices(GEPI->op_begin()+1, GEPI->op_end()); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1242 | Indices[1] = Constant::getNullValue(Type::getInt32Ty(GEPI->getContext())); |
Devang Patel | 7afe8fa | 2009-02-10 19:28:07 +0000 | [diff] [blame] | 1243 | Value *ZeroIdx = GetElementPtrInst::Create(GEPI->getOperand(0), |
| 1244 | Indices.begin(), |
| 1245 | Indices.end(), |
| 1246 | GEPI->getName()+".0", GEPI); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1247 | Indices[1] = ConstantInt::get(Type::getInt32Ty(GEPI->getContext()), 1); |
Devang Patel | 7afe8fa | 2009-02-10 19:28:07 +0000 | [diff] [blame] | 1248 | Value *OneIdx = GetElementPtrInst::Create(GEPI->getOperand(0), |
| 1249 | Indices.begin(), |
| 1250 | Indices.end(), |
| 1251 | GEPI->getName()+".1", GEPI); |
| 1252 | // Replace all loads of the variable index GEP with loads from both |
| 1253 | // indexes and a select. |
| 1254 | while (!GEPI->use_empty()) { |
| 1255 | LoadInst *LI = cast<LoadInst>(GEPI->use_back()); |
| 1256 | Value *Zero = new LoadInst(ZeroIdx, LI->getName()+".0", LI); |
| 1257 | Value *One = new LoadInst(OneIdx , LI->getName()+".1", LI); |
| 1258 | Value *R = SelectInst::Create(IsOne, One, Zero, LI->getName(), LI); |
| 1259 | LI->replaceAllUsesWith(R); |
| 1260 | LI->eraseFromParent(); |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 1261 | } |
Devang Patel | 7afe8fa | 2009-02-10 19:28:07 +0000 | [diff] [blame] | 1262 | GEPI->eraseFromParent(); |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Devang Patel | 7afe8fa | 2009-02-10 19:28:07 +0000 | [diff] [blame] | 1265 | |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 1266 | /// CleanupAllocaUsers - If SROA reported that it can promote the specified |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 1267 | /// allocation, but only if cleaned up, perform the cleanups required. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1268 | void SROA::CleanupAllocaUsers(AllocaInst *AI) { |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 1269 | // At this point, we know that the end result will be SROA'd and promoted, so |
| 1270 | // we can insert ugly code if required so long as sroa+mem2reg will clean it |
| 1271 | // up. |
| 1272 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 1273 | UI != E; ) { |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 1274 | User *U = *UI++; |
| 1275 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) |
| 1276 | CleanupGEP(GEPI); |
Jay Foad | 0906b1b | 2009-06-06 17:49:35 +0000 | [diff] [blame] | 1277 | else { |
| 1278 | Instruction *I = cast<Instruction>(U); |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 1279 | SmallVector<DbgInfoIntrinsic *, 2> DbgInUses; |
Zhou Sheng | b0c4199 | 2009-03-18 12:48:48 +0000 | [diff] [blame] | 1280 | if (!isa<StoreInst>(I) && OnlyUsedByDbgInfoIntrinsics(I, &DbgInUses)) { |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 1281 | // Safe to remove debug info uses. |
| 1282 | while (!DbgInUses.empty()) { |
| 1283 | DbgInfoIntrinsic *DI = DbgInUses.back(); DbgInUses.pop_back(); |
| 1284 | DI->eraseFromParent(); |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 1285 | } |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 1286 | I->eraseFromParent(); |
Chris Lattner | d878ecd | 2004-11-14 05:00:19 +0000 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | } |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 1290 | } |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1291 | |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1292 | /// MergeInType - Add the 'In' type to the accumulated type (Accum) so far at |
| 1293 | /// the offset specified by Offset (which is specified in bytes). |
Chris Lattner | de6df88 | 2006-04-14 21:42:41 +0000 | [diff] [blame] | 1294 | /// |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1295 | /// There are two cases we handle here: |
| 1296 | /// 1) A union of vector types of the same size and potentially its elements. |
Chris Lattner | d22dbdf | 2006-12-15 07:32:38 +0000 | [diff] [blame] | 1297 | /// Here we turn element accesses into insert/extract element operations. |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1298 | /// This promotes a <4 x float> with a store of float to the third element |
| 1299 | /// into a <4 x float> that uses insert element. |
| 1300 | /// 2) A fully general blob of memory, which we turn into some (potentially |
| 1301 | /// large) integer type with extract and insert operations where the loads |
| 1302 | /// and stores would mutate the memory. |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1303 | static void MergeInType(const Type *In, uint64_t Offset, const Type *&VecTy, |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1304 | unsigned AllocaSize, const TargetData &TD, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 1305 | LLVMContext &Context) { |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1306 | // If this could be contributing to a vector, analyze it. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1307 | if (VecTy != Type::getVoidTy(Context)) { // either null or a vector type. |
Chris Lattner | 996d7a9 | 2009-02-02 18:02:59 +0000 | [diff] [blame] | 1308 | |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1309 | // If the In type is a vector that is the same size as the alloca, see if it |
| 1310 | // matches the existing VecTy. |
| 1311 | if (const VectorType *VInTy = dyn_cast<VectorType>(In)) { |
| 1312 | if (VInTy->getBitWidth()/8 == AllocaSize && Offset == 0) { |
| 1313 | // If we're storing/loading a vector of the right size, allow it as a |
| 1314 | // vector. If this the first vector we see, remember the type so that |
| 1315 | // we know the element size. |
| 1316 | if (VecTy == 0) |
| 1317 | VecTy = VInTy; |
| 1318 | return; |
| 1319 | } |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1320 | } else if (In->isFloatTy() || In->isDoubleTy() || |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1321 | (isa<IntegerType>(In) && In->getPrimitiveSizeInBits() >= 8 && |
| 1322 | isPowerOf2_32(In->getPrimitiveSizeInBits()))) { |
| 1323 | // If we're accessing something that could be an element of a vector, see |
| 1324 | // if the implied vector agrees with what we already have and if Offset is |
| 1325 | // compatible with it. |
| 1326 | unsigned EltSize = In->getPrimitiveSizeInBits()/8; |
| 1327 | if (Offset % EltSize == 0 && |
| 1328 | AllocaSize % EltSize == 0 && |
| 1329 | (VecTy == 0 || |
| 1330 | cast<VectorType>(VecTy)->getElementType() |
| 1331 | ->getPrimitiveSizeInBits()/8 == EltSize)) { |
| 1332 | if (VecTy == 0) |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1333 | VecTy = VectorType::get(In, AllocaSize/EltSize); |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1334 | return; |
| 1335 | } |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1339 | // Otherwise, we have a case that we can't handle with an optimized vector |
| 1340 | // form. We can still turn this into a large integer. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1341 | VecTy = Type::getVoidTy(Context); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1344 | /// CanConvertToScalar - V is a pointer. If we can convert the pointee and all |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1345 | /// its accesses to use a to single vector type, return true, and set VecTy to |
| 1346 | /// the new type. If we could convert the alloca into a single promotable |
| 1347 | /// integer, return true but set VecTy to VoidTy. Further, if the use is not a |
| 1348 | /// completely trivial use that mem2reg could promote, set IsNotTrivial. Offset |
| 1349 | /// is the current offset from the base of the alloca being analyzed. |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1350 | /// |
Chris Lattner | 1a3257b | 2009-02-03 18:15:05 +0000 | [diff] [blame] | 1351 | /// If we see at least one access to the value that is as a vector type, set the |
| 1352 | /// SawVec flag. |
Chris Lattner | 1a3257b | 2009-02-03 18:15:05 +0000 | [diff] [blame] | 1353 | bool SROA::CanConvertToScalar(Value *V, bool &IsNotTrivial, const Type *&VecTy, |
| 1354 | bool &SawVec, uint64_t Offset, |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1355 | unsigned AllocaSize) { |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1356 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) { |
| 1357 | Instruction *User = cast<Instruction>(*UI); |
| 1358 | |
| 1359 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1360 | // Don't break volatile loads. |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 1361 | if (LI->isVolatile()) |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1362 | return false; |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 1363 | MergeInType(LI->getType(), Offset, VecTy, |
| 1364 | AllocaSize, *TD, V->getContext()); |
Chris Lattner | 1a3257b | 2009-02-03 18:15:05 +0000 | [diff] [blame] | 1365 | SawVec |= isa<VectorType>(LI->getType()); |
Chris Lattner | cf32186 | 2009-01-07 06:39:58 +0000 | [diff] [blame] | 1366 | continue; |
| 1367 | } |
| 1368 | |
| 1369 | if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1370 | // Storing the pointer, not into the value? |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 1371 | if (SI->getOperand(0) == V || SI->isVolatile()) return 0; |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1372 | MergeInType(SI->getOperand(0)->getType(), Offset, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 1373 | VecTy, AllocaSize, *TD, V->getContext()); |
Chris Lattner | 1a3257b | 2009-02-03 18:15:05 +0000 | [diff] [blame] | 1374 | SawVec |= isa<VectorType>(SI->getOperand(0)->getType()); |
Chris Lattner | cf32186 | 2009-01-07 06:39:58 +0000 | [diff] [blame] | 1375 | continue; |
| 1376 | } |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1377 | |
| 1378 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) { |
Chris Lattner | 1a3257b | 2009-02-03 18:15:05 +0000 | [diff] [blame] | 1379 | if (!CanConvertToScalar(BCI, IsNotTrivial, VecTy, SawVec, Offset, |
| 1380 | AllocaSize)) |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1381 | return false; |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1382 | IsNotTrivial = true; |
Chris Lattner | cf32186 | 2009-01-07 06:39:58 +0000 | [diff] [blame] | 1383 | continue; |
| 1384 | } |
| 1385 | |
| 1386 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) { |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1387 | // If this is a GEP with a variable indices, we can't handle it. |
| 1388 | if (!GEP->hasAllConstantIndices()) |
| 1389 | return false; |
Chris Lattner | cf32186 | 2009-01-07 06:39:58 +0000 | [diff] [blame] | 1390 | |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1391 | // Compute the offset that this GEP adds to the pointer. |
| 1392 | SmallVector<Value*, 8> Indices(GEP->op_begin()+1, GEP->op_end()); |
| 1393 | uint64_t GEPOffset = TD->getIndexedOffset(GEP->getOperand(0)->getType(), |
| 1394 | &Indices[0], Indices.size()); |
| 1395 | // See if all uses can be converted. |
Chris Lattner | 1a3257b | 2009-02-03 18:15:05 +0000 | [diff] [blame] | 1396 | if (!CanConvertToScalar(GEP, IsNotTrivial, VecTy, SawVec,Offset+GEPOffset, |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1397 | AllocaSize)) |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1398 | return false; |
| 1399 | IsNotTrivial = true; |
| 1400 | continue; |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1401 | } |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 1402 | |
Chris Lattner | 3d730f7 | 2009-02-03 02:01:43 +0000 | [diff] [blame] | 1403 | // If this is a constant sized memset of a constant value (e.g. 0) we can |
| 1404 | // handle it. |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 1405 | if (MemSetInst *MSI = dyn_cast<MemSetInst>(User)) { |
| 1406 | // Store of constant value and constant size. |
| 1407 | if (isa<ConstantInt>(MSI->getValue()) && |
| 1408 | isa<ConstantInt>(MSI->getLength())) { |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 1409 | IsNotTrivial = true; |
| 1410 | continue; |
| 1411 | } |
Chris Lattner | 3d730f7 | 2009-02-03 02:01:43 +0000 | [diff] [blame] | 1412 | } |
Chris Lattner | c570487 | 2009-03-08 04:04:21 +0000 | [diff] [blame] | 1413 | |
| 1414 | // If this is a memcpy or memmove into or out of the whole allocation, we |
| 1415 | // can handle it like a load or store of the scalar type. |
| 1416 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(User)) { |
| 1417 | if (ConstantInt *Len = dyn_cast<ConstantInt>(MTI->getLength())) |
| 1418 | if (Len->getZExtValue() == AllocaSize && Offset == 0) { |
| 1419 | IsNotTrivial = true; |
| 1420 | continue; |
| 1421 | } |
| 1422 | } |
Chris Lattner | dfe964c | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 1423 | |
Devang Patel | 00e389c | 2009-03-06 07:03:54 +0000 | [diff] [blame] | 1424 | // Ignore dbg intrinsic. |
| 1425 | if (isa<DbgInfoIntrinsic>(User)) |
| 1426 | continue; |
| 1427 | |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1428 | // Otherwise, we cannot handle this! |
| 1429 | return false; |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1432 | return true; |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1435 | /// 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] | 1436 | /// directly. This happens when we are converting an "integer union" to a |
| 1437 | /// single integer scalar, or when we are converting a "vector union" to a |
| 1438 | /// vector with insert/extractelement instructions. |
| 1439 | /// |
| 1440 | /// Offset is an offset from the original alloca, in bits that need to be |
| 1441 | /// shifted to the right. By the end of this, there should be no uses of Ptr. |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1442 | void SROA::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset) { |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1443 | while (!Ptr->use_empty()) { |
| 1444 | Instruction *User = cast<Instruction>(Ptr->use_back()); |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1445 | |
Chris Lattner | cf32186 | 2009-01-07 06:39:58 +0000 | [diff] [blame] | 1446 | if (BitCastInst *CI = dyn_cast<BitCastInst>(User)) { |
Chris Lattner | b10e0da | 2008-01-30 00:39:15 +0000 | [diff] [blame] | 1447 | ConvertUsesToScalar(CI, NewAI, Offset); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1448 | CI->eraseFromParent(); |
Chris Lattner | cf32186 | 2009-01-07 06:39:58 +0000 | [diff] [blame] | 1449 | continue; |
| 1450 | } |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1451 | |
Chris Lattner | cf32186 | 2009-01-07 06:39:58 +0000 | [diff] [blame] | 1452 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) { |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1453 | // Compute the offset that this GEP adds to the pointer. |
| 1454 | SmallVector<Value*, 8> Indices(GEP->op_begin()+1, GEP->op_end()); |
| 1455 | uint64_t GEPOffset = TD->getIndexedOffset(GEP->getOperand(0)->getType(), |
| 1456 | &Indices[0], Indices.size()); |
| 1457 | ConvertUsesToScalar(GEP, NewAI, Offset+GEPOffset*8); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1458 | GEP->eraseFromParent(); |
Chris Lattner | cf32186 | 2009-01-07 06:39:58 +0000 | [diff] [blame] | 1459 | continue; |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1460 | } |
Chris Lattner | 3d730f7 | 2009-02-03 02:01:43 +0000 | [diff] [blame] | 1461 | |
Chris Lattner | 9bc67da | 2009-02-03 19:45:44 +0000 | [diff] [blame] | 1462 | IRBuilder<> Builder(User->getParent(), User); |
| 1463 | |
| 1464 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1465 | // The load is a bit extract from NewAI shifted right by Offset bits. |
| 1466 | Value *LoadedVal = Builder.CreateLoad(NewAI, "tmp"); |
| 1467 | Value *NewLoadVal |
| 1468 | = ConvertScalar_ExtractValue(LoadedVal, LI->getType(), Offset, Builder); |
| 1469 | LI->replaceAllUsesWith(NewLoadVal); |
Chris Lattner | 9bc67da | 2009-02-03 19:45:44 +0000 | [diff] [blame] | 1470 | LI->eraseFromParent(); |
| 1471 | continue; |
| 1472 | } |
| 1473 | |
| 1474 | if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 1475 | assert(SI->getOperand(0) != Ptr && "Consistency error!"); |
Benjamin Kramer | e6f3294 | 2009-11-29 21:17:48 +0000 | [diff] [blame] | 1476 | // FIXME: Remove once builder has Twine API. |
Bob Wilson | d614a1f | 2009-12-04 21:51:35 +0000 | [diff] [blame] | 1477 | Value *Old = Builder.CreateLoad(NewAI, |
| 1478 | (NewAI->getName()+".in").str().c_str()); |
Chris Lattner | 9bc67da | 2009-02-03 19:45:44 +0000 | [diff] [blame] | 1479 | Value *New = ConvertScalar_InsertValue(SI->getOperand(0), Old, Offset, |
| 1480 | Builder); |
| 1481 | Builder.CreateStore(New, NewAI); |
| 1482 | SI->eraseFromParent(); |
| 1483 | continue; |
| 1484 | } |
| 1485 | |
Chris Lattner | 3d730f7 | 2009-02-03 02:01:43 +0000 | [diff] [blame] | 1486 | // If this is a constant sized memset of a constant value (e.g. 0) we can |
| 1487 | // transform it into a store of the expanded constant value. |
| 1488 | if (MemSetInst *MSI = dyn_cast<MemSetInst>(User)) { |
| 1489 | assert(MSI->getRawDest() == Ptr && "Consistency error!"); |
| 1490 | unsigned NumBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue(); |
Chris Lattner | 33e24ad | 2009-04-21 16:52:12 +0000 | [diff] [blame] | 1491 | if (NumBytes != 0) { |
| 1492 | unsigned Val = cast<ConstantInt>(MSI->getValue())->getZExtValue(); |
| 1493 | |
| 1494 | // Compute the value replicated the right number of times. |
| 1495 | APInt APVal(NumBytes*8, Val); |
Chris Lattner | 3d730f7 | 2009-02-03 02:01:43 +0000 | [diff] [blame] | 1496 | |
Chris Lattner | 33e24ad | 2009-04-21 16:52:12 +0000 | [diff] [blame] | 1497 | // Splat the value if non-zero. |
| 1498 | if (Val) |
| 1499 | for (unsigned i = 1; i != NumBytes; ++i) |
| 1500 | APVal |= APVal << 8; |
Benjamin Kramer | e6f3294 | 2009-11-29 21:17:48 +0000 | [diff] [blame] | 1501 | |
| 1502 | // FIXME: Remove once builder has Twine API. |
Bob Wilson | d614a1f | 2009-12-04 21:51:35 +0000 | [diff] [blame] | 1503 | Value *Old = Builder.CreateLoad(NewAI, |
| 1504 | (NewAI->getName()+".in").str().c_str()); |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 1505 | Value *New = ConvertScalar_InsertValue( |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1506 | ConstantInt::get(User->getContext(), APVal), |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1507 | Old, Offset, Builder); |
Chris Lattner | 33e24ad | 2009-04-21 16:52:12 +0000 | [diff] [blame] | 1508 | Builder.CreateStore(New, NewAI); |
| 1509 | } |
Chris Lattner | 3d730f7 | 2009-02-03 02:01:43 +0000 | [diff] [blame] | 1510 | MSI->eraseFromParent(); |
| 1511 | continue; |
| 1512 | } |
Chris Lattner | c570487 | 2009-03-08 04:04:21 +0000 | [diff] [blame] | 1513 | |
| 1514 | // If this is a memcpy or memmove into or out of the whole allocation, we |
| 1515 | // can handle it like a load or store of the scalar type. |
| 1516 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(User)) { |
| 1517 | assert(Offset == 0 && "must be store to start of alloca"); |
| 1518 | |
| 1519 | // If the source and destination are both to the same alloca, then this is |
| 1520 | // a noop copy-to-self, just delete it. Otherwise, emit a load and store |
| 1521 | // as appropriate. |
| 1522 | AllocaInst *OrigAI = cast<AllocaInst>(Ptr->getUnderlyingObject()); |
| 1523 | |
| 1524 | if (MTI->getSource()->getUnderlyingObject() != OrigAI) { |
| 1525 | // Dest must be OrigAI, change this to be a load from the original |
| 1526 | // pointer (bitcasted), then a store to our new alloca. |
| 1527 | assert(MTI->getRawDest() == Ptr && "Neither use is of pointer?"); |
| 1528 | Value *SrcPtr = MTI->getSource(); |
| 1529 | SrcPtr = Builder.CreateBitCast(SrcPtr, NewAI->getType()); |
| 1530 | |
| 1531 | LoadInst *SrcVal = Builder.CreateLoad(SrcPtr, "srcval"); |
| 1532 | SrcVal->setAlignment(MTI->getAlignment()); |
| 1533 | Builder.CreateStore(SrcVal, NewAI); |
| 1534 | } else if (MTI->getDest()->getUnderlyingObject() != OrigAI) { |
| 1535 | // Src must be OrigAI, change this to be a load from NewAI then a store |
| 1536 | // through the original dest pointer (bitcasted). |
| 1537 | assert(MTI->getRawSource() == Ptr && "Neither use is of pointer?"); |
| 1538 | LoadInst *SrcVal = Builder.CreateLoad(NewAI, "srcval"); |
| 1539 | |
| 1540 | Value *DstPtr = Builder.CreateBitCast(MTI->getDest(), NewAI->getType()); |
| 1541 | StoreInst *NewStore = Builder.CreateStore(SrcVal, DstPtr); |
| 1542 | NewStore->setAlignment(MTI->getAlignment()); |
| 1543 | } else { |
| 1544 | // Noop transfer. Src == Dst |
| 1545 | } |
| 1546 | |
| 1547 | |
| 1548 | MTI->eraseFromParent(); |
| 1549 | continue; |
| 1550 | } |
Chris Lattner | dfe964c | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 1551 | |
Devang Patel | 00e389c | 2009-03-06 07:03:54 +0000 | [diff] [blame] | 1552 | // If user is a dbg info intrinsic then it is safe to remove it. |
| 1553 | if (isa<DbgInfoIntrinsic>(User)) { |
| 1554 | User->eraseFromParent(); |
| 1555 | continue; |
| 1556 | } |
| 1557 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1558 | llvm_unreachable("Unsupported operation!"); |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1559 | } |
| 1560 | } |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1561 | |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1562 | /// ConvertScalar_ExtractValue - Extract a value of type ToType from an integer |
| 1563 | /// or vector value FromVal, extracting the bits from the offset specified by |
| 1564 | /// Offset. This returns the value, which is of type ToType. |
| 1565 | /// |
| 1566 | /// This happens when we are converting an "integer union" to a single |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1567 | /// integer scalar, or when we are converting a "vector union" to a vector with |
| 1568 | /// insert/extractelement instructions. |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1569 | /// |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1570 | /// Offset is an offset from the original alloca, in bits that need to be |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1571 | /// shifted to the right. |
| 1572 | Value *SROA::ConvertScalar_ExtractValue(Value *FromVal, const Type *ToType, |
| 1573 | uint64_t Offset, IRBuilder<> &Builder) { |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1574 | // If the load is of the whole new alloca, no conversion is needed. |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1575 | if (FromVal->getType() == ToType && Offset == 0) |
| 1576 | return FromVal; |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1577 | |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1578 | // If the result alloca is a vector type, this is either an element |
| 1579 | // access or a bitcast to another vector type of the same size. |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1580 | if (const VectorType *VTy = dyn_cast<VectorType>(FromVal->getType())) { |
| 1581 | if (isa<VectorType>(ToType)) |
| 1582 | return Builder.CreateBitCast(FromVal, ToType, "tmp"); |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1583 | |
| 1584 | // Otherwise it must be an element access. |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1585 | unsigned Elt = 0; |
| 1586 | if (Offset) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1587 | unsigned EltSize = TD->getTypeAllocSizeInBits(VTy->getElementType()); |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1588 | Elt = Offset/EltSize; |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1589 | assert(EltSize*Elt == Offset && "Invalid modulus in validity checking"); |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1590 | } |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1591 | // Return the element extracted out of it. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1592 | Value *V = Builder.CreateExtractElement(FromVal, ConstantInt::get( |
| 1593 | Type::getInt32Ty(FromVal->getContext()), Elt), "tmp"); |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1594 | if (V->getType() != ToType) |
| 1595 | V = Builder.CreateBitCast(V, ToType, "tmp"); |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1596 | return V; |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1597 | } |
Chris Lattner | 1aa7056 | 2009-02-03 21:08:45 +0000 | [diff] [blame] | 1598 | |
| 1599 | // If ToType is a first class aggregate, extract out each of the pieces and |
| 1600 | // use insertvalue's to form the FCA. |
| 1601 | if (const StructType *ST = dyn_cast<StructType>(ToType)) { |
| 1602 | const StructLayout &Layout = *TD->getStructLayout(ST); |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1603 | Value *Res = UndefValue::get(ST); |
Chris Lattner | 1aa7056 | 2009-02-03 21:08:45 +0000 | [diff] [blame] | 1604 | for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { |
| 1605 | Value *Elt = ConvertScalar_ExtractValue(FromVal, ST->getElementType(i), |
Chris Lattner | e991ced | 2009-02-06 04:34:07 +0000 | [diff] [blame] | 1606 | Offset+Layout.getElementOffsetInBits(i), |
Chris Lattner | 1aa7056 | 2009-02-03 21:08:45 +0000 | [diff] [blame] | 1607 | Builder); |
| 1608 | Res = Builder.CreateInsertValue(Res, Elt, i, "tmp"); |
| 1609 | } |
| 1610 | return Res; |
| 1611 | } |
| 1612 | |
| 1613 | if (const ArrayType *AT = dyn_cast<ArrayType>(ToType)) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1614 | uint64_t EltSize = TD->getTypeAllocSizeInBits(AT->getElementType()); |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1615 | Value *Res = UndefValue::get(AT); |
Chris Lattner | 1aa7056 | 2009-02-03 21:08:45 +0000 | [diff] [blame] | 1616 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
| 1617 | Value *Elt = ConvertScalar_ExtractValue(FromVal, AT->getElementType(), |
| 1618 | Offset+i*EltSize, Builder); |
| 1619 | Res = Builder.CreateInsertValue(Res, Elt, i, "tmp"); |
| 1620 | } |
| 1621 | return Res; |
| 1622 | } |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1623 | |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1624 | // Otherwise, this must be a union that was converted to an integer value. |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1625 | const IntegerType *NTy = cast<IntegerType>(FromVal->getType()); |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1626 | |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1627 | // If this is a big-endian system and the load is narrower than the |
| 1628 | // full alloca type, we need to do a shift to get the right bits. |
| 1629 | int ShAmt = 0; |
Chris Lattner | 56c3852 | 2009-01-07 06:34:28 +0000 | [diff] [blame] | 1630 | if (TD->isBigEndian()) { |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1631 | // On big-endian machines, the lowest bit is stored at the bit offset |
| 1632 | // from the pointer given by getTypeStoreSizeInBits. This matters for |
| 1633 | // integers with a bitwidth that is not a multiple of 8. |
Chris Lattner | 56c3852 | 2009-01-07 06:34:28 +0000 | [diff] [blame] | 1634 | ShAmt = TD->getTypeStoreSizeInBits(NTy) - |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1635 | TD->getTypeStoreSizeInBits(ToType) - Offset; |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1636 | } else { |
| 1637 | ShAmt = Offset; |
| 1638 | } |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1639 | |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1640 | // Note: we support negative bitwidths (with shl) which are not defined. |
| 1641 | // We do this to support (f.e.) loads off the end of a structure where |
| 1642 | // only some bits are used. |
| 1643 | if (ShAmt > 0 && (unsigned)ShAmt < NTy->getBitWidth()) |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1644 | FromVal = Builder.CreateLShr(FromVal, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1645 | ConstantInt::get(FromVal->getType(), |
Chris Lattner | 1aa7056 | 2009-02-03 21:08:45 +0000 | [diff] [blame] | 1646 | ShAmt), "tmp"); |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1647 | else if (ShAmt < 0 && (unsigned)-ShAmt < NTy->getBitWidth()) |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1648 | FromVal = Builder.CreateShl(FromVal, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1649 | ConstantInt::get(FromVal->getType(), |
Chris Lattner | 1aa7056 | 2009-02-03 21:08:45 +0000 | [diff] [blame] | 1650 | -ShAmt), "tmp"); |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1651 | |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1652 | // Finally, unconditionally truncate the integer to the right width. |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1653 | unsigned LIBitWidth = TD->getTypeSizeInBits(ToType); |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1654 | if (LIBitWidth < NTy->getBitWidth()) |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1655 | FromVal = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1656 | Builder.CreateTrunc(FromVal, IntegerType::get(FromVal->getContext(), |
| 1657 | LIBitWidth), "tmp"); |
Chris Lattner | 55a683d | 2009-02-03 07:08:57 +0000 | [diff] [blame] | 1658 | else if (LIBitWidth > NTy->getBitWidth()) |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1659 | FromVal = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1660 | Builder.CreateZExt(FromVal, IntegerType::get(FromVal->getContext(), |
| 1661 | LIBitWidth), "tmp"); |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1662 | |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1663 | // If the result is an integer, this is a trunc or bitcast. |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1664 | if (isa<IntegerType>(ToType)) { |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1665 | // Should be done. |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1666 | } else if (ToType->isFloatingPoint() || isa<VectorType>(ToType)) { |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1667 | // Just do a bitcast, we know the sizes match up. |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1668 | FromVal = Builder.CreateBitCast(FromVal, ToType, "tmp"); |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1669 | } else { |
Chris Lattner | 9d34c4d | 2008-02-29 07:12:06 +0000 | [diff] [blame] | 1670 | // Otherwise must be a pointer. |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1671 | FromVal = Builder.CreateIntToPtr(FromVal, ToType, "tmp"); |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1672 | } |
Chris Lattner | 6e01115 | 2009-02-03 21:01:03 +0000 | [diff] [blame] | 1673 | assert(FromVal->getType() == ToType && "Didn't convert right?"); |
| 1674 | return FromVal; |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Chris Lattner | 9b872db | 2009-02-03 19:30:11 +0000 | [diff] [blame] | 1677 | /// ConvertScalar_InsertValue - Insert the value "SV" into the existing integer |
| 1678 | /// or vector value "Old" at the offset specified by Offset. |
| 1679 | /// |
| 1680 | /// This happens when we are converting an "integer union" to a |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1681 | /// single integer scalar, or when we are converting a "vector union" to a |
| 1682 | /// vector with insert/extractelement instructions. |
| 1683 | /// |
| 1684 | /// Offset is an offset from the original alloca, in bits that need to be |
Chris Lattner | 9b872db | 2009-02-03 19:30:11 +0000 | [diff] [blame] | 1685 | /// shifted to the right. |
| 1686 | Value *SROA::ConvertScalar_InsertValue(Value *SV, Value *Old, |
Chris Lattner | 65a6502 | 2009-02-03 19:41:50 +0000 | [diff] [blame] | 1687 | uint64_t Offset, IRBuilder<> &Builder) { |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1688 | |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1689 | // Convert the stored type to the actual type, shift it left to insert |
| 1690 | // then 'or' into place. |
Chris Lattner | 9b872db | 2009-02-03 19:30:11 +0000 | [diff] [blame] | 1691 | const Type *AllocaType = Old->getType(); |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 1692 | LLVMContext &Context = Old->getContext(); |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1693 | |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1694 | if (const VectorType *VTy = dyn_cast<VectorType>(AllocaType)) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1695 | uint64_t VecSize = TD->getTypeAllocSizeInBits(VTy); |
| 1696 | uint64_t ValSize = TD->getTypeAllocSizeInBits(SV->getType()); |
Chris Lattner | 29e6417 | 2009-03-08 04:17:04 +0000 | [diff] [blame] | 1697 | |
| 1698 | // Changing the whole vector with memset or with an access of a different |
| 1699 | // vector type? |
| 1700 | if (ValSize == VecSize) |
| 1701 | return Builder.CreateBitCast(SV, AllocaType, "tmp"); |
| 1702 | |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1703 | uint64_t EltSize = TD->getTypeAllocSizeInBits(VTy->getElementType()); |
Chris Lattner | 29e6417 | 2009-03-08 04:17:04 +0000 | [diff] [blame] | 1704 | |
| 1705 | // Must be an element insertion. |
| 1706 | unsigned Elt = Offset/EltSize; |
| 1707 | |
| 1708 | if (SV->getType() != VTy->getElementType()) |
| 1709 | SV = Builder.CreateBitCast(SV, VTy->getElementType(), "tmp"); |
| 1710 | |
| 1711 | SV = Builder.CreateInsertElement(Old, SV, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1712 | ConstantInt::get(Type::getInt32Ty(SV->getContext()), Elt), |
Chris Lattner | 29e6417 | 2009-03-08 04:17:04 +0000 | [diff] [blame] | 1713 | "tmp"); |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1714 | return SV; |
| 1715 | } |
Chris Lattner | 9b872db | 2009-02-03 19:30:11 +0000 | [diff] [blame] | 1716 | |
| 1717 | // If SV is a first-class aggregate value, insert each value recursively. |
| 1718 | if (const StructType *ST = dyn_cast<StructType>(SV->getType())) { |
| 1719 | const StructLayout &Layout = *TD->getStructLayout(ST); |
| 1720 | for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { |
Chris Lattner | 65a6502 | 2009-02-03 19:41:50 +0000 | [diff] [blame] | 1721 | Value *Elt = Builder.CreateExtractValue(SV, i, "tmp"); |
Chris Lattner | 9b872db | 2009-02-03 19:30:11 +0000 | [diff] [blame] | 1722 | Old = ConvertScalar_InsertValue(Elt, Old, |
Chris Lattner | e991ced | 2009-02-06 04:34:07 +0000 | [diff] [blame] | 1723 | Offset+Layout.getElementOffsetInBits(i), |
Chris Lattner | 65a6502 | 2009-02-03 19:41:50 +0000 | [diff] [blame] | 1724 | Builder); |
Chris Lattner | 9b872db | 2009-02-03 19:30:11 +0000 | [diff] [blame] | 1725 | } |
| 1726 | return Old; |
| 1727 | } |
| 1728 | |
| 1729 | if (const ArrayType *AT = dyn_cast<ArrayType>(SV->getType())) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1730 | uint64_t EltSize = TD->getTypeAllocSizeInBits(AT->getElementType()); |
Chris Lattner | 9b872db | 2009-02-03 19:30:11 +0000 | [diff] [blame] | 1731 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
Chris Lattner | 65a6502 | 2009-02-03 19:41:50 +0000 | [diff] [blame] | 1732 | Value *Elt = Builder.CreateExtractValue(SV, i, "tmp"); |
| 1733 | Old = ConvertScalar_InsertValue(Elt, Old, Offset+i*EltSize, Builder); |
Chris Lattner | 9b872db | 2009-02-03 19:30:11 +0000 | [diff] [blame] | 1734 | } |
| 1735 | return Old; |
| 1736 | } |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1737 | |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1738 | // If SV is a float, convert it to the appropriate integer type. |
Chris Lattner | 9b872db | 2009-02-03 19:30:11 +0000 | [diff] [blame] | 1739 | // If it is a pointer, do the same. |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1740 | unsigned SrcWidth = TD->getTypeSizeInBits(SV->getType()); |
| 1741 | unsigned DestWidth = TD->getTypeSizeInBits(AllocaType); |
| 1742 | unsigned SrcStoreWidth = TD->getTypeStoreSizeInBits(SV->getType()); |
| 1743 | unsigned DestStoreWidth = TD->getTypeStoreSizeInBits(AllocaType); |
| 1744 | if (SV->getType()->isFloatingPoint() || isa<VectorType>(SV->getType())) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1745 | SV = Builder.CreateBitCast(SV, |
| 1746 | IntegerType::get(SV->getContext(),SrcWidth), "tmp"); |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1747 | else if (isa<PointerType>(SV->getType())) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1748 | SV = Builder.CreatePtrToInt(SV, TD->getIntPtrType(SV->getContext()), "tmp"); |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1749 | |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1750 | // Zero extend or truncate the value if needed. |
| 1751 | if (SV->getType() != AllocaType) { |
| 1752 | if (SV->getType()->getPrimitiveSizeInBits() < |
| 1753 | AllocaType->getPrimitiveSizeInBits()) |
Chris Lattner | 65a6502 | 2009-02-03 19:41:50 +0000 | [diff] [blame] | 1754 | SV = Builder.CreateZExt(SV, AllocaType, "tmp"); |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1755 | else { |
| 1756 | // Truncation may be needed if storing more than the alloca can hold |
| 1757 | // (undefined behavior). |
Chris Lattner | 65a6502 | 2009-02-03 19:41:50 +0000 | [diff] [blame] | 1758 | SV = Builder.CreateTrunc(SV, AllocaType, "tmp"); |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1759 | SrcWidth = DestWidth; |
| 1760 | SrcStoreWidth = DestStoreWidth; |
| 1761 | } |
| 1762 | } |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1763 | |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1764 | // If this is a big-endian system and the store is narrower than the |
| 1765 | // full alloca type, we need to do a shift to get the right bits. |
| 1766 | int ShAmt = 0; |
| 1767 | if (TD->isBigEndian()) { |
| 1768 | // On big-endian machines, the lowest bit is stored at the bit offset |
| 1769 | // from the pointer given by getTypeStoreSizeInBits. This matters for |
| 1770 | // integers with a bitwidth that is not a multiple of 8. |
| 1771 | ShAmt = DestStoreWidth - SrcStoreWidth - Offset; |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1772 | } else { |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1773 | ShAmt = Offset; |
| 1774 | } |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1775 | |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1776 | // Note: we support negative bitwidths (with shr) which are not defined. |
| 1777 | // We do this to support (f.e.) stores off the end of a structure where |
| 1778 | // only some bits in the structure are set. |
| 1779 | APInt Mask(APInt::getLowBitsSet(DestWidth, SrcWidth)); |
| 1780 | if (ShAmt > 0 && (unsigned)ShAmt < DestWidth) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1781 | SV = Builder.CreateShl(SV, ConstantInt::get(SV->getType(), |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1782 | ShAmt), "tmp"); |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1783 | Mask <<= ShAmt; |
| 1784 | } else if (ShAmt < 0 && (unsigned)-ShAmt < DestWidth) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1785 | SV = Builder.CreateLShr(SV, ConstantInt::get(SV->getType(), |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1786 | -ShAmt), "tmp"); |
Duncan Sands | 0e7c46b | 2009-02-02 09:53:14 +0000 | [diff] [blame] | 1787 | Mask = Mask.lshr(-ShAmt); |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1788 | } |
Duncan Sands | 4b3dfbd | 2009-02-02 10:06:20 +0000 | [diff] [blame] | 1789 | |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1790 | // Mask out the bits we are about to insert from the old value, and or |
| 1791 | // in the new bits. |
| 1792 | if (SrcWidth != DestWidth) { |
| 1793 | assert(DestWidth > SrcWidth); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1794 | Old = Builder.CreateAnd(Old, ConstantInt::get(Context, ~Mask), "mask"); |
Chris Lattner | 65a6502 | 2009-02-03 19:41:50 +0000 | [diff] [blame] | 1795 | SV = Builder.CreateOr(Old, SV, "ins"); |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1796 | } |
| 1797 | return SV; |
| 1798 | } |
| 1799 | |
| 1800 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1801 | |
| 1802 | /// PointsToConstantGlobal - Return true if V (possibly indirectly) points to |
| 1803 | /// some part of a constant global variable. This intentionally only accepts |
| 1804 | /// constant expressions because we don't can't rewrite arbitrary instructions. |
| 1805 | static bool PointsToConstantGlobal(Value *V) { |
| 1806 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 1807 | return GV->isConstant(); |
| 1808 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 1809 | if (CE->getOpcode() == Instruction::BitCast || |
| 1810 | CE->getOpcode() == Instruction::GetElementPtr) |
| 1811 | return PointsToConstantGlobal(CE->getOperand(0)); |
| 1812 | return false; |
| 1813 | } |
| 1814 | |
| 1815 | /// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived) |
| 1816 | /// pointer to an alloca. Ignore any reads of the pointer, return false if we |
| 1817 | /// see any stores or other unknown uses. If we see pointer arithmetic, keep |
| 1818 | /// track of whether it moves the pointer (with isOffset) but otherwise traverse |
| 1819 | /// the uses. If we see a memcpy/memmove that targets an unoffseted pointer to |
| 1820 | /// the alloca, and if the source pointer is a pointer to a constant global, we |
| 1821 | /// can optimize this. |
| 1822 | static bool isOnlyCopiedFromConstantGlobal(Value *V, Instruction *&TheCopy, |
| 1823 | bool isOffset) { |
| 1824 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) { |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 1825 | if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) |
| 1826 | // Ignore non-volatile loads, they are always ok. |
| 1827 | if (!LI->isVolatile()) |
| 1828 | continue; |
| 1829 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1830 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI)) { |
| 1831 | // If uses of the bitcast are ok, we are ok. |
| 1832 | if (!isOnlyCopiedFromConstantGlobal(BCI, TheCopy, isOffset)) |
| 1833 | return false; |
| 1834 | continue; |
| 1835 | } |
| 1836 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) { |
| 1837 | // If the GEP has all zero indices, it doesn't offset the pointer. If it |
| 1838 | // doesn't, it does. |
| 1839 | if (!isOnlyCopiedFromConstantGlobal(GEP, TheCopy, |
| 1840 | isOffset || !GEP->hasAllZeroIndices())) |
| 1841 | return false; |
| 1842 | continue; |
| 1843 | } |
| 1844 | |
| 1845 | // If this is isn't our memcpy/memmove, reject it as something we can't |
| 1846 | // handle. |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 1847 | if (!isa<MemTransferInst>(*UI)) |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1848 | return false; |
| 1849 | |
| 1850 | // If we already have seen a copy, reject the second one. |
| 1851 | if (TheCopy) return false; |
| 1852 | |
| 1853 | // If the pointer has been offset from the start of the alloca, we can't |
| 1854 | // safely handle this. |
| 1855 | if (isOffset) return false; |
| 1856 | |
| 1857 | // If the memintrinsic isn't using the alloca as the dest, reject it. |
| 1858 | if (UI.getOperandNo() != 1) return false; |
| 1859 | |
| 1860 | MemIntrinsic *MI = cast<MemIntrinsic>(*UI); |
| 1861 | |
| 1862 | // If the source of the memcpy/move is not a constant global, reject it. |
| 1863 | if (!PointsToConstantGlobal(MI->getOperand(2))) |
| 1864 | return false; |
| 1865 | |
| 1866 | // Otherwise, the transform is safe. Remember the copy instruction. |
| 1867 | TheCopy = MI; |
| 1868 | } |
| 1869 | return true; |
| 1870 | } |
| 1871 | |
| 1872 | /// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only |
| 1873 | /// modified by a copy from a constant global. If we can prove this, we can |
| 1874 | /// replace any uses of the alloca with uses of the global directly. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1875 | Instruction *SROA::isOnlyCopiedFromConstantGlobal(AllocaInst *AI) { |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1876 | Instruction *TheCopy = 0; |
| 1877 | if (::isOnlyCopiedFromConstantGlobal(AI, TheCopy, false)) |
| 1878 | return TheCopy; |
| 1879 | return 0; |
| 1880 | } |