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