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