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