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 | // |
Chad Rosier | c9f27ee | 2012-04-11 19:21:58 +0000 | [diff] [blame] | 16 | // This combines a simple SRoA algorithm with the Mem2Reg algorithm because they |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 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" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 25 | #include "llvm/DIBuilder.h" |
Bill Wendling | 0bcbd1d | 2012-06-28 00:05:13 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo.h" |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 27 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 28 | #include "llvm/Function.h" |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 29 | #include "llvm/GlobalVariable.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 30 | #include "llvm/IRBuilder.h" |
Misha Brukman | d8e1eea | 2004-07-29 17:05:13 +0000 | [diff] [blame] | 31 | #include "llvm/Instructions.h" |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 32 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 33 | #include "llvm/LLVMContext.h" |
Chris Lattner | 72eaa0e | 2010-09-01 23:09:27 +0000 | [diff] [blame] | 34 | #include "llvm/Module.h" |
Peter Collingbourne | 9012c57 | 2012-05-19 22:52:10 +0000 | [diff] [blame] | 35 | #include "llvm/Operator.h" |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 36 | #include "llvm/Pass.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SetVector.h" |
| 38 | #include "llvm/ADT/SmallVector.h" |
| 39 | #include "llvm/ADT/Statistic.h" |
Cameron Zwarich | b1686c3 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/Loads.h" |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | a9be1df | 2010-11-18 06:26:49 +0000 | [diff] [blame] | 43 | #include "llvm/Support/CallSite.h" |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 45 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 46 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 47 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 48 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 49 | #include "llvm/Target/TargetData.h" |
| 50 | #include "llvm/Transforms/Utils/Local.h" |
| 51 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
| 52 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Chris Lattner | d866473 | 2003-12-02 17:43:55 +0000 | [diff] [blame] | 53 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 55 | STATISTIC(NumReplaced, "Number of allocas broken up"); |
| 56 | STATISTIC(NumPromoted, "Number of allocas promoted"); |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 57 | STATISTIC(NumAdjusted, "Number of scalar allocas adjusted to allow promotion"); |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 58 | STATISTIC(NumConverted, "Number of aggregates converted to scalar"); |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 59 | STATISTIC(NumGlobals, "Number of allocas copied from constant global"); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 61 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 62 | struct SROA : public FunctionPass { |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 63 | SROA(int T, bool hasDT, char &ID, int ST, int AT, int SLT) |
Cameron Zwarich | b1686c3 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 64 | : FunctionPass(ID), HasDomTree(hasDT) { |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 65 | if (T == -1) |
Chris Lattner | b0e71ed | 2007-08-02 21:33:36 +0000 | [diff] [blame] | 66 | SRThreshold = 128; |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 67 | else |
| 68 | SRThreshold = T; |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 69 | if (ST == -1) |
| 70 | StructMemberThreshold = 32; |
| 71 | else |
| 72 | StructMemberThreshold = ST; |
| 73 | if (AT == -1) |
| 74 | ArrayElementThreshold = 8; |
| 75 | else |
| 76 | ArrayElementThreshold = AT; |
| 77 | if (SLT == -1) |
| 78 | // Do not limit the scalar integer load size if no threshold is given. |
| 79 | ScalarLoadThreshold = -1; |
| 80 | else |
| 81 | ScalarLoadThreshold = SLT; |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 82 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 83 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 84 | bool runOnFunction(Function &F); |
| 85 | |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 86 | bool performScalarRepl(Function &F); |
| 87 | bool performPromotion(Function &F); |
| 88 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 89 | private: |
Cameron Zwarich | b1686c3 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 90 | bool HasDomTree; |
Chris Lattner | 56c3852 | 2009-01-07 06:34:28 +0000 | [diff] [blame] | 91 | TargetData *TD; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 92 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 93 | /// DeadInsts - Keep track of instructions we have made dead, so that |
| 94 | /// we can remove them after we are done working. |
| 95 | SmallVector<Value*, 32> DeadInsts; |
| 96 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 97 | /// AllocaInfo - When analyzing uses of an alloca instruction, this captures |
| 98 | /// information about the uses. All these fields are initialized to false |
| 99 | /// and set to true when something is learned. |
| 100 | struct AllocaInfo { |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 101 | /// The alloca to promote. |
| 102 | AllocaInst *AI; |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 104 | /// CheckedPHIs - This is a set of verified PHI nodes, to prevent infinite |
| 105 | /// looping and avoid redundant work. |
| 106 | SmallPtrSet<PHINode*, 8> CheckedPHIs; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 108 | /// isUnsafe - This is set to true if the alloca cannot be SROA'd. |
| 109 | bool isUnsafe : 1; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 111 | /// isMemCpySrc - This is true if this aggregate is memcpy'd from. |
| 112 | bool isMemCpySrc : 1; |
| 113 | |
Zhou Sheng | 33b0b8d | 2007-07-06 06:01:16 +0000 | [diff] [blame] | 114 | /// isMemCpyDst - This is true if this aggregate is memcpy'd into. |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 115 | bool isMemCpyDst : 1; |
| 116 | |
Chris Lattner | 7e9b427 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 117 | /// hasSubelementAccess - This is true if a subelement of the alloca is |
| 118 | /// ever accessed, or false if the alloca is only accessed with mem |
| 119 | /// intrinsics or load/store that only access the entire alloca at once. |
| 120 | bool hasSubelementAccess : 1; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 7e9b427 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 122 | /// hasALoadOrStore - This is true if there are any loads or stores to it. |
| 123 | /// The alloca may just be accessed with memcpy, for example, which would |
| 124 | /// not set this. |
| 125 | bool hasALoadOrStore : 1; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 127 | explicit AllocaInfo(AllocaInst *ai) |
| 128 | : AI(ai), isUnsafe(false), isMemCpySrc(false), isMemCpyDst(false), |
Chris Lattner | 7e9b427 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 129 | hasSubelementAccess(false), hasALoadOrStore(false) {} |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 130 | }; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 131 | |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 132 | /// SRThreshold - The maximum alloca size to considered for SROA. |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 133 | unsigned SRThreshold; |
| 134 | |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 135 | /// StructMemberThreshold - The maximum number of members a struct can |
| 136 | /// contain to be considered for SROA. |
| 137 | unsigned StructMemberThreshold; |
| 138 | |
| 139 | /// ArrayElementThreshold - The maximum number of elements an array can |
| 140 | /// have to be considered for SROA. |
| 141 | unsigned ArrayElementThreshold; |
| 142 | |
| 143 | /// ScalarLoadThreshold - The maximum size in bits of scalars to load when |
| 144 | /// converting to scalar |
| 145 | unsigned ScalarLoadThreshold; |
| 146 | |
Chris Lattner | d01a0da | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 147 | void MarkUnsafe(AllocaInfo &I, Instruction *User) { |
| 148 | I.isUnsafe = true; |
| 149 | DEBUG(dbgs() << " Transformation preventing inst: " << *User << '\n'); |
| 150 | } |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 151 | |
Victor Hernandez | 6c146ee | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 152 | bool isSafeAllocaToScalarRepl(AllocaInst *AI); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 154 | void isSafeForScalarRepl(Instruction *I, uint64_t Offset, AllocaInfo &Info); |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 155 | void isSafePHISelectUseForScalarRepl(Instruction *User, uint64_t Offset, |
| 156 | AllocaInfo &Info); |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 157 | void isSafeGEP(GetElementPtrInst *GEPI, uint64_t &Offset, AllocaInfo &Info); |
| 158 | void isSafeMemAccess(uint64_t Offset, uint64_t MemSize, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 159 | Type *MemOpType, bool isStore, AllocaInfo &Info, |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 160 | Instruction *TheAccess, bool AllowWholeAccess); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 161 | bool TypeHasComponent(Type *T, uint64_t Offset, uint64_t Size); |
| 162 | uint64_t FindElementAndOffset(Type *&T, uint64_t &Offset, |
| 163 | Type *&IdxTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 164 | |
| 165 | void DoScalarReplacement(AllocaInst *AI, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 166 | std::vector<AllocaInst*> &WorkList); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 167 | void DeleteDeadInstructions(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 168 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 169 | void RewriteForScalarRepl(Instruction *I, AllocaInst *AI, uint64_t Offset, |
| 170 | SmallVector<AllocaInst*, 32> &NewElts); |
| 171 | void RewriteBitCast(BitCastInst *BC, AllocaInst *AI, uint64_t Offset, |
| 172 | SmallVector<AllocaInst*, 32> &NewElts); |
| 173 | void RewriteGEP(GetElementPtrInst *GEPI, AllocaInst *AI, uint64_t Offset, |
| 174 | SmallVector<AllocaInst*, 32> &NewElts); |
Nick Lewycky | 5a1cb64 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 175 | void RewriteLifetimeIntrinsic(IntrinsicInst *II, AllocaInst *AI, |
| 176 | uint64_t Offset, |
| 177 | SmallVector<AllocaInst*, 32> &NewElts); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 178 | void RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *Inst, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 179 | AllocaInst *AI, |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 180 | SmallVector<AllocaInst*, 32> &NewElts); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 181 | void RewriteStoreUserOfWholeAlloca(StoreInst *SI, AllocaInst *AI, |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 182 | SmallVector<AllocaInst*, 32> &NewElts); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 183 | void RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocaInst *AI, |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 184 | SmallVector<AllocaInst*, 32> &NewElts); |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 185 | bool ShouldAttemptScalarRepl(AllocaInst *AI); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 186 | |
Nick Lewycky | 9174d5c | 2011-06-27 05:40:02 +0000 | [diff] [blame] | 187 | static MemTransferInst *isOnlyCopiedFromConstantGlobal( |
| 188 | AllocaInst *AI, SmallVector<Instruction*, 4> &ToDelete); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 189 | }; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 190 | |
Cameron Zwarich | b1686c3 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 191 | // SROA_DT - SROA that uses DominatorTree. |
| 192 | struct SROA_DT : public SROA { |
Chris Lattner | b352d6e | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 193 | static char ID; |
| 194 | public: |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 195 | SROA_DT(int T = -1, int ST = -1, int AT = -1, int SLT = -1) : |
| 196 | SROA(T, true, ID, ST, AT, SLT) { |
Cameron Zwarich | b1686c3 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 197 | initializeSROA_DTPass(*PassRegistry::getPassRegistry()); |
Chris Lattner | b352d6e | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 198 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 199 | |
Chris Lattner | b352d6e | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 200 | // getAnalysisUsage - This pass does not require any passes, but we know it |
| 201 | // will not alter the CFG, so say so. |
| 202 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 203 | AU.addRequired<DominatorTree>(); |
Chris Lattner | b352d6e | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 204 | AU.setPreservesCFG(); |
| 205 | } |
| 206 | }; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 207 | |
Chris Lattner | b352d6e | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 208 | // SROA_SSAUp - SROA that uses SSAUpdater. |
| 209 | struct SROA_SSAUp : public SROA { |
| 210 | static char ID; |
| 211 | public: |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 212 | SROA_SSAUp(int T = -1, int ST = -1, int AT = -1, int SLT = -1) : |
| 213 | SROA(T, false, ID, ST, AT, SLT) { |
Chris Lattner | b352d6e | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 214 | initializeSROA_SSAUpPass(*PassRegistry::getPassRegistry()); |
| 215 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 216 | |
Chris Lattner | b352d6e | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 217 | // getAnalysisUsage - This pass does not require any passes, but we know it |
| 218 | // will not alter the CFG, so say so. |
| 219 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 220 | AU.setPreservesCFG(); |
| 221 | } |
| 222 | }; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 223 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Cameron Zwarich | b1686c3 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 226 | char SROA_DT::ID = 0; |
Chris Lattner | b352d6e | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 227 | char SROA_SSAUp::ID = 0; |
| 228 | |
Cameron Zwarich | b1686c3 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 229 | INITIALIZE_PASS_BEGIN(SROA_DT, "scalarrepl", |
| 230 | "Scalar Replacement of Aggregates (DT)", false, false) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 231 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
Cameron Zwarich | b1686c3 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 232 | INITIALIZE_PASS_END(SROA_DT, "scalarrepl", |
| 233 | "Scalar Replacement of Aggregates (DT)", false, false) |
Chris Lattner | b352d6e | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 234 | |
| 235 | INITIALIZE_PASS_BEGIN(SROA_SSAUp, "scalarrepl-ssa", |
| 236 | "Scalar Replacement of Aggregates (SSAUp)", false, false) |
| 237 | INITIALIZE_PASS_END(SROA_SSAUp, "scalarrepl-ssa", |
| 238 | "Scalar Replacement of Aggregates (SSAUp)", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 239 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 240 | // Public interface to the ScalarReplAggregates pass |
Chris Lattner | b352d6e | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 241 | FunctionPass *llvm::createScalarReplAggregatesPass(int Threshold, |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 242 | bool UseDomTree, |
| 243 | int StructMemberThreshold, |
| 244 | int ArrayElementThreshold, |
| 245 | int ScalarLoadThreshold) { |
Cameron Zwarich | b1686c3 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 246 | if (UseDomTree) |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 247 | return new SROA_DT(Threshold, StructMemberThreshold, ArrayElementThreshold, |
| 248 | ScalarLoadThreshold); |
| 249 | return new SROA_SSAUp(Threshold, StructMemberThreshold, |
| 250 | ArrayElementThreshold, ScalarLoadThreshold); |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 251 | } |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 252 | |
| 253 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 254 | //===----------------------------------------------------------------------===// |
| 255 | // Convert To Scalar Optimization. |
| 256 | //===----------------------------------------------------------------------===// |
| 257 | |
| 258 | namespace { |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 259 | /// ConvertToScalarInfo - This class implements the "Convert To Scalar" |
| 260 | /// optimization, which scans the uses of an alloca and determines if it can |
| 261 | /// rewrite it in terms of a single new alloca that can be mem2reg'd. |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 262 | class ConvertToScalarInfo { |
Cameron Zwarich | d4c9c3e | 2011-03-16 00:13:35 +0000 | [diff] [blame] | 263 | /// AllocaSize - The size of the alloca being considered in bytes. |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 264 | unsigned AllocaSize; |
| 265 | const TargetData &TD; |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 266 | unsigned ScalarLoadThreshold; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 267 | |
Chris Lattner | a0bada7 | 2010-04-16 02:32:17 +0000 | [diff] [blame] | 268 | /// IsNotTrivial - This is set to true if there is some access to the object |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 269 | /// which means that mem2reg can't promote it. |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 270 | bool IsNotTrivial; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 271 | |
Cameron Zwarich | deb74f2 | 2011-06-13 21:44:35 +0000 | [diff] [blame] | 272 | /// ScalarKind - Tracks the kind of alloca being considered for promotion, |
| 273 | /// computed based on the uses of the alloca rather than the LLVM type system. |
| 274 | enum { |
| 275 | Unknown, |
Cameron Zwarich | 5179782 | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 276 | |
Cameron Zwarich | 15cd80c | 2011-06-13 23:39:23 +0000 | [diff] [blame] | 277 | // Accesses via GEPs that are consistent with element access of a vector |
Cameron Zwarich | 5179782 | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 278 | // type. This will not be converted into a vector unless there is a later |
| 279 | // access using an actual vector type. |
| 280 | ImplicitVector, |
| 281 | |
Cameron Zwarich | 15cd80c | 2011-06-13 23:39:23 +0000 | [diff] [blame] | 282 | // Accesses via vector operations and GEPs that are consistent with the |
| 283 | // layout of a vector type. |
Cameron Zwarich | deb74f2 | 2011-06-13 21:44:35 +0000 | [diff] [blame] | 284 | Vector, |
Cameron Zwarich | 5179782 | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 285 | |
| 286 | // An integer bag-of-bits with bitwise operations for insertion and |
| 287 | // extraction. Any combination of types can be converted into this kind |
| 288 | // of scalar. |
Cameron Zwarich | deb74f2 | 2011-06-13 21:44:35 +0000 | [diff] [blame] | 289 | Integer |
| 290 | } ScalarKind; |
| 291 | |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 292 | /// VectorTy - This tracks the type that we should promote the vector to if |
| 293 | /// it is possible to turn it into a vector. This starts out null, and if it |
| 294 | /// isn't possible to turn into a vector type, it gets set to VoidTy. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 295 | VectorType *VectorTy; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 296 | |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 297 | /// HadNonMemTransferAccess - True if there is at least one access to the |
Cameron Zwarich | 1bcdb6f | 2011-03-16 08:13:42 +0000 | [diff] [blame] | 298 | /// alloca that is not a MemTransferInst. We don't want to turn structs into |
| 299 | /// large integers unless there is some potential for optimization. |
Cameron Zwarich | 85b0f46 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 300 | bool HadNonMemTransferAccess; |
| 301 | |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 302 | /// HadDynamicAccess - True if some element of this alloca was dynamic. |
| 303 | /// We don't yet have support for turning a dynamic access into a large |
| 304 | /// integer. |
| 305 | bool HadDynamicAccess; |
| 306 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 307 | public: |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 308 | explicit ConvertToScalarInfo(unsigned Size, const TargetData &td, |
| 309 | unsigned SLT) |
| 310 | : AllocaSize(Size), TD(td), ScalarLoadThreshold(SLT), IsNotTrivial(false), |
| 311 | ScalarKind(Unknown), VectorTy(0), HadNonMemTransferAccess(false), |
| 312 | HadDynamicAccess(false) { } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 313 | |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 314 | AllocaInst *TryConvert(AllocaInst *AI); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 316 | private: |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 317 | bool CanConvertToScalar(Value *V, uint64_t Offset, Value* NonConstantIdx); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 318 | void MergeInTypeForLoadOrStore(Type *In, uint64_t Offset); |
| 319 | bool MergeInVectorType(VectorType *VInTy, uint64_t Offset); |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 320 | void ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset, |
| 321 | Value *NonConstantIdx); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 322 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 323 | Value *ConvertScalar_ExtractValue(Value *NV, Type *ToType, |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 324 | uint64_t Offset, Value* NonConstantIdx, |
| 325 | IRBuilder<> &Builder); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 326 | Value *ConvertScalar_InsertValue(Value *StoredVal, Value *ExistingVal, |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 327 | uint64_t Offset, Value* NonConstantIdx, |
| 328 | IRBuilder<> &Builder); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 329 | }; |
| 330 | } // end anonymous namespace. |
| 331 | |
Chris Lattner | 91abace | 2010-09-01 05:14:33 +0000 | [diff] [blame] | 332 | |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 333 | /// TryConvert - Analyze the specified alloca, and if it is safe to do so, |
| 334 | /// rewrite it to be a new alloca which is mem2reg'able. This returns the new |
| 335 | /// alloca if possible or null if not. |
| 336 | AllocaInst *ConvertToScalarInfo::TryConvert(AllocaInst *AI) { |
| 337 | // If we can't convert this scalar, or if mem2reg can trivially do it, bail |
| 338 | // out. |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 339 | if (!CanConvertToScalar(AI, 0, 0) || !IsNotTrivial) |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 340 | return 0; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 341 | |
Cameron Zwarich | 5179782 | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 342 | // If an alloca has only memset / memcpy uses, it may still have an Unknown |
| 343 | // ScalarKind. Treat it as an Integer below. |
| 344 | if (ScalarKind == Unknown) |
| 345 | ScalarKind = Integer; |
| 346 | |
Cameron Zwarich | 3ebb05d | 2011-06-18 06:17:51 +0000 | [diff] [blame] | 347 | if (ScalarKind == Vector && VectorTy->getBitWidth() != AllocaSize * 8) |
| 348 | ScalarKind = Integer; |
| 349 | |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 350 | // If we were able to find a vector type that can handle this with |
| 351 | // insert/extract elements, and if there was at least one use that had |
| 352 | // a vector type, promote this to a vector. We don't want to promote |
| 353 | // random stuff that doesn't use vectors (e.g. <9 x double>) because then |
| 354 | // we just get a lot of insert/extracts. If at least one vector is |
| 355 | // involved, then we probably really do have a union of vector/array. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 356 | Type *NewTy; |
Cameron Zwarich | 5b93d3c | 2011-06-14 06:33:51 +0000 | [diff] [blame] | 357 | if (ScalarKind == Vector) { |
| 358 | assert(VectorTy && "Missing type for vector scalar."); |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 359 | DEBUG(dbgs() << "CONVERT TO VECTOR: " << *AI << "\n TYPE = " |
| 360 | << *VectorTy << '\n'); |
| 361 | NewTy = VectorTy; // Use the vector type. |
| 362 | } else { |
Cameron Zwarich | 85b0f46 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 363 | unsigned BitWidth = AllocaSize * 8; |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 364 | |
| 365 | // Do not convert to scalar integer if the alloca size exceeds the |
| 366 | // scalar load threshold. |
| 367 | if (BitWidth > ScalarLoadThreshold) |
| 368 | return 0; |
| 369 | |
Cameron Zwarich | 5179782 | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 370 | if ((ScalarKind == ImplicitVector || ScalarKind == Integer) && |
| 371 | !HadNonMemTransferAccess && !TD.fitsInLegalInteger(BitWidth)) |
Cameron Zwarich | 85b0f46 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 372 | return 0; |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 373 | // Dynamic accesses on integers aren't yet supported. They need us to shift |
| 374 | // by a dynamic amount which could be difficult to work out as we might not |
| 375 | // know whether to use a left or right shift. |
| 376 | if (ScalarKind == Integer && HadDynamicAccess) |
| 377 | return 0; |
Cameron Zwarich | 85b0f46 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 378 | |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 379 | DEBUG(dbgs() << "CONVERT TO SCALAR INTEGER: " << *AI << "\n"); |
| 380 | // Create and insert the integer alloca. |
Cameron Zwarich | 85b0f46 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 381 | NewTy = IntegerType::get(AI->getContext(), BitWidth); |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 382 | } |
| 383 | AllocaInst *NewAI = new AllocaInst(NewTy, 0, "", AI->getParent()->begin()); |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 384 | ConvertUsesToScalar(AI, NewAI, 0, 0); |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 385 | return NewAI; |
| 386 | } |
| 387 | |
Cameron Zwarich | c0e2607 | 2011-06-13 21:44:43 +0000 | [diff] [blame] | 388 | /// MergeInTypeForLoadOrStore - Add the 'In' type to the accumulated vector type |
| 389 | /// (VectorTy) so far at the offset specified by Offset (which is specified in |
| 390 | /// bytes). |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 391 | /// |
Cameron Zwarich | 446d952 | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 392 | /// There are two cases we handle here: |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 393 | /// 1) A union of vector types of the same size and potentially its elements. |
| 394 | /// Here we turn element accesses into insert/extract element operations. |
| 395 | /// This promotes a <4 x float> with a store of float to the third element |
| 396 | /// into a <4 x float> that uses insert element. |
Cameron Zwarich | 446d952 | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 397 | /// 2) A fully general blob of memory, which we turn into some (potentially |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 398 | /// large) integer type with extract and insert operations where the loads |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 399 | /// and stores would mutate the memory. We mark this by setting VectorTy |
| 400 | /// to VoidTy. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 401 | void ConvertToScalarInfo::MergeInTypeForLoadOrStore(Type *In, |
Cameron Zwarich | c0e2607 | 2011-06-13 21:44:43 +0000 | [diff] [blame] | 402 | uint64_t Offset) { |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 403 | // If we already decided to turn this into a blob of integer memory, there is |
| 404 | // nothing to be done. |
Cameron Zwarich | deb74f2 | 2011-06-13 21:44:35 +0000 | [diff] [blame] | 405 | if (ScalarKind == Integer) |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 406 | return; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 407 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 408 | // If this could be contributing to a vector, analyze it. |
| 409 | |
| 410 | // If the In type is a vector that is the same size as the alloca, see if it |
| 411 | // matches the existing VecTy. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 412 | if (VectorType *VInTy = dyn_cast<VectorType>(In)) { |
Cameron Zwarich | c9ecd14 | 2011-03-09 05:43:01 +0000 | [diff] [blame] | 413 | if (MergeInVectorType(VInTy, Offset)) |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 414 | return; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 415 | } else if (In->isFloatTy() || In->isDoubleTy() || |
| 416 | (In->isIntegerTy() && In->getPrimitiveSizeInBits() >= 8 && |
| 417 | isPowerOf2_32(In->getPrimitiveSizeInBits()))) { |
Cameron Zwarich | 9827b78 | 2011-03-29 05:19:52 +0000 | [diff] [blame] | 418 | // Full width accesses can be ignored, because they can always be turned |
| 419 | // into bitcasts. |
| 420 | unsigned EltSize = In->getPrimitiveSizeInBits()/8; |
Cameron Zwarich | dd68912 | 2011-06-13 21:44:31 +0000 | [diff] [blame] | 421 | if (EltSize == AllocaSize) |
Cameron Zwarich | 9827b78 | 2011-03-29 05:19:52 +0000 | [diff] [blame] | 422 | return; |
Cameron Zwarich | 5fc1282 | 2011-04-20 21:48:16 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 424 | // If we're accessing something that could be an element of a vector, see |
| 425 | // if the implied vector agrees with what we already have and if Offset is |
| 426 | // compatible with it. |
Cameron Zwarich | 96cc1d0 | 2011-06-09 01:45:33 +0000 | [diff] [blame] | 427 | if (Offset % EltSize == 0 && AllocaSize % EltSize == 0 && |
Cameron Zwarich | 446d952 | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 428 | (!VectorTy || EltSize == VectorTy->getElementType() |
| 429 | ->getPrimitiveSizeInBits()/8)) { |
Cameron Zwarich | 5fc1282 | 2011-04-20 21:48:16 +0000 | [diff] [blame] | 430 | if (!VectorTy) { |
Cameron Zwarich | 5179782 | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 431 | ScalarKind = ImplicitVector; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 432 | VectorTy = VectorType::get(In, AllocaSize/EltSize); |
Cameron Zwarich | 5fc1282 | 2011-04-20 21:48:16 +0000 | [diff] [blame] | 433 | } |
Cameron Zwarich | 446d952 | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 434 | return; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 437 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 438 | // Otherwise, we have a case that we can't handle with an optimized vector |
| 439 | // form. We can still turn this into a large integer. |
Cameron Zwarich | deb74f2 | 2011-06-13 21:44:35 +0000 | [diff] [blame] | 440 | ScalarKind = Integer; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Cameron Zwarich | c0e2607 | 2011-06-13 21:44:43 +0000 | [diff] [blame] | 443 | /// MergeInVectorType - Handles the vector case of MergeInTypeForLoadOrStore, |
| 444 | /// returning true if the type was successfully merged and false otherwise. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 445 | bool ConvertToScalarInfo::MergeInVectorType(VectorType *VInTy, |
Cameron Zwarich | c9ecd14 | 2011-03-09 05:43:01 +0000 | [diff] [blame] | 446 | uint64_t Offset) { |
Cameron Zwarich | 446d952 | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 447 | if (VInTy->getBitWidth()/8 == AllocaSize && Offset == 0) { |
| 448 | // If we're storing/loading a vector of the right size, allow it as a |
| 449 | // vector. If this the first vector we see, remember the type so that |
| 450 | // we know the element size. If this is a subsequent access, ignore it |
| 451 | // even if it is a differing type but the same size. Worst case we can |
| 452 | // bitcast the resultant vectors. |
| 453 | if (!VectorTy) |
| 454 | VectorTy = VInTy; |
Cameron Zwarich | 5179782 | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 455 | ScalarKind = Vector; |
Cameron Zwarich | b2fd770 | 2011-03-09 05:43:05 +0000 | [diff] [blame] | 456 | return true; |
Cameron Zwarich | 5179782 | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 457 | } |
Cameron Zwarich | b2fd770 | 2011-03-09 05:43:05 +0000 | [diff] [blame] | 458 | |
Cameron Zwarich | 446d952 | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 459 | return false; |
Cameron Zwarich | c9ecd14 | 2011-03-09 05:43:01 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 462 | /// CanConvertToScalar - V is a pointer. If we can convert the pointee and all |
| 463 | /// its accesses to a single vector type, return true and set VecTy to |
| 464 | /// the new type. If we could convert the alloca into a single promotable |
| 465 | /// integer, return true but set VecTy to VoidTy. Further, if the use is not a |
| 466 | /// completely trivial use that mem2reg could promote, set IsNotTrivial. Offset |
| 467 | /// is the current offset from the base of the alloca being analyzed. |
| 468 | /// |
| 469 | /// If we see at least one access to the value that is as a vector type, set the |
| 470 | /// SawVec flag. |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 471 | bool ConvertToScalarInfo::CanConvertToScalar(Value *V, uint64_t Offset, |
| 472 | Value* NonConstantIdx) { |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 473 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) { |
| 474 | Instruction *User = cast<Instruction>(*UI); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 475 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 476 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
| 477 | // Don't break volatile loads. |
Eli Friedman | 2bc3d52 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 478 | if (!LI->isSimple()) |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 479 | return false; |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 480 | // Don't touch MMX operations. |
| 481 | if (LI->getType()->isX86_MMXTy()) |
| 482 | return false; |
Cameron Zwarich | 85b0f46 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 483 | HadNonMemTransferAccess = true; |
Cameron Zwarich | c0e2607 | 2011-06-13 21:44:43 +0000 | [diff] [blame] | 484 | MergeInTypeForLoadOrStore(LI->getType(), Offset); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 485 | continue; |
| 486 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 488 | if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 489 | // Storing the pointer, not into the value? |
Eli Friedman | 2bc3d52 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 490 | if (SI->getOperand(0) == V || !SI->isSimple()) return false; |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 491 | // Don't touch MMX operations. |
| 492 | if (SI->getOperand(0)->getType()->isX86_MMXTy()) |
| 493 | return false; |
Cameron Zwarich | 85b0f46 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 494 | HadNonMemTransferAccess = true; |
Cameron Zwarich | c0e2607 | 2011-06-13 21:44:43 +0000 | [diff] [blame] | 495 | MergeInTypeForLoadOrStore(SI->getOperand(0)->getType(), Offset); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 496 | continue; |
| 497 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 498 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 499 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) { |
Nick Lewycky | 5a1cb64 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 500 | if (!onlyUsedByLifetimeMarkers(BCI)) |
| 501 | IsNotTrivial = true; // Can't be mem2reg'd. |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 502 | if (!CanConvertToScalar(BCI, Offset, NonConstantIdx)) |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 503 | return false; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 504 | continue; |
| 505 | } |
| 506 | |
| 507 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) { |
| 508 | // If this is a GEP with a variable indices, we can't handle it. |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 509 | PointerType* PtrTy = dyn_cast<PointerType>(GEP->getPointerOperandType()); |
| 510 | if (!PtrTy) |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 511 | return false; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 512 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 513 | // Compute the offset that this GEP adds to the pointer. |
| 514 | SmallVector<Value*, 8> Indices(GEP->op_begin()+1, GEP->op_end()); |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 515 | Value *GEPNonConstantIdx = 0; |
| 516 | if (!GEP->hasAllConstantIndices()) { |
| 517 | if (!isa<VectorType>(PtrTy->getElementType())) |
| 518 | return false; |
| 519 | if (NonConstantIdx) |
| 520 | return false; |
| 521 | GEPNonConstantIdx = Indices.pop_back_val(); |
| 522 | if (!GEPNonConstantIdx->getType()->isIntegerTy(32)) |
| 523 | return false; |
| 524 | HadDynamicAccess = true; |
| 525 | } else |
| 526 | GEPNonConstantIdx = NonConstantIdx; |
| 527 | uint64_t GEPOffset = TD.getIndexedOffset(PtrTy, |
Jay Foad | 8fbbb39 | 2011-07-19 14:01:37 +0000 | [diff] [blame] | 528 | Indices); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 529 | // See if all uses can be converted. |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 530 | if (!CanConvertToScalar(GEP, Offset+GEPOffset, GEPNonConstantIdx)) |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 531 | return false; |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 532 | IsNotTrivial = true; // Can't be mem2reg'd. |
Cameron Zwarich | 85b0f46 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 533 | HadNonMemTransferAccess = true; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 534 | continue; |
| 535 | } |
| 536 | |
| 537 | // If this is a constant sized memset of a constant value (e.g. 0) we can |
| 538 | // handle it. |
| 539 | if (MemSetInst *MSI = dyn_cast<MemSetInst>(User)) { |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 540 | // Store to dynamic index. |
| 541 | if (NonConstantIdx) |
| 542 | return false; |
Cameron Zwarich | 6be41eb | 2011-06-18 05:47:49 +0000 | [diff] [blame] | 543 | // Store of constant value. |
| 544 | if (!isa<ConstantInt>(MSI->getValue())) |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 545 | return false; |
Cameron Zwarich | 6be41eb | 2011-06-18 05:47:49 +0000 | [diff] [blame] | 546 | |
| 547 | // Store of constant size. |
| 548 | ConstantInt *Len = dyn_cast<ConstantInt>(MSI->getLength()); |
| 549 | if (!Len) |
| 550 | return false; |
| 551 | |
| 552 | // If the size differs from the alloca, we can only convert the alloca to |
| 553 | // an integer bag-of-bits. |
| 554 | // FIXME: This should handle all of the cases that are currently accepted |
| 555 | // as vector element insertions. |
| 556 | if (Len->getZExtValue() != AllocaSize || Offset != 0) |
| 557 | ScalarKind = Integer; |
| 558 | |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 559 | IsNotTrivial = true; // Can't be mem2reg'd. |
Cameron Zwarich | 85b0f46 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 560 | HadNonMemTransferAccess = true; |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 561 | continue; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | // If this is a memcpy or memmove into or out of the whole allocation, we |
| 565 | // can handle it like a load or store of the scalar type. |
| 566 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(User)) { |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 567 | // Store to dynamic index. |
| 568 | if (NonConstantIdx) |
| 569 | return false; |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 570 | ConstantInt *Len = dyn_cast<ConstantInt>(MTI->getLength()); |
| 571 | if (Len == 0 || Len->getZExtValue() != AllocaSize || Offset != 0) |
| 572 | return false; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 573 | |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 574 | IsNotTrivial = true; // Can't be mem2reg'd. |
| 575 | continue; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 576 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 577 | |
Nick Lewycky | 5a1cb64 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 578 | // If this is a lifetime intrinsic, we can handle it. |
| 579 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(User)) { |
| 580 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 581 | II->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 582 | continue; |
| 583 | } |
| 584 | } |
| 585 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 586 | // Otherwise, we cannot handle this! |
| 587 | return false; |
| 588 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 590 | return true; |
| 591 | } |
| 592 | |
| 593 | /// ConvertUsesToScalar - Convert all of the users of Ptr to use the new alloca |
| 594 | /// directly. This happens when we are converting an "integer union" to a |
| 595 | /// single integer scalar, or when we are converting a "vector union" to a |
| 596 | /// vector with insert/extractelement instructions. |
| 597 | /// |
| 598 | /// Offset is an offset from the original alloca, in bits that need to be |
| 599 | /// shifted to the right. By the end of this, there should be no uses of Ptr. |
| 600 | void ConvertToScalarInfo::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 601 | uint64_t Offset, |
| 602 | Value* NonConstantIdx) { |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 603 | while (!Ptr->use_empty()) { |
| 604 | Instruction *User = cast<Instruction>(Ptr->use_back()); |
| 605 | |
| 606 | if (BitCastInst *CI = dyn_cast<BitCastInst>(User)) { |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 607 | ConvertUsesToScalar(CI, NewAI, Offset, NonConstantIdx); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 608 | CI->eraseFromParent(); |
| 609 | continue; |
| 610 | } |
| 611 | |
| 612 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) { |
| 613 | // Compute the offset that this GEP adds to the pointer. |
| 614 | SmallVector<Value*, 8> Indices(GEP->op_begin()+1, GEP->op_end()); |
Pete Cooper | 3eeba88 | 2012-08-10 03:26:36 +0000 | [diff] [blame^] | 615 | Value* GEPNonConstantIdx = 0; |
| 616 | if (!GEP->hasAllConstantIndices()) { |
| 617 | assert(!NonConstantIdx && |
| 618 | "Dynamic GEP reading from dynamic GEP unsupported"); |
| 619 | GEPNonConstantIdx = Indices.pop_back_val(); |
| 620 | } else |
| 621 | GEPNonConstantIdx = NonConstantIdx; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 622 | uint64_t GEPOffset = TD.getIndexedOffset(GEP->getPointerOperandType(), |
Jay Foad | 8fbbb39 | 2011-07-19 14:01:37 +0000 | [diff] [blame] | 623 | Indices); |
Pete Cooper | 3eeba88 | 2012-08-10 03:26:36 +0000 | [diff] [blame^] | 624 | ConvertUsesToScalar(GEP, NewAI, Offset+GEPOffset*8, GEPNonConstantIdx); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 625 | GEP->eraseFromParent(); |
| 626 | continue; |
| 627 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 628 | |
Chris Lattner | 61db1f5 | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 629 | IRBuilder<> Builder(User); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 631 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
| 632 | // The load is a bit extract from NewAI shifted right by Offset bits. |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 633 | Value *LoadedVal = Builder.CreateLoad(NewAI); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 634 | Value *NewLoadVal |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 635 | = ConvertScalar_ExtractValue(LoadedVal, LI->getType(), Offset, |
| 636 | NonConstantIdx, Builder); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 637 | LI->replaceAllUsesWith(NewLoadVal); |
| 638 | LI->eraseFromParent(); |
| 639 | continue; |
| 640 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 641 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 642 | if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 643 | assert(SI->getOperand(0) != Ptr && "Consistency error!"); |
| 644 | Instruction *Old = Builder.CreateLoad(NewAI, NewAI->getName()+".in"); |
| 645 | Value *New = ConvertScalar_InsertValue(SI->getOperand(0), Old, Offset, |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 646 | NonConstantIdx, Builder); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 647 | Builder.CreateStore(New, NewAI); |
| 648 | SI->eraseFromParent(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 649 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 650 | // If the load we just inserted is now dead, then the inserted store |
| 651 | // overwrote the entire thing. |
| 652 | if (Old->use_empty()) |
| 653 | Old->eraseFromParent(); |
| 654 | continue; |
| 655 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 657 | // If this is a constant sized memset of a constant value (e.g. 0) we can |
| 658 | // transform it into a store of the expanded constant value. |
| 659 | if (MemSetInst *MSI = dyn_cast<MemSetInst>(User)) { |
| 660 | assert(MSI->getRawDest() == Ptr && "Consistency error!"); |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 661 | assert(!NonConstantIdx && "Cannot replace dynamic memset with insert"); |
Duncan Sands | 01b305f | 2012-03-23 08:29:04 +0000 | [diff] [blame] | 662 | int64_t SNumBytes = cast<ConstantInt>(MSI->getLength())->getSExtValue(); |
Chris Lattner | 1fe6bfc | 2012-03-22 03:46:58 +0000 | [diff] [blame] | 663 | if (SNumBytes > 0 && (SNumBytes >> 32) == 0) { |
Aaron Ballman | 7e2fa31 | 2012-03-15 00:05:31 +0000 | [diff] [blame] | 664 | unsigned NumBytes = static_cast<unsigned>(SNumBytes); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 665 | unsigned Val = cast<ConstantInt>(MSI->getValue())->getZExtValue(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 666 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 667 | // Compute the value replicated the right number of times. |
| 668 | APInt APVal(NumBytes*8, Val); |
| 669 | |
| 670 | // Splat the value if non-zero. |
| 671 | if (Val) |
| 672 | for (unsigned i = 1; i != NumBytes; ++i) |
| 673 | APVal |= APVal << 8; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 674 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 675 | Instruction *Old = Builder.CreateLoad(NewAI, NewAI->getName()+".in"); |
| 676 | Value *New = ConvertScalar_InsertValue( |
| 677 | ConstantInt::get(User->getContext(), APVal), |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 678 | Old, Offset, 0, Builder); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 679 | Builder.CreateStore(New, NewAI); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 680 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 681 | // If the load we just inserted is now dead, then the memset overwrote |
| 682 | // the entire thing. |
| 683 | if (Old->use_empty()) |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 684 | Old->eraseFromParent(); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 685 | } |
| 686 | MSI->eraseFromParent(); |
| 687 | continue; |
| 688 | } |
| 689 | |
| 690 | // If this is a memcpy or memmove into or out of the whole allocation, we |
| 691 | // can handle it like a load or store of the scalar type. |
| 692 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(User)) { |
| 693 | assert(Offset == 0 && "must be store to start of alloca"); |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 694 | assert(!NonConstantIdx && "Cannot replace dynamic transfer with insert"); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 695 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 696 | // If the source and destination are both to the same alloca, then this is |
| 697 | // a noop copy-to-self, just delete it. Otherwise, emit a load and store |
| 698 | // as appropriate. |
Dan Gohman | bd1801b | 2011-01-24 18:53:32 +0000 | [diff] [blame] | 699 | AllocaInst *OrigAI = cast<AllocaInst>(GetUnderlyingObject(Ptr, &TD, 0)); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 700 | |
Dan Gohman | bd1801b | 2011-01-24 18:53:32 +0000 | [diff] [blame] | 701 | if (GetUnderlyingObject(MTI->getSource(), &TD, 0) != OrigAI) { |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 702 | // Dest must be OrigAI, change this to be a load from the original |
| 703 | // pointer (bitcasted), then a store to our new alloca. |
| 704 | assert(MTI->getRawDest() == Ptr && "Neither use is of pointer?"); |
| 705 | Value *SrcPtr = MTI->getSource(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 706 | PointerType* SPTy = cast<PointerType>(SrcPtr->getType()); |
| 707 | PointerType* AIPTy = cast<PointerType>(NewAI->getType()); |
Mon P Wang | e90a633 | 2010-12-23 01:41:32 +0000 | [diff] [blame] | 708 | if (SPTy->getAddressSpace() != AIPTy->getAddressSpace()) { |
| 709 | AIPTy = PointerType::get(AIPTy->getElementType(), |
| 710 | SPTy->getAddressSpace()); |
| 711 | } |
| 712 | SrcPtr = Builder.CreateBitCast(SrcPtr, AIPTy); |
| 713 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 714 | LoadInst *SrcVal = Builder.CreateLoad(SrcPtr, "srcval"); |
| 715 | SrcVal->setAlignment(MTI->getAlignment()); |
| 716 | Builder.CreateStore(SrcVal, NewAI); |
Dan Gohman | bd1801b | 2011-01-24 18:53:32 +0000 | [diff] [blame] | 717 | } else if (GetUnderlyingObject(MTI->getDest(), &TD, 0) != OrigAI) { |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 718 | // Src must be OrigAI, change this to be a load from NewAI then a store |
| 719 | // through the original dest pointer (bitcasted). |
| 720 | assert(MTI->getRawSource() == Ptr && "Neither use is of pointer?"); |
| 721 | LoadInst *SrcVal = Builder.CreateLoad(NewAI, "srcval"); |
| 722 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 723 | PointerType* DPTy = cast<PointerType>(MTI->getDest()->getType()); |
| 724 | PointerType* AIPTy = cast<PointerType>(NewAI->getType()); |
Mon P Wang | e90a633 | 2010-12-23 01:41:32 +0000 | [diff] [blame] | 725 | if (DPTy->getAddressSpace() != AIPTy->getAddressSpace()) { |
| 726 | AIPTy = PointerType::get(AIPTy->getElementType(), |
| 727 | DPTy->getAddressSpace()); |
| 728 | } |
| 729 | Value *DstPtr = Builder.CreateBitCast(MTI->getDest(), AIPTy); |
| 730 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 731 | StoreInst *NewStore = Builder.CreateStore(SrcVal, DstPtr); |
| 732 | NewStore->setAlignment(MTI->getAlignment()); |
| 733 | } else { |
| 734 | // Noop transfer. Src == Dst |
| 735 | } |
| 736 | |
| 737 | MTI->eraseFromParent(); |
| 738 | continue; |
| 739 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 740 | |
Nick Lewycky | 5a1cb64 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 741 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(User)) { |
| 742 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 743 | II->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 744 | // There's no need to preserve these, as the resulting alloca will be |
| 745 | // converted to a register anyways. |
| 746 | II->eraseFromParent(); |
| 747 | continue; |
| 748 | } |
| 749 | } |
| 750 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 751 | llvm_unreachable("Unsupported operation!"); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | /// ConvertScalar_ExtractValue - Extract a value of type ToType from an integer |
| 756 | /// or vector value FromVal, extracting the bits from the offset specified by |
| 757 | /// Offset. This returns the value, which is of type ToType. |
| 758 | /// |
| 759 | /// This happens when we are converting an "integer union" to a single |
| 760 | /// integer scalar, or when we are converting a "vector union" to a vector with |
| 761 | /// insert/extractelement instructions. |
| 762 | /// |
| 763 | /// Offset is an offset from the original alloca, in bits that need to be |
| 764 | /// shifted to the right. |
| 765 | Value *ConvertToScalarInfo:: |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 766 | ConvertScalar_ExtractValue(Value *FromVal, Type *ToType, |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 767 | uint64_t Offset, Value* NonConstantIdx, |
| 768 | IRBuilder<> &Builder) { |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 769 | // If the load is of the whole new alloca, no conversion is needed. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 770 | Type *FromType = FromVal->getType(); |
Mon P Wang | be0761c | 2011-04-13 21:40:02 +0000 | [diff] [blame] | 771 | if (FromType == ToType && Offset == 0) |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 772 | return FromVal; |
| 773 | |
| 774 | // If the result alloca is a vector type, this is either an element |
| 775 | // access or a bitcast to another vector type of the same size. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 776 | if (VectorType *VTy = dyn_cast<VectorType>(FromType)) { |
Cameron Zwarich | 0398d61 | 2011-06-08 22:08:31 +0000 | [diff] [blame] | 777 | unsigned FromTypeSize = TD.getTypeAllocSize(FromType); |
Cameron Zwarich | 9827b78 | 2011-03-29 05:19:52 +0000 | [diff] [blame] | 778 | unsigned ToTypeSize = TD.getTypeAllocSize(ToType); |
Cameron Zwarich | 446d952 | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 779 | if (FromTypeSize == ToTypeSize) |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 780 | return Builder.CreateBitCast(FromVal, ToType); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 781 | |
| 782 | // Otherwise it must be an element access. |
| 783 | unsigned Elt = 0; |
| 784 | if (Offset) { |
| 785 | unsigned EltSize = TD.getTypeAllocSizeInBits(VTy->getElementType()); |
| 786 | Elt = Offset/EltSize; |
| 787 | assert(EltSize*Elt == Offset && "Invalid modulus in validity checking"); |
| 788 | } |
| 789 | // Return the element extracted out of it. |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 790 | Value *Idx; |
| 791 | if (NonConstantIdx) { |
| 792 | if (Elt) |
| 793 | Idx = Builder.CreateAdd(NonConstantIdx, |
| 794 | Builder.getInt32(Elt), |
| 795 | "dyn.offset"); |
| 796 | else |
| 797 | Idx = NonConstantIdx; |
| 798 | } else |
| 799 | Idx = Builder.getInt32(Elt); |
| 800 | Value *V = Builder.CreateExtractElement(FromVal, Idx); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 801 | if (V->getType() != ToType) |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 802 | V = Builder.CreateBitCast(V, ToType); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 803 | return V; |
| 804 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 805 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 806 | // If ToType is a first class aggregate, extract out each of the pieces and |
| 807 | // use insertvalue's to form the FCA. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 808 | if (StructType *ST = dyn_cast<StructType>(ToType)) { |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 809 | assert(!NonConstantIdx && |
| 810 | "Dynamic indexing into struct types not supported"); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 811 | const StructLayout &Layout = *TD.getStructLayout(ST); |
| 812 | Value *Res = UndefValue::get(ST); |
| 813 | for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { |
| 814 | Value *Elt = ConvertScalar_ExtractValue(FromVal, ST->getElementType(i), |
| 815 | Offset+Layout.getElementOffsetInBits(i), |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 816 | 0, Builder); |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 817 | Res = Builder.CreateInsertValue(Res, Elt, i); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 818 | } |
| 819 | return Res; |
| 820 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 821 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 822 | if (ArrayType *AT = dyn_cast<ArrayType>(ToType)) { |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 823 | assert(!NonConstantIdx && |
| 824 | "Dynamic indexing into array types not supported"); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 825 | uint64_t EltSize = TD.getTypeAllocSizeInBits(AT->getElementType()); |
| 826 | Value *Res = UndefValue::get(AT); |
| 827 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
| 828 | Value *Elt = ConvertScalar_ExtractValue(FromVal, AT->getElementType(), |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 829 | Offset+i*EltSize, 0, Builder); |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 830 | Res = Builder.CreateInsertValue(Res, Elt, i); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 831 | } |
| 832 | return Res; |
| 833 | } |
| 834 | |
| 835 | // Otherwise, this must be a union that was converted to an integer value. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 836 | IntegerType *NTy = cast<IntegerType>(FromVal->getType()); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 837 | |
| 838 | // If this is a big-endian system and the load is narrower than the |
| 839 | // full alloca type, we need to do a shift to get the right bits. |
| 840 | int ShAmt = 0; |
| 841 | if (TD.isBigEndian()) { |
| 842 | // On big-endian machines, the lowest bit is stored at the bit offset |
| 843 | // from the pointer given by getTypeStoreSizeInBits. This matters for |
| 844 | // integers with a bitwidth that is not a multiple of 8. |
| 845 | ShAmt = TD.getTypeStoreSizeInBits(NTy) - |
| 846 | TD.getTypeStoreSizeInBits(ToType) - Offset; |
| 847 | } else { |
| 848 | ShAmt = Offset; |
| 849 | } |
| 850 | |
| 851 | // Note: we support negative bitwidths (with shl) which are not defined. |
| 852 | // We do this to support (f.e.) loads off the end of a structure where |
| 853 | // only some bits are used. |
| 854 | if (ShAmt > 0 && (unsigned)ShAmt < NTy->getBitWidth()) |
| 855 | FromVal = Builder.CreateLShr(FromVal, |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 856 | ConstantInt::get(FromVal->getType(), ShAmt)); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 857 | else if (ShAmt < 0 && (unsigned)-ShAmt < NTy->getBitWidth()) |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 858 | FromVal = Builder.CreateShl(FromVal, |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 859 | ConstantInt::get(FromVal->getType(), -ShAmt)); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 860 | |
| 861 | // Finally, unconditionally truncate the integer to the right width. |
| 862 | unsigned LIBitWidth = TD.getTypeSizeInBits(ToType); |
| 863 | if (LIBitWidth < NTy->getBitWidth()) |
| 864 | FromVal = |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 865 | Builder.CreateTrunc(FromVal, IntegerType::get(FromVal->getContext(), |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 866 | LIBitWidth)); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 867 | else if (LIBitWidth > NTy->getBitWidth()) |
| 868 | FromVal = |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 869 | Builder.CreateZExt(FromVal, IntegerType::get(FromVal->getContext(), |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 870 | LIBitWidth)); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 871 | |
| 872 | // If the result is an integer, this is a trunc or bitcast. |
| 873 | if (ToType->isIntegerTy()) { |
| 874 | // Should be done. |
| 875 | } else if (ToType->isFloatingPointTy() || ToType->isVectorTy()) { |
| 876 | // Just do a bitcast, we know the sizes match up. |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 877 | FromVal = Builder.CreateBitCast(FromVal, ToType); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 878 | } else { |
| 879 | // Otherwise must be a pointer. |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 880 | FromVal = Builder.CreateIntToPtr(FromVal, ToType); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 881 | } |
| 882 | assert(FromVal->getType() == ToType && "Didn't convert right?"); |
| 883 | return FromVal; |
| 884 | } |
| 885 | |
| 886 | /// ConvertScalar_InsertValue - Insert the value "SV" into the existing integer |
| 887 | /// or vector value "Old" at the offset specified by Offset. |
| 888 | /// |
| 889 | /// This happens when we are converting an "integer union" to a |
| 890 | /// single integer scalar, or when we are converting a "vector union" to a |
| 891 | /// vector with insert/extractelement instructions. |
| 892 | /// |
| 893 | /// Offset is an offset from the original alloca, in bits that need to be |
| 894 | /// shifted to the right. |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 895 | /// |
| 896 | /// NonConstantIdx is an index value if there was a GEP with a non-constant |
| 897 | /// index value. If this is 0 then all GEPs used to find this insert address |
| 898 | /// are constant. |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 899 | Value *ConvertToScalarInfo:: |
| 900 | ConvertScalar_InsertValue(Value *SV, Value *Old, |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 901 | uint64_t Offset, Value* NonConstantIdx, |
| 902 | IRBuilder<> &Builder) { |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 903 | // Convert the stored type to the actual type, shift it left to insert |
| 904 | // then 'or' into place. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 905 | Type *AllocaType = Old->getType(); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 906 | LLVMContext &Context = Old->getContext(); |
| 907 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 908 | if (VectorType *VTy = dyn_cast<VectorType>(AllocaType)) { |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 909 | uint64_t VecSize = TD.getTypeAllocSizeInBits(VTy); |
| 910 | uint64_t ValSize = TD.getTypeAllocSizeInBits(SV->getType()); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 911 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 912 | // Changing the whole vector with memset or with an access of a different |
| 913 | // vector type? |
Cameron Zwarich | 446d952 | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 914 | if (ValSize == VecSize) |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 915 | return Builder.CreateBitCast(SV, AllocaType); |
Cameron Zwarich | b2fd770 | 2011-03-09 05:43:05 +0000 | [diff] [blame] | 916 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 917 | // Must be an element insertion. |
Cameron Zwarich | 90747e3 | 2011-10-23 07:02:10 +0000 | [diff] [blame] | 918 | Type *EltTy = VTy->getElementType(); |
| 919 | if (SV->getType() != EltTy) |
| 920 | SV = Builder.CreateBitCast(SV, EltTy); |
| 921 | uint64_t EltSize = TD.getTypeAllocSizeInBits(EltTy); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 922 | unsigned Elt = Offset/EltSize; |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 923 | Value *Idx; |
| 924 | if (NonConstantIdx) { |
| 925 | if (Elt) |
| 926 | Idx = Builder.CreateAdd(NonConstantIdx, |
| 927 | Builder.getInt32(Elt), |
| 928 | "dyn.offset"); |
| 929 | else |
| 930 | Idx = NonConstantIdx; |
| 931 | } else |
| 932 | Idx = Builder.getInt32(Elt); |
| 933 | return Builder.CreateInsertElement(Old, SV, Idx); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 934 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 935 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 936 | // If SV is a first-class aggregate value, insert each value recursively. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 937 | if (StructType *ST = dyn_cast<StructType>(SV->getType())) { |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 938 | assert(!NonConstantIdx && |
| 939 | "Dynamic indexing into struct types not supported"); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 940 | const StructLayout &Layout = *TD.getStructLayout(ST); |
| 941 | for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 942 | Value *Elt = Builder.CreateExtractValue(SV, i); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 943 | Old = ConvertScalar_InsertValue(Elt, Old, |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 944 | Offset+Layout.getElementOffsetInBits(i), |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 945 | 0, Builder); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 946 | } |
| 947 | return Old; |
| 948 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 949 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 950 | if (ArrayType *AT = dyn_cast<ArrayType>(SV->getType())) { |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 951 | assert(!NonConstantIdx && |
| 952 | "Dynamic indexing into array types not supported"); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 953 | uint64_t EltSize = TD.getTypeAllocSizeInBits(AT->getElementType()); |
| 954 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 955 | Value *Elt = Builder.CreateExtractValue(SV, i); |
Pete Cooper | 80f020a | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 956 | Old = ConvertScalar_InsertValue(Elt, Old, Offset+i*EltSize, 0, Builder); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 957 | } |
| 958 | return Old; |
| 959 | } |
| 960 | |
| 961 | // If SV is a float, convert it to the appropriate integer type. |
| 962 | // If it is a pointer, do the same. |
| 963 | unsigned SrcWidth = TD.getTypeSizeInBits(SV->getType()); |
| 964 | unsigned DestWidth = TD.getTypeSizeInBits(AllocaType); |
| 965 | unsigned SrcStoreWidth = TD.getTypeStoreSizeInBits(SV->getType()); |
| 966 | unsigned DestStoreWidth = TD.getTypeStoreSizeInBits(AllocaType); |
| 967 | if (SV->getType()->isFloatingPointTy() || SV->getType()->isVectorTy()) |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 968 | SV = Builder.CreateBitCast(SV, IntegerType::get(SV->getContext(),SrcWidth)); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 969 | else if (SV->getType()->isPointerTy()) |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 970 | SV = Builder.CreatePtrToInt(SV, TD.getIntPtrType(SV->getContext())); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 971 | |
| 972 | // Zero extend or truncate the value if needed. |
| 973 | if (SV->getType() != AllocaType) { |
| 974 | if (SV->getType()->getPrimitiveSizeInBits() < |
| 975 | AllocaType->getPrimitiveSizeInBits()) |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 976 | SV = Builder.CreateZExt(SV, AllocaType); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 977 | else { |
| 978 | // Truncation may be needed if storing more than the alloca can hold |
| 979 | // (undefined behavior). |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 980 | SV = Builder.CreateTrunc(SV, AllocaType); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 981 | SrcWidth = DestWidth; |
| 982 | SrcStoreWidth = DestStoreWidth; |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | // If this is a big-endian system and the store is narrower than the |
| 987 | // full alloca type, we need to do a shift to get the right bits. |
| 988 | int ShAmt = 0; |
| 989 | if (TD.isBigEndian()) { |
| 990 | // On big-endian machines, the lowest bit is stored at the bit offset |
| 991 | // from the pointer given by getTypeStoreSizeInBits. This matters for |
| 992 | // integers with a bitwidth that is not a multiple of 8. |
| 993 | ShAmt = DestStoreWidth - SrcStoreWidth - Offset; |
| 994 | } else { |
| 995 | ShAmt = Offset; |
| 996 | } |
| 997 | |
| 998 | // Note: we support negative bitwidths (with shr) which are not defined. |
| 999 | // We do this to support (f.e.) stores off the end of a structure where |
| 1000 | // only some bits in the structure are set. |
| 1001 | APInt Mask(APInt::getLowBitsSet(DestWidth, SrcWidth)); |
| 1002 | if (ShAmt > 0 && (unsigned)ShAmt < DestWidth) { |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1003 | SV = Builder.CreateShl(SV, ConstantInt::get(SV->getType(), ShAmt)); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 1004 | Mask <<= ShAmt; |
| 1005 | } else if (ShAmt < 0 && (unsigned)-ShAmt < DestWidth) { |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1006 | SV = Builder.CreateLShr(SV, ConstantInt::get(SV->getType(), -ShAmt)); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 1007 | Mask = Mask.lshr(-ShAmt); |
| 1008 | } |
| 1009 | |
| 1010 | // Mask out the bits we are about to insert from the old value, and or |
| 1011 | // in the new bits. |
| 1012 | if (SrcWidth != DestWidth) { |
| 1013 | assert(DestWidth > SrcWidth); |
| 1014 | Old = Builder.CreateAnd(Old, ConstantInt::get(Context, ~Mask), "mask"); |
| 1015 | SV = Builder.CreateOr(Old, SV, "ins"); |
| 1016 | } |
| 1017 | return SV; |
| 1018 | } |
| 1019 | |
| 1020 | |
| 1021 | //===----------------------------------------------------------------------===// |
| 1022 | // SRoA Driver |
| 1023 | //===----------------------------------------------------------------------===// |
| 1024 | |
| 1025 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1026 | bool SROA::runOnFunction(Function &F) { |
Dan Gohman | e4af1cf | 2009-08-19 18:22:18 +0000 | [diff] [blame] | 1027 | TD = getAnalysisIfAvailable<TargetData>(); |
| 1028 | |
Chris Lattner | fe7ea0d | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 1029 | bool Changed = performPromotion(F); |
Dan Gohman | e4af1cf | 2009-08-19 18:22:18 +0000 | [diff] [blame] | 1030 | |
| 1031 | // FIXME: ScalarRepl currently depends on TargetData more than it |
| 1032 | // theoretically needs to. It should be refactored in order to support |
| 1033 | // target-independent IR. Until this is done, just skip the actual |
| 1034 | // scalar-replacement portion of this pass. |
| 1035 | if (!TD) return Changed; |
| 1036 | |
Chris Lattner | fe7ea0d | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 1037 | while (1) { |
| 1038 | bool LocalChange = performScalarRepl(F); |
| 1039 | if (!LocalChange) break; // No need to repromote if no scalarrepl |
| 1040 | Changed = true; |
| 1041 | LocalChange = performPromotion(F); |
| 1042 | if (!LocalChange) break; // No need to re-scalarrepl if no promotion |
| 1043 | } |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1044 | |
| 1045 | return Changed; |
| 1046 | } |
| 1047 | |
Chris Lattner | d0f5613 | 2011-01-14 19:50:47 +0000 | [diff] [blame] | 1048 | namespace { |
| 1049 | class AllocaPromoter : public LoadAndStorePromoter { |
| 1050 | AllocaInst *AI; |
Devang Patel | 231a5ab | 2011-07-06 21:09:55 +0000 | [diff] [blame] | 1051 | DIBuilder *DIB; |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1052 | SmallVector<DbgDeclareInst *, 4> DDIs; |
| 1053 | SmallVector<DbgValueInst *, 4> DVIs; |
Chris Lattner | d0f5613 | 2011-01-14 19:50:47 +0000 | [diff] [blame] | 1054 | public: |
Cameron Zwarich | c827939 | 2011-05-24 03:10:43 +0000 | [diff] [blame] | 1055 | AllocaPromoter(const SmallVectorImpl<Instruction*> &Insts, SSAUpdater &S, |
Devang Patel | 231a5ab | 2011-07-06 21:09:55 +0000 | [diff] [blame] | 1056 | DIBuilder *DB) |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1057 | : LoadAndStorePromoter(Insts, S), AI(0), DIB(DB) {} |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1058 | |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 1059 | void run(AllocaInst *AI, const SmallVectorImpl<Instruction*> &Insts) { |
Chris Lattner | d0f5613 | 2011-01-14 19:50:47 +0000 | [diff] [blame] | 1060 | // Remember which alloca we're promoting (for isInstInList). |
| 1061 | this->AI = AI; |
Rafael Espindola | 125ef76 | 2011-12-26 23:12:42 +0000 | [diff] [blame] | 1062 | if (MDNode *DebugNode = MDNode::getIfExists(AI->getContext(), AI)) { |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1063 | for (Value::use_iterator UI = DebugNode->use_begin(), |
| 1064 | E = DebugNode->use_end(); UI != E; ++UI) |
| 1065 | if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(*UI)) |
| 1066 | DDIs.push_back(DDI); |
| 1067 | else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(*UI)) |
| 1068 | DVIs.push_back(DVI); |
Rafael Espindola | 125ef76 | 2011-12-26 23:12:42 +0000 | [diff] [blame] | 1069 | } |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1070 | |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 1071 | LoadAndStorePromoter::run(Insts); |
Chris Lattner | d0f5613 | 2011-01-14 19:50:47 +0000 | [diff] [blame] | 1072 | AI->eraseFromParent(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1073 | for (SmallVector<DbgDeclareInst *, 4>::iterator I = DDIs.begin(), |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1074 | E = DDIs.end(); I != E; ++I) { |
| 1075 | DbgDeclareInst *DDI = *I; |
Devang Patel | 231a5ab | 2011-07-06 21:09:55 +0000 | [diff] [blame] | 1076 | DDI->eraseFromParent(); |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1077 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1078 | for (SmallVector<DbgValueInst *, 4>::iterator I = DVIs.begin(), |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1079 | E = DVIs.end(); I != E; ++I) { |
| 1080 | DbgValueInst *DVI = *I; |
| 1081 | DVI->eraseFromParent(); |
| 1082 | } |
Chris Lattner | e0a1a5b | 2011-01-14 07:50:47 +0000 | [diff] [blame] | 1083 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1084 | |
Chris Lattner | d0f5613 | 2011-01-14 19:50:47 +0000 | [diff] [blame] | 1085 | virtual bool isInstInList(Instruction *I, |
| 1086 | const SmallVectorImpl<Instruction*> &Insts) const { |
| 1087 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 1088 | return LI->getOperand(0) == AI; |
| 1089 | return cast<StoreInst>(I)->getPointerOperand() == AI; |
Chris Lattner | e0a1a5b | 2011-01-14 07:50:47 +0000 | [diff] [blame] | 1090 | } |
Devang Patel | 231a5ab | 2011-07-06 21:09:55 +0000 | [diff] [blame] | 1091 | |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1092 | virtual void updateDebugInfo(Instruction *Inst) const { |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1093 | for (SmallVector<DbgDeclareInst *, 4>::const_iterator I = DDIs.begin(), |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1094 | E = DDIs.end(); I != E; ++I) { |
| 1095 | DbgDeclareInst *DDI = *I; |
| 1096 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) |
| 1097 | ConvertDebugDeclareToDebugValue(DDI, SI, *DIB); |
| 1098 | else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) |
| 1099 | ConvertDebugDeclareToDebugValue(DDI, LI, *DIB); |
| 1100 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1101 | for (SmallVector<DbgValueInst *, 4>::const_iterator I = DVIs.begin(), |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1102 | E = DVIs.end(); I != E; ++I) { |
| 1103 | DbgValueInst *DVI = *I; |
Benjamin Kramer | 6a1c779 | 2012-02-23 17:42:19 +0000 | [diff] [blame] | 1104 | Value *Arg = NULL; |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1105 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1106 | // If an argument is zero extended then use argument directly. The ZExt |
| 1107 | // may be zapped by an optimization pass in future. |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1108 | if (ZExtInst *ZExt = dyn_cast<ZExtInst>(SI->getOperand(0))) |
Benjamin Kramer | 6a1c779 | 2012-02-23 17:42:19 +0000 | [diff] [blame] | 1109 | Arg = dyn_cast<Argument>(ZExt->getOperand(0)); |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1110 | if (SExtInst *SExt = dyn_cast<SExtInst>(SI->getOperand(0))) |
Benjamin Kramer | 6a1c779 | 2012-02-23 17:42:19 +0000 | [diff] [blame] | 1111 | Arg = dyn_cast<Argument>(SExt->getOperand(0)); |
| 1112 | if (!Arg) |
| 1113 | Arg = SI->getOperand(0); |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1114 | } else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
Benjamin Kramer | 6a1c779 | 2012-02-23 17:42:19 +0000 | [diff] [blame] | 1115 | Arg = LI->getOperand(0); |
| 1116 | } else { |
| 1117 | continue; |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1118 | } |
Benjamin Kramer | 6a1c779 | 2012-02-23 17:42:19 +0000 | [diff] [blame] | 1119 | Instruction *DbgVal = |
| 1120 | DIB->insertDbgValueIntrinsic(Arg, 0, DIVariable(DVI->getVariable()), |
| 1121 | Inst); |
| 1122 | DbgVal->setDebugLoc(DVI->getDebugLoc()); |
Devang Patel | 4fd3c59 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1123 | } |
Devang Patel | 231a5ab | 2011-07-06 21:09:55 +0000 | [diff] [blame] | 1124 | } |
Chris Lattner | d0f5613 | 2011-01-14 19:50:47 +0000 | [diff] [blame] | 1125 | }; |
| 1126 | } // end anon namespace |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1128 | /// isSafeSelectToSpeculate - Select instructions that use an alloca and are |
| 1129 | /// subsequently loaded can be rewritten to load both input pointers and then |
| 1130 | /// select between the result, allowing the load of the alloca to be promoted. |
| 1131 | /// From this: |
| 1132 | /// %P2 = select i1 %cond, i32* %Alloca, i32* %Other |
| 1133 | /// %V = load i32* %P2 |
| 1134 | /// to: |
| 1135 | /// %V1 = load i32* %Alloca -> will be mem2reg'd |
| 1136 | /// %V2 = load i32* %Other |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1137 | /// %V = select i1 %cond, i32 %V1, i32 %V2 |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1138 | /// |
| 1139 | /// We can do this to a select if its only uses are loads and if the operand to |
| 1140 | /// the select can be loaded unconditionally. |
| 1141 | static bool isSafeSelectToSpeculate(SelectInst *SI, const TargetData *TD) { |
| 1142 | bool TDerefable = SI->getTrueValue()->isDereferenceablePointer(); |
| 1143 | bool FDerefable = SI->getFalseValue()->isDereferenceablePointer(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1144 | |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1145 | for (Value::use_iterator UI = SI->use_begin(), UE = SI->use_end(); |
| 1146 | UI != UE; ++UI) { |
| 1147 | LoadInst *LI = dyn_cast<LoadInst>(*UI); |
Eli Friedman | 2bc3d52 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1148 | if (LI == 0 || !LI->isSimple()) return false; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1149 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1150 | // Both operands to the select need to be dereferencable, either absolutely |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1151 | // (e.g. allocas) or at this point because we can see other accesses to it. |
| 1152 | if (!TDerefable && !isSafeToLoadUnconditionally(SI->getTrueValue(), LI, |
| 1153 | LI->getAlignment(), TD)) |
| 1154 | return false; |
| 1155 | if (!FDerefable && !isSafeToLoadUnconditionally(SI->getFalseValue(), LI, |
| 1156 | LI->getAlignment(), TD)) |
| 1157 | return false; |
| 1158 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1159 | |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1160 | return true; |
| 1161 | } |
| 1162 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1163 | /// isSafePHIToSpeculate - PHI instructions that use an alloca and are |
| 1164 | /// subsequently loaded can be rewritten to load both input pointers in the pred |
| 1165 | /// blocks and then PHI the results, allowing the load of the alloca to be |
| 1166 | /// promoted. |
| 1167 | /// From this: |
| 1168 | /// %P2 = phi [i32* %Alloca, i32* %Other] |
| 1169 | /// %V = load i32* %P2 |
| 1170 | /// to: |
| 1171 | /// %V1 = load i32* %Alloca -> will be mem2reg'd |
| 1172 | /// ... |
| 1173 | /// %V2 = load i32* %Other |
| 1174 | /// ... |
| 1175 | /// %V = phi [i32 %V1, i32 %V2] |
| 1176 | /// |
| 1177 | /// We can do this to a select if its only uses are loads and if the operand to |
| 1178 | /// the select can be loaded unconditionally. |
| 1179 | static bool isSafePHIToSpeculate(PHINode *PN, const TargetData *TD) { |
| 1180 | // For now, we can only do this promotion if the load is in the same block as |
| 1181 | // the PHI, and if there are no stores between the phi and load. |
| 1182 | // TODO: Allow recursive phi users. |
| 1183 | // TODO: Allow stores. |
| 1184 | BasicBlock *BB = PN->getParent(); |
| 1185 | unsigned MaxAlign = 0; |
| 1186 | for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end(); |
| 1187 | UI != UE; ++UI) { |
| 1188 | LoadInst *LI = dyn_cast<LoadInst>(*UI); |
Eli Friedman | 2bc3d52 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1189 | if (LI == 0 || !LI->isSimple()) return false; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1190 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1191 | // For now we only allow loads in the same block as the PHI. This is a |
| 1192 | // common case that happens when instcombine merges two loads through a PHI. |
| 1193 | if (LI->getParent() != BB) return false; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1194 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1195 | // Ensure that there are no instructions between the PHI and the load that |
| 1196 | // could store. |
| 1197 | for (BasicBlock::iterator BBI = PN; &*BBI != LI; ++BBI) |
| 1198 | if (BBI->mayWriteToMemory()) |
| 1199 | return false; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1200 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1201 | MaxAlign = std::max(MaxAlign, LI->getAlignment()); |
| 1202 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1203 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1204 | // Okay, we know that we have one or more loads in the same block as the PHI. |
| 1205 | // We can transform this if it is safe to push the loads into the predecessor |
| 1206 | // blocks. The only thing to watch out for is that we can't put a possibly |
| 1207 | // trapping load in the predecessor if it is a critical edge. |
| 1208 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1209 | BasicBlock *Pred = PN->getIncomingBlock(i); |
Eli Friedman | d102a03 | 2011-09-22 18:56:30 +0000 | [diff] [blame] | 1210 | Value *InVal = PN->getIncomingValue(i); |
| 1211 | |
| 1212 | // If the terminator of the predecessor has side-effects (an invoke), |
| 1213 | // there is no safe place to put a load in the predecessor. |
| 1214 | if (Pred->getTerminator()->mayHaveSideEffects()) |
| 1215 | return false; |
| 1216 | |
| 1217 | // If the value is produced by the terminator of the predecessor |
| 1218 | // (an invoke), there is no valid place to put a load in the predecessor. |
| 1219 | if (Pred->getTerminator() == InVal) |
| 1220 | return false; |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1221 | |
| 1222 | // If the predecessor has a single successor, then the edge isn't critical. |
| 1223 | if (Pred->getTerminator()->getNumSuccessors() == 1) |
| 1224 | continue; |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1225 | |
| 1226 | // If this pointer is always safe to load, or if we can prove that there is |
| 1227 | // already a load in the block, then we can move the load to the pred block. |
| 1228 | if (InVal->isDereferenceablePointer() || |
| 1229 | isSafeToLoadUnconditionally(InVal, Pred->getTerminator(), MaxAlign, TD)) |
| 1230 | continue; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1232 | return false; |
| 1233 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1234 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1235 | return true; |
| 1236 | } |
| 1237 | |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1238 | |
| 1239 | /// tryToMakeAllocaBePromotable - This returns true if the alloca only has |
| 1240 | /// direct (non-volatile) loads and stores to it. If the alloca is close but |
| 1241 | /// not quite there, this will transform the code to allow promotion. As such, |
| 1242 | /// it is a non-pure predicate. |
| 1243 | static bool tryToMakeAllocaBePromotable(AllocaInst *AI, const TargetData *TD) { |
| 1244 | SetVector<Instruction*, SmallVector<Instruction*, 4>, |
| 1245 | SmallPtrSet<Instruction*, 4> > InstsToRewrite; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1246 | |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1247 | for (Value::use_iterator UI = AI->use_begin(), UE = AI->use_end(); |
| 1248 | UI != UE; ++UI) { |
| 1249 | User *U = *UI; |
| 1250 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
Eli Friedman | 2bc3d52 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1251 | if (!LI->isSimple()) |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1252 | return false; |
| 1253 | continue; |
| 1254 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1255 | |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1256 | if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
Eli Friedman | 2bc3d52 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1257 | if (SI->getOperand(0) == AI || !SI->isSimple()) |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1258 | return false; // Don't allow a store OF the AI, only INTO the AI. |
| 1259 | continue; |
| 1260 | } |
| 1261 | |
| 1262 | if (SelectInst *SI = dyn_cast<SelectInst>(U)) { |
| 1263 | // If the condition being selected on is a constant, fold the select, yes |
| 1264 | // this does (rarely) happen early on. |
| 1265 | if (ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 1266 | Value *Result = SI->getOperand(1+CI->isZero()); |
| 1267 | SI->replaceAllUsesWith(Result); |
| 1268 | SI->eraseFromParent(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1269 | |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1270 | // This is very rare and we just scrambled the use list of AI, start |
| 1271 | // over completely. |
| 1272 | return tryToMakeAllocaBePromotable(AI, TD); |
| 1273 | } |
| 1274 | |
| 1275 | // If it is safe to turn "load (select c, AI, ptr)" into a select of two |
| 1276 | // loads, then we can transform this by rewriting the select. |
| 1277 | if (!isSafeSelectToSpeculate(SI, TD)) |
| 1278 | return false; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1279 | |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1280 | InstsToRewrite.insert(SI); |
| 1281 | continue; |
| 1282 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1283 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1284 | if (PHINode *PN = dyn_cast<PHINode>(U)) { |
| 1285 | if (PN->use_empty()) { // Dead PHIs can be stripped. |
| 1286 | InstsToRewrite.insert(PN); |
| 1287 | continue; |
| 1288 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1289 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1290 | // If it is safe to turn "load (phi [AI, ptr, ...])" into a PHI of loads |
| 1291 | // in the pred blocks, then we can transform this by rewriting the PHI. |
| 1292 | if (!isSafePHIToSpeculate(PN, TD)) |
| 1293 | return false; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1294 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1295 | InstsToRewrite.insert(PN); |
| 1296 | continue; |
| 1297 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1298 | |
Nick Lewycky | 5a1cb64 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 1299 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(U)) { |
| 1300 | if (onlyUsedByLifetimeMarkers(BCI)) { |
| 1301 | InstsToRewrite.insert(BCI); |
| 1302 | continue; |
| 1303 | } |
| 1304 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1305 | |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1306 | return false; |
| 1307 | } |
| 1308 | |
| 1309 | // If there are no instructions to rewrite, then all uses are load/stores and |
| 1310 | // we're done! |
| 1311 | if (InstsToRewrite.empty()) |
| 1312 | return true; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1313 | |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1314 | // If we have instructions that need to be rewritten for this to be promotable |
| 1315 | // take care of it now. |
| 1316 | for (unsigned i = 0, e = InstsToRewrite.size(); i != e; ++i) { |
Nick Lewycky | 5a1cb64 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 1317 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(InstsToRewrite[i])) { |
| 1318 | // This could only be a bitcast used by nothing but lifetime intrinsics. |
| 1319 | for (BitCastInst::use_iterator I = BCI->use_begin(), E = BCI->use_end(); |
| 1320 | I != E;) { |
| 1321 | Use &U = I.getUse(); |
| 1322 | ++I; |
| 1323 | cast<Instruction>(U.getUser())->eraseFromParent(); |
| 1324 | } |
| 1325 | BCI->eraseFromParent(); |
| 1326 | continue; |
| 1327 | } |
| 1328 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1329 | if (SelectInst *SI = dyn_cast<SelectInst>(InstsToRewrite[i])) { |
| 1330 | // Selects in InstsToRewrite only have load uses. Rewrite each as two |
| 1331 | // loads with a new select. |
| 1332 | while (!SI->use_empty()) { |
| 1333 | LoadInst *LI = cast<LoadInst>(SI->use_back()); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1334 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1335 | IRBuilder<> Builder(LI); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1336 | LoadInst *TrueLoad = |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1337 | Builder.CreateLoad(SI->getTrueValue(), LI->getName()+".t"); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1338 | LoadInst *FalseLoad = |
Nick Lewycky | 394d1f1 | 2011-07-01 06:27:03 +0000 | [diff] [blame] | 1339 | Builder.CreateLoad(SI->getFalseValue(), LI->getName()+".f"); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1340 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1341 | // Transfer alignment and TBAA info if present. |
| 1342 | TrueLoad->setAlignment(LI->getAlignment()); |
| 1343 | FalseLoad->setAlignment(LI->getAlignment()); |
| 1344 | if (MDNode *Tag = LI->getMetadata(LLVMContext::MD_tbaa)) { |
| 1345 | TrueLoad->setMetadata(LLVMContext::MD_tbaa, Tag); |
| 1346 | FalseLoad->setMetadata(LLVMContext::MD_tbaa, Tag); |
| 1347 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1348 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1349 | Value *V = Builder.CreateSelect(SI->getCondition(), TrueLoad, FalseLoad); |
| 1350 | V->takeName(LI); |
| 1351 | LI->replaceAllUsesWith(V); |
| 1352 | LI->eraseFromParent(); |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1353 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1355 | // Now that all the loads are gone, the select is gone too. |
| 1356 | SI->eraseFromParent(); |
| 1357 | continue; |
| 1358 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1359 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1360 | // Otherwise, we have a PHI node which allows us to push the loads into the |
| 1361 | // predecessors. |
| 1362 | PHINode *PN = cast<PHINode>(InstsToRewrite[i]); |
| 1363 | if (PN->use_empty()) { |
| 1364 | PN->eraseFromParent(); |
| 1365 | continue; |
| 1366 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1367 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1368 | Type *LoadTy = cast<PointerType>(PN->getType())->getElementType(); |
Jay Foad | 3ecfc86 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 1369 | PHINode *NewPN = PHINode::Create(LoadTy, PN->getNumIncomingValues(), |
| 1370 | PN->getName()+".ld", PN); |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1371 | |
| 1372 | // Get the TBAA tag and alignment to use from one of the loads. It doesn't |
| 1373 | // matter which one we get and if any differ, it doesn't matter. |
| 1374 | LoadInst *SomeLoad = cast<LoadInst>(PN->use_back()); |
| 1375 | MDNode *TBAATag = SomeLoad->getMetadata(LLVMContext::MD_tbaa); |
| 1376 | unsigned Align = SomeLoad->getAlignment(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1377 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1378 | // Rewrite all loads of the PN to use the new PHI. |
| 1379 | while (!PN->use_empty()) { |
| 1380 | LoadInst *LI = cast<LoadInst>(PN->use_back()); |
| 1381 | LI->replaceAllUsesWith(NewPN); |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1382 | LI->eraseFromParent(); |
| 1383 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1384 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1385 | // Inject loads into all of the pred blocks. Keep track of which blocks we |
| 1386 | // insert them into in case we have multiple edges from the same block. |
| 1387 | DenseMap<BasicBlock*, LoadInst*> InsertedLoads; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1388 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1389 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1390 | BasicBlock *Pred = PN->getIncomingBlock(i); |
| 1391 | LoadInst *&Load = InsertedLoads[Pred]; |
| 1392 | if (Load == 0) { |
| 1393 | Load = new LoadInst(PN->getIncomingValue(i), |
| 1394 | PN->getName() + "." + Pred->getName(), |
| 1395 | Pred->getTerminator()); |
| 1396 | Load->setAlignment(Align); |
| 1397 | if (TBAATag) Load->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
| 1398 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1399 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1400 | NewPN->addIncoming(Load, Pred); |
| 1401 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1402 | |
Chris Lattner | e335786 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1403 | PN->eraseFromParent(); |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1404 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1405 | |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1406 | ++NumAdjusted; |
| 1407 | return true; |
| 1408 | } |
| 1409 | |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1410 | bool SROA::performPromotion(Function &F) { |
| 1411 | std::vector<AllocaInst*> Allocas; |
Chris Lattner | e0a1a5b | 2011-01-14 07:50:47 +0000 | [diff] [blame] | 1412 | DominatorTree *DT = 0; |
Cameron Zwarich | b1686c3 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 1413 | if (HasDomTree) |
Chris Lattner | e0a1a5b | 2011-01-14 07:50:47 +0000 | [diff] [blame] | 1414 | DT = &getAnalysis<DominatorTree>(); |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1415 | |
Chris Lattner | 02a3be0 | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 1416 | BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function |
Devang Patel | 231a5ab | 2011-07-06 21:09:55 +0000 | [diff] [blame] | 1417 | DIBuilder DIB(*F.getParent()); |
Chris Lattner | fe7ea0d | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 1418 | bool Changed = false; |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 1419 | SmallVector<Instruction*, 64> Insts; |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1420 | while (1) { |
| 1421 | Allocas.clear(); |
| 1422 | |
| 1423 | // Find allocas that are safe to promote, by looking at all instructions in |
| 1424 | // the entry node |
| 1425 | for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) |
| 1426 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? |
Chris Lattner | c87c50a | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1427 | if (tryToMakeAllocaBePromotable(AI, TD)) |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1428 | Allocas.push_back(AI); |
| 1429 | |
| 1430 | if (Allocas.empty()) break; |
| 1431 | |
Cameron Zwarich | b1686c3 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 1432 | if (HasDomTree) |
Cameron Zwarich | 419e8a6 | 2011-01-17 17:38:41 +0000 | [diff] [blame] | 1433 | PromoteMemToReg(Allocas, *DT); |
Chris Lattner | e0a1a5b | 2011-01-14 07:50:47 +0000 | [diff] [blame] | 1434 | else { |
| 1435 | SSAUpdater SSA; |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 1436 | for (unsigned i = 0, e = Allocas.size(); i != e; ++i) { |
| 1437 | AllocaInst *AI = Allocas[i]; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1438 | |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 1439 | // Build list of instructions to promote. |
| 1440 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 1441 | UI != E; ++UI) |
| 1442 | Insts.push_back(cast<Instruction>(*UI)); |
Devang Patel | 231a5ab | 2011-07-06 21:09:55 +0000 | [diff] [blame] | 1443 | AllocaPromoter(Insts, SSA, &DIB).run(AI, Insts); |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 1444 | Insts.clear(); |
| 1445 | } |
Chris Lattner | e0a1a5b | 2011-01-14 07:50:47 +0000 | [diff] [blame] | 1446 | } |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1447 | NumPromoted += Allocas.size(); |
| 1448 | Changed = true; |
| 1449 | } |
| 1450 | |
| 1451 | return Changed; |
| 1452 | } |
| 1453 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 1454 | |
Bob Wilson | 3992feb | 2010-02-03 17:23:56 +0000 | [diff] [blame] | 1455 | /// ShouldAttemptScalarRepl - Decide if an alloca is a good candidate for |
| 1456 | /// SROA. It must be a struct or array type with a small number of elements. |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 1457 | bool SROA::ShouldAttemptScalarRepl(AllocaInst *AI) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1458 | Type *T = AI->getAllocatedType(); |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 1459 | // Do not promote any struct that has too many members. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1460 | if (StructType *ST = dyn_cast<StructType>(T)) |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 1461 | return ST->getNumElements() <= StructMemberThreshold; |
| 1462 | // Do not promote any array that has too many elements. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1463 | if (ArrayType *AT = dyn_cast<ArrayType>(T)) |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 1464 | return AT->getNumElements() <= ArrayElementThreshold; |
Bob Wilson | 3992feb | 2010-02-03 17:23:56 +0000 | [diff] [blame] | 1465 | return false; |
Chris Lattner | 963a97f | 2008-06-22 17:46:21 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Peter Collingbourne | 9012c57 | 2012-05-19 22:52:10 +0000 | [diff] [blame] | 1468 | /// getPointeeAlignment - Compute the minimum alignment of the value pointed |
| 1469 | /// to by the given pointer. |
| 1470 | static unsigned getPointeeAlignment(Value *V, const TargetData &TD) { |
| 1471 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 1472 | if (CE->getOpcode() == Instruction::BitCast || |
| 1473 | (CE->getOpcode() == Instruction::GetElementPtr && |
| 1474 | cast<GEPOperator>(CE)->hasAllZeroIndices())) |
| 1475 | return getPointeeAlignment(CE->getOperand(0), TD); |
| 1476 | |
| 1477 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 1478 | if (!GV->isDeclaration()) |
| 1479 | return TD.getPreferredAlignment(GV); |
| 1480 | |
| 1481 | if (PointerType *PT = dyn_cast<PointerType>(V->getType())) |
| 1482 | return TD.getABITypeAlignment(PT->getElementType()); |
| 1483 | |
| 1484 | return 0; |
| 1485 | } |
| 1486 | |
Chris Lattner | c447207 | 2010-04-15 23:50:26 +0000 | [diff] [blame] | 1487 | |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1488 | // performScalarRepl - This algorithm is a simple worklist driven algorithm, |
Nick Lewycky | 9174d5c | 2011-06-27 05:40:02 +0000 | [diff] [blame] | 1489 | // which runs on all of the alloca instructions in the function, removing them |
| 1490 | // if they are only used by getelementptr instructions. |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1491 | // |
| 1492 | bool SROA::performScalarRepl(Function &F) { |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1493 | std::vector<AllocaInst*> WorkList; |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1494 | |
Chris Lattner | 31d8010 | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 1495 | // Scan the entry basic block, adding allocas to the worklist. |
Chris Lattner | 02a3be0 | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 1496 | BasicBlock &BB = F.getEntryBlock(); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1497 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1498 | if (AllocaInst *A = dyn_cast<AllocaInst>(I)) |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1499 | WorkList.push_back(A); |
| 1500 | |
| 1501 | // Process the worklist |
| 1502 | bool Changed = false; |
| 1503 | while (!WorkList.empty()) { |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1504 | AllocaInst *AI = WorkList.back(); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1505 | WorkList.pop_back(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1506 | |
Chris Lattner | add2bd7 | 2006-12-22 23:14:42 +0000 | [diff] [blame] | 1507 | // Handle dead allocas trivially. These can be formed by SROA'ing arrays |
| 1508 | // with unused elements. |
| 1509 | if (AI->use_empty()) { |
| 1510 | AI->eraseFromParent(); |
Chris Lattner | c447207 | 2010-04-15 23:50:26 +0000 | [diff] [blame] | 1511 | Changed = true; |
Chris Lattner | add2bd7 | 2006-12-22 23:14:42 +0000 | [diff] [blame] | 1512 | continue; |
| 1513 | } |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1514 | |
| 1515 | // If this alloca is impossible for us to promote, reject it early. |
| 1516 | if (AI->isArrayAllocation() || !AI->getAllocatedType()->isSized()) |
| 1517 | continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1518 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1519 | // Check to see if this allocation is only modified by a memcpy/memmove from |
Peter Collingbourne | 9012c57 | 2012-05-19 22:52:10 +0000 | [diff] [blame] | 1520 | // a constant global whose alignment is equal to or exceeds that of the |
| 1521 | // allocation. If this is the case, we can change all users to use |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1522 | // the constant global instead. This is commonly produced by the CFE by |
| 1523 | // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A' |
| 1524 | // is only subsequently read. |
Nick Lewycky | 9174d5c | 2011-06-27 05:40:02 +0000 | [diff] [blame] | 1525 | SmallVector<Instruction *, 4> ToDelete; |
| 1526 | if (MemTransferInst *Copy = isOnlyCopiedFromConstantGlobal(AI, ToDelete)) { |
Peter Collingbourne | 9012c57 | 2012-05-19 22:52:10 +0000 | [diff] [blame] | 1527 | if (AI->getAlignment() <= getPointeeAlignment(Copy->getSource(), *TD)) { |
| 1528 | DEBUG(dbgs() << "Found alloca equal to global: " << *AI << '\n'); |
| 1529 | DEBUG(dbgs() << " memcpy = " << *Copy << '\n'); |
| 1530 | for (unsigned i = 0, e = ToDelete.size(); i != e; ++i) |
| 1531 | ToDelete[i]->eraseFromParent(); |
| 1532 | Constant *TheSrc = cast<Constant>(Copy->getSource()); |
| 1533 | AI->replaceAllUsesWith(ConstantExpr::getBitCast(TheSrc, AI->getType())); |
| 1534 | Copy->eraseFromParent(); // Don't mutate the global. |
| 1535 | AI->eraseFromParent(); |
| 1536 | ++NumGlobals; |
| 1537 | Changed = true; |
| 1538 | continue; |
| 1539 | } |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1540 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1541 | |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1542 | // Check to see if we can perform the core SROA transformation. We cannot |
| 1543 | // transform the allocation instruction if it is an array allocation |
| 1544 | // (allocations OF arrays are ok though), and an allocation of a scalar |
| 1545 | // value cannot be decomposed at all. |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1546 | uint64_t AllocaSize = TD->getTypeAllocSize(AI->getAllocatedType()); |
Bill Wendling | 5a377cb | 2009-03-03 12:12:58 +0000 | [diff] [blame] | 1547 | |
Nick Lewycky | d3aa25e | 2009-08-17 05:37:31 +0000 | [diff] [blame] | 1548 | // Do not promote [0 x %struct]. |
| 1549 | if (AllocaSize == 0) continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1550 | |
Chris Lattner | 31d8010 | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 1551 | // Do not promote any struct whose size is too big. |
| 1552 | if (AllocaSize > SRThreshold) continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1553 | |
Bob Wilson | 3992feb | 2010-02-03 17:23:56 +0000 | [diff] [blame] | 1554 | // If the alloca looks like a good candidate for scalar replacement, and if |
| 1555 | // all its users can be transformed, then split up the aggregate into its |
| 1556 | // separate elements. |
| 1557 | if (ShouldAttemptScalarRepl(AI) && isSafeAllocaToScalarRepl(AI)) { |
| 1558 | DoScalarReplacement(AI, WorkList); |
| 1559 | Changed = true; |
| 1560 | continue; |
| 1561 | } |
| 1562 | |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 1563 | // If we can turn this aggregate value (potentially with casts) into a |
| 1564 | // simple scalar value that can be mem2reg'd into a register value. |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1565 | // IsNotTrivial tracks whether this is something that mem2reg could have |
| 1566 | // promoted itself. If so, we don't want to transform it needlessly. Note |
| 1567 | // that we can't just check based on the type: the alloca may be of an i32 |
| 1568 | // but that has pointer arithmetic to set byte 3 of it or something. |
Nadav Rotem | 2114a8a | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 1569 | if (AllocaInst *NewAI = ConvertToScalarInfo( |
| 1570 | (unsigned)AllocaSize, *TD, ScalarLoadThreshold).TryConvert(AI)) { |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1571 | NewAI->takeName(AI); |
| 1572 | AI->eraseFromParent(); |
| 1573 | ++NumConverted; |
| 1574 | Changed = true; |
| 1575 | continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1578 | // Otherwise, couldn't process this alloca. |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | return Changed; |
| 1582 | } |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 1583 | |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1584 | /// DoScalarReplacement - This alloca satisfied the isSafeAllocaToScalarRepl |
| 1585 | /// predicate, do SROA now. |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1586 | void SROA::DoScalarReplacement(AllocaInst *AI, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1587 | std::vector<AllocaInst*> &WorkList) { |
David Greene | 504c7d8 | 2010-01-05 01:27:09 +0000 | [diff] [blame] | 1588 | DEBUG(dbgs() << "Found inst to SROA: " << *AI << '\n'); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1589 | SmallVector<AllocaInst*, 32> ElementAllocas; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1590 | if (StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) { |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1591 | ElementAllocas.reserve(ST->getNumContainedTypes()); |
| 1592 | for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) { |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1593 | AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0, |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1594 | AI->getAlignment(), |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 1595 | AI->getName() + "." + Twine(i), AI); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1596 | ElementAllocas.push_back(NA); |
| 1597 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 1598 | } |
| 1599 | } else { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1600 | ArrayType *AT = cast<ArrayType>(AI->getAllocatedType()); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1601 | ElementAllocas.reserve(AT->getNumElements()); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1602 | Type *ElTy = AT->getElementType(); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1603 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 1604 | AllocaInst *NA = new AllocaInst(ElTy, 0, AI->getAlignment(), |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 1605 | AI->getName() + "." + Twine(i), AI); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1606 | ElementAllocas.push_back(NA); |
| 1607 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 1608 | } |
| 1609 | } |
| 1610 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1611 | // Now that we have created the new alloca instructions, rewrite all the |
| 1612 | // uses of the old alloca. |
| 1613 | RewriteForScalarRepl(AI, AI, 0, ElementAllocas); |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 1614 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1615 | // Now erase any instructions that were made dead while rewriting the alloca. |
| 1616 | DeleteDeadInstructions(); |
Bob Wilson | 39c88a6 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1617 | AI->eraseFromParent(); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1618 | |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1619 | ++NumReplaced; |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1620 | } |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 1621 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1622 | /// DeleteDeadInstructions - Erase instructions on the DeadInstrs list, |
| 1623 | /// recursively including all their operands that become trivially dead. |
| 1624 | void SROA::DeleteDeadInstructions() { |
| 1625 | while (!DeadInsts.empty()) { |
| 1626 | Instruction *I = cast<Instruction>(DeadInsts.pop_back_val()); |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 1627 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1628 | for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) |
| 1629 | if (Instruction *U = dyn_cast<Instruction>(*OI)) { |
| 1630 | // Zero out the operand and see if it becomes trivially dead. |
| 1631 | // (But, don't add allocas to the dead instruction list -- they are |
| 1632 | // already on the worklist and will be deleted separately.) |
| 1633 | *OI = 0; |
| 1634 | if (isInstructionTriviallyDead(U) && !isa<AllocaInst>(U)) |
| 1635 | DeadInsts.push_back(U); |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 1636 | } |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1637 | |
| 1638 | I->eraseFromParent(); |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 1639 | } |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 1640 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1641 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1642 | /// isSafeForScalarRepl - Check if instruction I is a safe use with regard to |
| 1643 | /// performing scalar replacement of alloca AI. The results are flagged in |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1644 | /// the Info parameter. Offset indicates the position within AI that is |
| 1645 | /// referenced by this instruction. |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1646 | void SROA::isSafeForScalarRepl(Instruction *I, uint64_t Offset, |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1647 | AllocaInfo &Info) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1648 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI!=E; ++UI) { |
| 1649 | Instruction *User = cast<Instruction>(*UI); |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 1650 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1651 | if (BitCastInst *BC = dyn_cast<BitCastInst>(User)) { |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1652 | isSafeForScalarRepl(BC, Offset, Info); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1653 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1654 | uint64_t GEPOffset = Offset; |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1655 | isSafeGEP(GEPI, GEPOffset, Info); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1656 | if (!Info.isUnsafe) |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1657 | isSafeForScalarRepl(GEPI, GEPOffset, Info); |
Gabor Greif | 19101c7 | 2010-06-28 11:20:42 +0000 | [diff] [blame] | 1658 | } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1659 | ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength()); |
Chris Lattner | d01a0da | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1660 | if (Length == 0) |
| 1661 | return MarkUnsafe(Info, User); |
Aaron Ballman | 7e2fa31 | 2012-03-15 00:05:31 +0000 | [diff] [blame] | 1662 | if (Length->isNegative()) |
| 1663 | return MarkUnsafe(Info, User); |
| 1664 | |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1665 | isSafeMemAccess(Offset, Length->getZExtValue(), 0, |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1666 | UI.getOperandNo() == 0, Info, MI, |
| 1667 | true /*AllowWholeAccess*/); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1668 | } else if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
Eli Friedman | 2bc3d52 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1669 | if (!LI->isSimple()) |
Chris Lattner | d01a0da | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1670 | return MarkUnsafe(Info, User); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1671 | Type *LIType = LI->getType(); |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1672 | isSafeMemAccess(Offset, TD->getTypeAllocSize(LIType), |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1673 | LIType, false, Info, LI, true /*AllowWholeAccess*/); |
Chris Lattner | d01a0da | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1674 | Info.hasALoadOrStore = true; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1675 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1676 | } else if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 1677 | // Store is ok if storing INTO the pointer, not storing the pointer |
Eli Friedman | 2bc3d52 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1678 | if (!SI->isSimple() || SI->getOperand(0) == I) |
Chris Lattner | d01a0da | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1679 | return MarkUnsafe(Info, User); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1680 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1681 | Type *SIType = SI->getOperand(0)->getType(); |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1682 | isSafeMemAccess(Offset, TD->getTypeAllocSize(SIType), |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1683 | SIType, true, Info, SI, true /*AllowWholeAccess*/); |
Chris Lattner | d01a0da | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1684 | Info.hasALoadOrStore = true; |
Nick Lewycky | 5a1cb64 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 1685 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(User)) { |
| 1686 | if (II->getIntrinsicID() != Intrinsic::lifetime_start && |
| 1687 | II->getIntrinsicID() != Intrinsic::lifetime_end) |
| 1688 | return MarkUnsafe(Info, User); |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1689 | } else if (isa<PHINode>(User) || isa<SelectInst>(User)) { |
| 1690 | isSafePHISelectUseForScalarRepl(User, Offset, Info); |
| 1691 | } else { |
| 1692 | return MarkUnsafe(Info, User); |
| 1693 | } |
| 1694 | if (Info.isUnsafe) return; |
| 1695 | } |
| 1696 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1697 | |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1698 | |
| 1699 | /// isSafePHIUseForScalarRepl - If we see a PHI node or select using a pointer |
| 1700 | /// derived from the alloca, we can often still split the alloca into elements. |
| 1701 | /// This is useful if we have a large alloca where one element is phi'd |
| 1702 | /// together somewhere: we can SRoA and promote all the other elements even if |
| 1703 | /// we end up not being able to promote this one. |
| 1704 | /// |
| 1705 | /// All we require is that the uses of the PHI do not index into other parts of |
| 1706 | /// the alloca. The most important use case for this is single load and stores |
| 1707 | /// that are PHI'd together, which can happen due to code sinking. |
| 1708 | void SROA::isSafePHISelectUseForScalarRepl(Instruction *I, uint64_t Offset, |
| 1709 | AllocaInfo &Info) { |
| 1710 | // If we've already checked this PHI, don't do it again. |
| 1711 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 1712 | if (!Info.CheckedPHIs.insert(PN)) |
| 1713 | return; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1714 | |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1715 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI!=E; ++UI) { |
| 1716 | Instruction *User = cast<Instruction>(*UI); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1717 | |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1718 | if (BitCastInst *BC = dyn_cast<BitCastInst>(User)) { |
| 1719 | isSafePHISelectUseForScalarRepl(BC, Offset, Info); |
| 1720 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) { |
| 1721 | // Only allow "bitcast" GEPs for simplicity. We could generalize this, |
| 1722 | // but would have to prove that we're staying inside of an element being |
| 1723 | // promoted. |
| 1724 | if (!GEPI->hasAllZeroIndices()) |
| 1725 | return MarkUnsafe(Info, User); |
| 1726 | isSafePHISelectUseForScalarRepl(GEPI, Offset, Info); |
| 1727 | } else if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
Eli Friedman | 2bc3d52 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1728 | if (!LI->isSimple()) |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1729 | return MarkUnsafe(Info, User); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1730 | Type *LIType = LI->getType(); |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1731 | isSafeMemAccess(Offset, TD->getTypeAllocSize(LIType), |
| 1732 | LIType, false, Info, LI, false /*AllowWholeAccess*/); |
| 1733 | Info.hasALoadOrStore = true; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1734 | |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1735 | } else if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 1736 | // Store is ok if storing INTO the pointer, not storing the pointer |
Eli Friedman | 2bc3d52 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1737 | if (!SI->isSimple() || SI->getOperand(0) == I) |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1738 | return MarkUnsafe(Info, User); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1739 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1740 | Type *SIType = SI->getOperand(0)->getType(); |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1741 | isSafeMemAccess(Offset, TD->getTypeAllocSize(SIType), |
| 1742 | SIType, true, Info, SI, false /*AllowWholeAccess*/); |
| 1743 | Info.hasALoadOrStore = true; |
| 1744 | } else if (isa<PHINode>(User) || isa<SelectInst>(User)) { |
| 1745 | isSafePHISelectUseForScalarRepl(User, Offset, Info); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1746 | } else { |
Chris Lattner | d01a0da | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1747 | return MarkUnsafe(Info, User); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1748 | } |
| 1749 | if (Info.isUnsafe) return; |
Bob Wilson | 39c88a6 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1750 | } |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1751 | } |
Bob Wilson | 39c88a6 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1752 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1753 | /// isSafeGEP - Check if a GEP instruction can be handled for scalar |
| 1754 | /// replacement. It is safe when all the indices are constant, in-bounds |
| 1755 | /// references, and when the resulting offset corresponds to an element within |
| 1756 | /// the alloca type. The results are flagged in the Info parameter. Upon |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1757 | /// return, Offset is adjusted as specified by the GEP indices. |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1758 | void SROA::isSafeGEP(GetElementPtrInst *GEPI, |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1759 | uint64_t &Offset, AllocaInfo &Info) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1760 | gep_type_iterator GEPIt = gep_type_begin(GEPI), E = gep_type_end(GEPI); |
| 1761 | if (GEPIt == E) |
| 1762 | return; |
Pete Cooper | cbf7390 | 2012-06-15 18:07:29 +0000 | [diff] [blame] | 1763 | bool NonConstant = false; |
| 1764 | unsigned NonConstantIdxSize = 0; |
Bob Wilson | 39c88a6 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1765 | |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 1766 | // Walk through the GEP type indices, checking the types that this indexes |
| 1767 | // into. |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1768 | for (; GEPIt != E; ++GEPIt) { |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 1769 | // Ignore struct elements, no extra checking needed for these. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1770 | if ((*GEPIt)->isStructTy()) |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 1771 | continue; |
Matthijs Kooijman | 5fac55f | 2008-10-06 16:23:31 +0000 | [diff] [blame] | 1772 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1773 | ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPIt.getOperand()); |
Pete Cooper | cbf7390 | 2012-06-15 18:07:29 +0000 | [diff] [blame] | 1774 | if (!IdxVal) { |
| 1775 | // Non constant GEPs are only a problem on arrays, structs, and pointers |
| 1776 | // Vectors can be dynamically indexed. |
| 1777 | // FIXME: Add support for dynamic indexing on arrays. This should be |
| 1778 | // ok on any subarrays of the alloca array, eg, a[0][i] is ok, but a[i][0] |
| 1779 | // isn't. |
| 1780 | if (!(*GEPIt)->isVectorTy()) |
| 1781 | return MarkUnsafe(Info, GEPI); |
| 1782 | NonConstant = true; |
| 1783 | NonConstantIdxSize = TD->getTypeAllocSize(*GEPIt); |
| 1784 | } |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 1785 | } |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1786 | |
Bob Wilson | f27a4cd | 2009-12-22 06:57:14 +0000 | [diff] [blame] | 1787 | // Compute the offset due to this GEP and check if the alloca has a |
| 1788 | // component element at that offset. |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1789 | SmallVector<Value*, 8> Indices(GEPI->op_begin() + 1, GEPI->op_end()); |
Pete Cooper | cbf7390 | 2012-06-15 18:07:29 +0000 | [diff] [blame] | 1790 | // If this GEP is non constant then the last operand must have been a |
| 1791 | // dynamic index into a vector. Pop this now as it has no impact on the |
| 1792 | // constant part of the offset. |
| 1793 | if (NonConstant) |
| 1794 | Indices.pop_back(); |
Jay Foad | 8fbbb39 | 2011-07-19 14:01:37 +0000 | [diff] [blame] | 1795 | Offset += TD->getIndexedOffset(GEPI->getPointerOperandType(), Indices); |
Pete Cooper | cbf7390 | 2012-06-15 18:07:29 +0000 | [diff] [blame] | 1796 | if (!TypeHasComponent(Info.AI->getAllocatedType(), Offset, |
| 1797 | NonConstantIdxSize)) |
Chris Lattner | d01a0da | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1798 | MarkUnsafe(Info, GEPI); |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1801 | /// isHomogeneousAggregate - Check if type T is a struct or array containing |
| 1802 | /// elements of the same type (which is always true for arrays). If so, |
| 1803 | /// return true with NumElts and EltTy set to the number of elements and the |
| 1804 | /// element type, respectively. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1805 | static bool isHomogeneousAggregate(Type *T, unsigned &NumElts, |
| 1806 | Type *&EltTy) { |
| 1807 | if (ArrayType *AT = dyn_cast<ArrayType>(T)) { |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1808 | NumElts = AT->getNumElements(); |
Bob Wilson | f0908ae | 2011-01-13 18:26:59 +0000 | [diff] [blame] | 1809 | EltTy = (NumElts == 0 ? 0 : AT->getElementType()); |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1810 | return true; |
| 1811 | } |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1812 | if (StructType *ST = dyn_cast<StructType>(T)) { |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1813 | NumElts = ST->getNumContainedTypes(); |
Bob Wilson | f0908ae | 2011-01-13 18:26:59 +0000 | [diff] [blame] | 1814 | EltTy = (NumElts == 0 ? 0 : ST->getContainedType(0)); |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1815 | for (unsigned n = 1; n < NumElts; ++n) { |
| 1816 | if (ST->getContainedType(n) != EltTy) |
| 1817 | return false; |
| 1818 | } |
| 1819 | return true; |
| 1820 | } |
| 1821 | return false; |
| 1822 | } |
| 1823 | |
| 1824 | /// isCompatibleAggregate - Check if T1 and T2 are either the same type or are |
| 1825 | /// "homogeneous" aggregates with the same element type and number of elements. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1826 | static bool isCompatibleAggregate(Type *T1, Type *T2) { |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1827 | if (T1 == T2) |
| 1828 | return true; |
| 1829 | |
| 1830 | unsigned NumElts1, NumElts2; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1831 | Type *EltTy1, *EltTy2; |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1832 | if (isHomogeneousAggregate(T1, NumElts1, EltTy1) && |
| 1833 | isHomogeneousAggregate(T2, NumElts2, EltTy2) && |
| 1834 | NumElts1 == NumElts2 && |
| 1835 | EltTy1 == EltTy2) |
| 1836 | return true; |
| 1837 | |
| 1838 | return false; |
| 1839 | } |
| 1840 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1841 | /// isSafeMemAccess - Check if a load/store/memcpy operates on the entire AI |
| 1842 | /// alloca or has an offset and size that corresponds to a component element |
| 1843 | /// within it. The offset checked here may have been formed from a GEP with a |
| 1844 | /// pointer bitcasted to a different type. |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1845 | /// |
| 1846 | /// If AllowWholeAccess is true, then this allows uses of the entire alloca as a |
| 1847 | /// unit. If false, it only allows accesses known to be in a single element. |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1848 | void SROA::isSafeMemAccess(uint64_t Offset, uint64_t MemSize, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1849 | Type *MemOpType, bool isStore, |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1850 | AllocaInfo &Info, Instruction *TheAccess, |
| 1851 | bool AllowWholeAccess) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1852 | // Check if this is a load/store of the entire alloca. |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1853 | if (Offset == 0 && AllowWholeAccess && |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1854 | MemSize == TD->getTypeAllocSize(Info.AI->getAllocatedType())) { |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1855 | // This can be safe for MemIntrinsics (where MemOpType is 0) and integer |
| 1856 | // loads/stores (which are essentially the same as the MemIntrinsics with |
| 1857 | // regard to copying padding between elements). But, if an alloca is |
| 1858 | // flagged as both a source and destination of such operations, we'll need |
| 1859 | // to check later for padding between elements. |
| 1860 | if (!MemOpType || MemOpType->isIntegerTy()) { |
| 1861 | if (isStore) |
| 1862 | Info.isMemCpyDst = true; |
| 1863 | else |
| 1864 | Info.isMemCpySrc = true; |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1865 | return; |
| 1866 | } |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1867 | // This is also safe for references using a type that is compatible with |
| 1868 | // the type of the alloca, so that loads/stores can be rewritten using |
| 1869 | // insertvalue/extractvalue. |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1870 | if (isCompatibleAggregate(MemOpType, Info.AI->getAllocatedType())) { |
Chris Lattner | 7e9b427 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 1871 | Info.hasSubelementAccess = true; |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1872 | return; |
Chris Lattner | 7e9b427 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 1873 | } |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1874 | } |
| 1875 | // Check if the offset/size correspond to a component within the alloca type. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1876 | Type *T = Info.AI->getAllocatedType(); |
Chris Lattner | 7e9b427 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 1877 | if (TypeHasComponent(T, Offset, MemSize)) { |
| 1878 | Info.hasSubelementAccess = true; |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1879 | return; |
Chris Lattner | 7e9b427 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 1880 | } |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1881 | |
Chris Lattner | d01a0da | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1882 | return MarkUnsafe(Info, TheAccess); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | /// TypeHasComponent - Return true if T has a component type with the |
| 1886 | /// specified offset and size. If Size is zero, do not check the size. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1887 | bool SROA::TypeHasComponent(Type *T, uint64_t Offset, uint64_t Size) { |
| 1888 | Type *EltTy; |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1889 | uint64_t EltSize; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1890 | if (StructType *ST = dyn_cast<StructType>(T)) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1891 | const StructLayout *Layout = TD->getStructLayout(ST); |
| 1892 | unsigned EltIdx = Layout->getElementContainingOffset(Offset); |
| 1893 | EltTy = ST->getContainedType(EltIdx); |
| 1894 | EltSize = TD->getTypeAllocSize(EltTy); |
| 1895 | Offset -= Layout->getElementOffset(EltIdx); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1896 | } else if (ArrayType *AT = dyn_cast<ArrayType>(T)) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1897 | EltTy = AT->getElementType(); |
| 1898 | EltSize = TD->getTypeAllocSize(EltTy); |
Bob Wilson | f27a4cd | 2009-12-22 06:57:14 +0000 | [diff] [blame] | 1899 | if (Offset >= AT->getNumElements() * EltSize) |
| 1900 | return false; |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1901 | Offset %= EltSize; |
Pete Cooper | 6399b7c | 2012-06-14 23:53:53 +0000 | [diff] [blame] | 1902 | } else if (VectorType *VT = dyn_cast<VectorType>(T)) { |
| 1903 | EltTy = VT->getElementType(); |
| 1904 | EltSize = TD->getTypeAllocSize(EltTy); |
| 1905 | if (Offset >= VT->getNumElements() * EltSize) |
| 1906 | return false; |
| 1907 | Offset %= EltSize; |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1908 | } else { |
| 1909 | return false; |
| 1910 | } |
| 1911 | if (Offset == 0 && (Size == 0 || EltSize == Size)) |
| 1912 | return true; |
| 1913 | // Check if the component spans multiple elements. |
| 1914 | if (Offset + Size > EltSize) |
| 1915 | return false; |
| 1916 | return TypeHasComponent(EltTy, Offset, Size); |
| 1917 | } |
| 1918 | |
| 1919 | /// RewriteForScalarRepl - Alloca AI is being split into NewElts, so rewrite |
| 1920 | /// the instruction I, which references it, to use the separate elements. |
| 1921 | /// Offset indicates the position within AI that is referenced by this |
| 1922 | /// instruction. |
| 1923 | void SROA::RewriteForScalarRepl(Instruction *I, AllocaInst *AI, uint64_t Offset, |
| 1924 | SmallVector<AllocaInst*, 32> &NewElts) { |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1925 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI!=E;) { |
| 1926 | Use &TheUse = UI.getUse(); |
| 1927 | Instruction *User = cast<Instruction>(*UI++); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1928 | |
| 1929 | if (BitCastInst *BC = dyn_cast<BitCastInst>(User)) { |
| 1930 | RewriteBitCast(BC, AI, Offset, NewElts); |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1931 | continue; |
| 1932 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1933 | |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1934 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1935 | RewriteGEP(GEPI, AI, Offset, NewElts); |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1936 | continue; |
| 1937 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1938 | |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1939 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1940 | ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength()); |
| 1941 | uint64_t MemSize = Length->getZExtValue(); |
| 1942 | if (Offset == 0 && |
| 1943 | MemSize == TD->getTypeAllocSize(AI->getAllocatedType())) |
| 1944 | RewriteMemIntrinUserOfAlloca(MI, I, AI, NewElts); |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 1945 | // Otherwise the intrinsic can only touch a single element and the |
| 1946 | // address operand will be updated, so nothing else needs to be done. |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1947 | continue; |
| 1948 | } |
Nick Lewycky | 5a1cb64 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 1949 | |
| 1950 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(User)) { |
| 1951 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 1952 | II->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 1953 | RewriteLifetimeIntrinsic(II, AI, Offset, NewElts); |
| 1954 | } |
| 1955 | continue; |
| 1956 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1957 | |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1958 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1959 | Type *LIType = LI->getType(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1960 | |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1961 | if (isCompatibleAggregate(LIType, AI->getAllocatedType())) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1962 | // Replace: |
| 1963 | // %res = load { i32, i32 }* %alloc |
| 1964 | // with: |
| 1965 | // %load.0 = load i32* %alloc.0 |
| 1966 | // %insert.0 insertvalue { i32, i32 } zeroinitializer, i32 %load.0, 0 |
| 1967 | // %load.1 = load i32* %alloc.1 |
| 1968 | // %insert = insertvalue { i32, i32 } %insert.0, i32 %load.1, 1 |
| 1969 | // (Also works for arrays instead of structs) |
| 1970 | Value *Insert = UndefValue::get(LIType); |
Devang Patel | abb2512 | 2011-06-03 19:46:19 +0000 | [diff] [blame] | 1971 | IRBuilder<> Builder(LI); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1972 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
Devang Patel | abb2512 | 2011-06-03 19:46:19 +0000 | [diff] [blame] | 1973 | Value *Load = Builder.CreateLoad(NewElts[i], "load"); |
| 1974 | Insert = Builder.CreateInsertValue(Insert, Load, i, "insert"); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1975 | } |
| 1976 | LI->replaceAllUsesWith(Insert); |
| 1977 | DeadInsts.push_back(LI); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1978 | } else if (LIType->isIntegerTy() && |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1979 | TD->getTypeAllocSize(LIType) == |
| 1980 | TD->getTypeAllocSize(AI->getAllocatedType())) { |
| 1981 | // If this is a load of the entire alloca to an integer, rewrite it. |
| 1982 | RewriteLoadUserOfWholeAlloca(LI, AI, NewElts); |
| 1983 | } |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1984 | continue; |
| 1985 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1986 | |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1987 | if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1988 | Value *Val = SI->getOperand(0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1989 | Type *SIType = Val->getType(); |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1990 | if (isCompatibleAggregate(SIType, AI->getAllocatedType())) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1991 | // Replace: |
| 1992 | // store { i32, i32 } %val, { i32, i32 }* %alloc |
| 1993 | // with: |
| 1994 | // %val.0 = extractvalue { i32, i32 } %val, 0 |
| 1995 | // store i32 %val.0, i32* %alloc.0 |
| 1996 | // %val.1 = extractvalue { i32, i32 } %val, 1 |
| 1997 | // store i32 %val.1, i32* %alloc.1 |
| 1998 | // (Also works for arrays instead of structs) |
Devang Patel | abb2512 | 2011-06-03 19:46:19 +0000 | [diff] [blame] | 1999 | IRBuilder<> Builder(SI); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2000 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
Devang Patel | abb2512 | 2011-06-03 19:46:19 +0000 | [diff] [blame] | 2001 | Value *Extract = Builder.CreateExtractValue(Val, i, Val->getName()); |
| 2002 | Builder.CreateStore(Extract, NewElts[i]); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2003 | } |
| 2004 | DeadInsts.push_back(SI); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2005 | } else if (SIType->isIntegerTy() && |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2006 | TD->getTypeAllocSize(SIType) == |
| 2007 | TD->getTypeAllocSize(AI->getAllocatedType())) { |
| 2008 | // If this is a store of the entire alloca from an integer, rewrite it. |
| 2009 | RewriteStoreUserOfWholeAlloca(SI, AI, NewElts); |
| 2010 | } |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 2011 | continue; |
| 2012 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2013 | |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 2014 | if (isa<SelectInst>(User) || isa<PHINode>(User)) { |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2015 | // If we have a PHI user of the alloca itself (as opposed to a GEP or |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 2016 | // bitcast) we have to rewrite it. GEP and bitcast uses will be RAUW'd to |
| 2017 | // the new pointer. |
| 2018 | if (!isa<AllocaInst>(I)) continue; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2019 | |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 2020 | assert(Offset == 0 && NewElts[0] && |
| 2021 | "Direct alloca use should have a zero offset"); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2022 | |
Chris Lattner | 145c532 | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 2023 | // If we have a use of the alloca, we know the derived uses will be |
| 2024 | // utilizing just the first element of the scalarized result. Insert a |
| 2025 | // bitcast of the first alloca before the user as required. |
| 2026 | AllocaInst *NewAI = NewElts[0]; |
| 2027 | BitCastInst *BCI = new BitCastInst(NewAI, AI->getType(), "", NewAI); |
| 2028 | NewAI->moveBefore(BCI); |
| 2029 | TheUse = BCI; |
| 2030 | continue; |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2031 | } |
Bob Wilson | 39c88a6 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 2032 | } |
| 2033 | } |
| 2034 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2035 | /// RewriteBitCast - Update a bitcast reference to the alloca being replaced |
| 2036 | /// and recursively continue updating all of its uses. |
| 2037 | void SROA::RewriteBitCast(BitCastInst *BC, AllocaInst *AI, uint64_t Offset, |
| 2038 | SmallVector<AllocaInst*, 32> &NewElts) { |
| 2039 | RewriteForScalarRepl(BC, AI, Offset, NewElts); |
| 2040 | if (BC->getOperand(0) != AI) |
| 2041 | return; |
Bob Wilson | 39c88a6 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 2042 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2043 | // The bitcast references the original alloca. Replace its uses with |
Eli Friedman | 75f69e3 | 2011-11-12 02:07:50 +0000 | [diff] [blame] | 2044 | // references to the alloca containing offset zero (which is normally at |
| 2045 | // index zero, but might not be in cases involving structs with elements |
| 2046 | // of size zero). |
| 2047 | Type *T = AI->getAllocatedType(); |
| 2048 | uint64_t EltOffset = 0; |
| 2049 | Type *IdxTy; |
| 2050 | uint64_t Idx = FindElementAndOffset(T, EltOffset, IdxTy); |
| 2051 | Instruction *Val = NewElts[Idx]; |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2052 | if (Val->getType() != BC->getDestTy()) { |
| 2053 | Val = new BitCastInst(Val, BC->getDestTy(), "", BC); |
| 2054 | Val->takeName(BC); |
Daniel Dunbar | fca55c8 | 2009-12-16 10:56:17 +0000 | [diff] [blame] | 2055 | } |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2056 | BC->replaceAllUsesWith(Val); |
| 2057 | DeadInsts.push_back(BC); |
Daniel Dunbar | fca55c8 | 2009-12-16 10:56:17 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2060 | /// FindElementAndOffset - Return the index of the element containing Offset |
| 2061 | /// within the specified type, which must be either a struct or an array. |
| 2062 | /// 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] | 2063 | /// element. IdxTy is set to the type of the index result to be used in a |
| 2064 | /// GEP instruction. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2065 | uint64_t SROA::FindElementAndOffset(Type *&T, uint64_t &Offset, |
| 2066 | Type *&IdxTy) { |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 2067 | uint64_t Idx = 0; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2068 | if (StructType *ST = dyn_cast<StructType>(T)) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2069 | const StructLayout *Layout = TD->getStructLayout(ST); |
| 2070 | Idx = Layout->getElementContainingOffset(Offset); |
| 2071 | T = ST->getContainedType(Idx); |
| 2072 | Offset -= Layout->getElementOffset(Idx); |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 2073 | IdxTy = Type::getInt32Ty(T->getContext()); |
| 2074 | return Idx; |
Pete Cooper | 6399b7c | 2012-06-14 23:53:53 +0000 | [diff] [blame] | 2075 | } else if (ArrayType *AT = dyn_cast<ArrayType>(T)) { |
| 2076 | T = AT->getElementType(); |
| 2077 | uint64_t EltSize = TD->getTypeAllocSize(T); |
| 2078 | Idx = Offset / EltSize; |
| 2079 | Offset -= Idx * EltSize; |
| 2080 | IdxTy = Type::getInt64Ty(T->getContext()); |
| 2081 | return Idx; |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 2082 | } |
Pete Cooper | 6399b7c | 2012-06-14 23:53:53 +0000 | [diff] [blame] | 2083 | VectorType *VT = cast<VectorType>(T); |
| 2084 | T = VT->getElementType(); |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 2085 | uint64_t EltSize = TD->getTypeAllocSize(T); |
| 2086 | Idx = Offset / EltSize; |
| 2087 | Offset -= Idx * EltSize; |
| 2088 | IdxTy = Type::getInt64Ty(T->getContext()); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2089 | return Idx; |
| 2090 | } |
| 2091 | |
| 2092 | /// RewriteGEP - Check if this GEP instruction moves the pointer across |
| 2093 | /// elements of the alloca that are being split apart, and if so, rewrite |
| 2094 | /// the GEP to be relative to the new element. |
| 2095 | void SROA::RewriteGEP(GetElementPtrInst *GEPI, AllocaInst *AI, uint64_t Offset, |
| 2096 | SmallVector<AllocaInst*, 32> &NewElts) { |
| 2097 | uint64_t OldOffset = Offset; |
| 2098 | SmallVector<Value*, 8> Indices(GEPI->op_begin() + 1, GEPI->op_end()); |
Pete Cooper | cbf7390 | 2012-06-15 18:07:29 +0000 | [diff] [blame] | 2099 | // If the GEP was dynamic then it must have been a dynamic vector lookup. |
| 2100 | // In this case, it must be the last GEP operand which is dynamic so keep that |
| 2101 | // aside until we've found the constant GEP offset then add it back in at the |
| 2102 | // end. |
| 2103 | Value* NonConstantIdx = 0; |
| 2104 | if (!GEPI->hasAllConstantIndices()) |
| 2105 | NonConstantIdx = Indices.pop_back_val(); |
Jay Foad | 8fbbb39 | 2011-07-19 14:01:37 +0000 | [diff] [blame] | 2106 | Offset += TD->getIndexedOffset(GEPI->getPointerOperandType(), Indices); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2107 | |
| 2108 | RewriteForScalarRepl(GEPI, AI, Offset, NewElts); |
| 2109 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2110 | Type *T = AI->getAllocatedType(); |
| 2111 | Type *IdxTy; |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 2112 | uint64_t OldIdx = FindElementAndOffset(T, OldOffset, IdxTy); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2113 | if (GEPI->getOperand(0) == AI) |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 2114 | OldIdx = ~0ULL; // Force the GEP to be rewritten. |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2115 | |
| 2116 | T = AI->getAllocatedType(); |
| 2117 | uint64_t EltOffset = Offset; |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 2118 | uint64_t Idx = FindElementAndOffset(T, EltOffset, IdxTy); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2119 | |
| 2120 | // If this GEP does not move the pointer across elements of the alloca |
| 2121 | // being split, then it does not needs to be rewritten. |
| 2122 | if (Idx == OldIdx) |
| 2123 | return; |
| 2124 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2125 | Type *i32Ty = Type::getInt32Ty(AI->getContext()); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2126 | SmallVector<Value*, 8> NewArgs; |
| 2127 | NewArgs.push_back(Constant::getNullValue(i32Ty)); |
| 2128 | while (EltOffset != 0) { |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 2129 | uint64_t EltIdx = FindElementAndOffset(T, EltOffset, IdxTy); |
| 2130 | NewArgs.push_back(ConstantInt::get(IdxTy, EltIdx)); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2131 | } |
Pete Cooper | 06e6c38 | 2012-06-16 01:43:26 +0000 | [diff] [blame] | 2132 | if (NonConstantIdx) { |
| 2133 | Type* GepTy = T; |
| 2134 | // This GEP has a dynamic index. We need to add "i32 0" to index through |
| 2135 | // any structs or arrays in the original type until we get to the vector |
| 2136 | // to index. |
| 2137 | while (!isa<VectorType>(GepTy)) { |
| 2138 | NewArgs.push_back(Constant::getNullValue(i32Ty)); |
| 2139 | GepTy = cast<CompositeType>(GepTy)->getTypeAtIndex(0U); |
| 2140 | } |
Pete Cooper | cbf7390 | 2012-06-15 18:07:29 +0000 | [diff] [blame] | 2141 | NewArgs.push_back(NonConstantIdx); |
Pete Cooper | 06e6c38 | 2012-06-16 01:43:26 +0000 | [diff] [blame] | 2142 | } |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2143 | Instruction *Val = NewElts[Idx]; |
| 2144 | if (NewArgs.size() > 1) { |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 2145 | Val = GetElementPtrInst::CreateInBounds(Val, NewArgs, "", GEPI); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2146 | Val->takeName(GEPI); |
| 2147 | } |
| 2148 | if (Val->getType() != GEPI->getType()) |
Benjamin Kramer | 2d64ca0 | 2010-01-27 19:46:52 +0000 | [diff] [blame] | 2149 | Val = new BitCastInst(Val, GEPI->getType(), Val->getName(), GEPI); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2150 | GEPI->replaceAllUsesWith(Val); |
| 2151 | DeadInsts.push_back(GEPI); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Nick Lewycky | 5a1cb64 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 2154 | /// RewriteLifetimeIntrinsic - II is a lifetime.start/lifetime.end. Rewrite it |
| 2155 | /// to mark the lifetime of the scalarized memory. |
| 2156 | void SROA::RewriteLifetimeIntrinsic(IntrinsicInst *II, AllocaInst *AI, |
| 2157 | uint64_t Offset, |
| 2158 | SmallVector<AllocaInst*, 32> &NewElts) { |
| 2159 | ConstantInt *OldSize = cast<ConstantInt>(II->getArgOperand(0)); |
| 2160 | // Put matching lifetime markers on everything from Offset up to |
| 2161 | // Offset+OldSize. |
| 2162 | Type *AIType = AI->getAllocatedType(); |
| 2163 | uint64_t NewOffset = Offset; |
| 2164 | Type *IdxTy; |
| 2165 | uint64_t Idx = FindElementAndOffset(AIType, NewOffset, IdxTy); |
| 2166 | |
| 2167 | IRBuilder<> Builder(II); |
| 2168 | uint64_t Size = OldSize->getLimitedValue(); |
| 2169 | |
| 2170 | if (NewOffset) { |
| 2171 | // Splice the first element and index 'NewOffset' bytes in. SROA will |
| 2172 | // split the alloca again later. |
| 2173 | Value *V = Builder.CreateBitCast(NewElts[Idx], Builder.getInt8PtrTy()); |
| 2174 | V = Builder.CreateGEP(V, Builder.getInt64(NewOffset)); |
| 2175 | |
| 2176 | IdxTy = NewElts[Idx]->getAllocatedType(); |
| 2177 | uint64_t EltSize = TD->getTypeAllocSize(IdxTy) - NewOffset; |
| 2178 | if (EltSize > Size) { |
| 2179 | EltSize = Size; |
| 2180 | Size = 0; |
| 2181 | } else { |
| 2182 | Size -= EltSize; |
| 2183 | } |
| 2184 | if (II->getIntrinsicID() == Intrinsic::lifetime_start) |
| 2185 | Builder.CreateLifetimeStart(V, Builder.getInt64(EltSize)); |
| 2186 | else |
| 2187 | Builder.CreateLifetimeEnd(V, Builder.getInt64(EltSize)); |
| 2188 | ++Idx; |
| 2189 | } |
| 2190 | |
| 2191 | for (; Idx != NewElts.size() && Size; ++Idx) { |
| 2192 | IdxTy = NewElts[Idx]->getAllocatedType(); |
| 2193 | uint64_t EltSize = TD->getTypeAllocSize(IdxTy); |
| 2194 | if (EltSize > Size) { |
| 2195 | EltSize = Size; |
| 2196 | Size = 0; |
| 2197 | } else { |
| 2198 | Size -= EltSize; |
| 2199 | } |
| 2200 | if (II->getIntrinsicID() == Intrinsic::lifetime_start) |
| 2201 | Builder.CreateLifetimeStart(NewElts[Idx], |
| 2202 | Builder.getInt64(EltSize)); |
| 2203 | else |
| 2204 | Builder.CreateLifetimeEnd(NewElts[Idx], |
| 2205 | Builder.getInt64(EltSize)); |
| 2206 | } |
| 2207 | DeadInsts.push_back(II); |
| 2208 | } |
| 2209 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2210 | /// RewriteMemIntrinUserOfAlloca - MI is a memcpy/memset/memmove from or to AI. |
| 2211 | /// Rewrite it to copy or set the elements of the scalarized memory. |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2212 | void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *Inst, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 2213 | AllocaInst *AI, |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2214 | SmallVector<AllocaInst*, 32> &NewElts) { |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2215 | // If this is a memcpy/memmove, construct the other pointer as the |
Chris Lattner | 88fe1ad | 2009-03-04 19:23:25 +0000 | [diff] [blame] | 2216 | // appropriate type. The "Other" pointer is the pointer that goes to memory |
| 2217 | // that doesn't have anything to do with the alloca that we are promoting. For |
| 2218 | // memset, this Value* stays null. |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2219 | Value *OtherPtr = 0; |
Chris Lattner | dfe964c | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 2220 | unsigned MemAlignment = MI->getAlignment(); |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 2221 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { // memmove/memcopy |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2222 | if (Inst == MTI->getRawDest()) |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 2223 | OtherPtr = MTI->getRawSource(); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2224 | else { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2225 | assert(Inst == MTI->getRawSource()); |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 2226 | OtherPtr = MTI->getRawDest(); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2227 | } |
| 2228 | } |
Bob Wilson | 78c50b8 | 2009-12-08 18:22:03 +0000 | [diff] [blame] | 2229 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2230 | // If there is an other pointer, we want to convert it to the same pointer |
| 2231 | // type as AI has, so we can GEP through it safely. |
| 2232 | if (OtherPtr) { |
Chris Lattner | 0238f8c | 2010-07-08 00:27:05 +0000 | [diff] [blame] | 2233 | unsigned AddrSpace = |
| 2234 | cast<PointerType>(OtherPtr->getType())->getAddressSpace(); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2235 | |
| 2236 | // Remove bitcasts and all-zero GEPs from OtherPtr. This is an |
| 2237 | // optimization, but it's also required to detect the corner case where |
| 2238 | // both pointer operands are referencing the same memory, and where |
| 2239 | // OtherPtr may be a bitcast or GEP that currently being rewritten. (This |
| 2240 | // function is only called for mem intrinsics that access the whole |
| 2241 | // aggregate, so non-zero GEPs are not an issue here.) |
Chris Lattner | 0238f8c | 2010-07-08 00:27:05 +0000 | [diff] [blame] | 2242 | OtherPtr = OtherPtr->stripPointerCasts(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2243 | |
Bob Wilson | a756b1d | 2010-01-19 04:32:48 +0000 | [diff] [blame] | 2244 | // Copying the alloca to itself is a no-op: just delete it. |
| 2245 | if (OtherPtr == AI || OtherPtr == NewElts[0]) { |
| 2246 | // This code will run twice for a no-op memcpy -- once for each operand. |
| 2247 | // Put only one reference to MI on the DeadInsts list. |
| 2248 | for (SmallVector<Value*, 32>::const_iterator I = DeadInsts.begin(), |
| 2249 | E = DeadInsts.end(); I != E; ++I) |
| 2250 | if (*I == MI) return; |
| 2251 | DeadInsts.push_back(MI); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2252 | return; |
Bob Wilson | a756b1d | 2010-01-19 04:32:48 +0000 | [diff] [blame] | 2253 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2254 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2255 | // If the pointer is not the right type, insert a bitcast to the right |
| 2256 | // type. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2257 | Type *NewTy = |
Chris Lattner | 0238f8c | 2010-07-08 00:27:05 +0000 | [diff] [blame] | 2258 | PointerType::get(AI->getType()->getElementType(), AddrSpace); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2259 | |
Chris Lattner | 0238f8c | 2010-07-08 00:27:05 +0000 | [diff] [blame] | 2260 | if (OtherPtr->getType() != NewTy) |
| 2261 | OtherPtr = new BitCastInst(OtherPtr, NewTy, OtherPtr->getName(), MI); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2262 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2263 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2264 | // Process each element of the aggregate. |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2265 | bool SROADest = MI->getRawDest() == Inst; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2266 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2267 | Constant *Zero = Constant::getNullValue(Type::getInt32Ty(MI->getContext())); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2268 | |
| 2269 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 2270 | // If this is a memcpy/memmove, emit a GEP of the other element address. |
| 2271 | Value *OtherElt = 0; |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 2272 | unsigned OtherEltAlign = MemAlignment; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2273 | |
Bob Wilson | a756b1d | 2010-01-19 04:32:48 +0000 | [diff] [blame] | 2274 | if (OtherPtr) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2275 | Value *Idx[2] = { Zero, |
| 2276 | ConstantInt::get(Type::getInt32Ty(MI->getContext()), i) }; |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 2277 | OtherElt = GetElementPtrInst::CreateInBounds(OtherPtr, Idx, |
Benjamin Kramer | 2d64ca0 | 2010-01-27 19:46:52 +0000 | [diff] [blame] | 2278 | OtherPtr->getName()+"."+Twine(i), |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2279 | MI); |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 2280 | uint64_t EltOffset; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2281 | PointerType *OtherPtrTy = cast<PointerType>(OtherPtr->getType()); |
| 2282 | Type *OtherTy = OtherPtrTy->getElementType(); |
| 2283 | if (StructType *ST = dyn_cast<StructType>(OtherTy)) { |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 2284 | EltOffset = TD->getStructLayout(ST)->getElementOffset(i); |
| 2285 | } else { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2286 | Type *EltTy = cast<SequentialType>(OtherTy)->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2287 | EltOffset = TD->getTypeAllocSize(EltTy)*i; |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 2288 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2289 | |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 2290 | // The alignment of the other pointer is the guaranteed alignment of the |
| 2291 | // element, which is affected by both the known alignment of the whole |
| 2292 | // mem intrinsic and the alignment of the element. If the alignment of |
| 2293 | // the memcpy (f.e.) is 32 but the element is at a 4-byte offset, then the |
| 2294 | // known alignment is just 4 bytes. |
| 2295 | OtherEltAlign = (unsigned)MinAlign(OtherEltAlign, EltOffset); |
Chris Lattner | c14d3ca | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 2296 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2297 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2298 | Value *EltPtr = NewElts[i]; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2299 | Type *EltTy = cast<PointerType>(EltPtr->getType())->getElementType(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2300 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2301 | // If we got down to a scalar, insert a load or store as appropriate. |
| 2302 | if (EltTy->isSingleValueType()) { |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 2303 | if (isa<MemTransferInst>(MI)) { |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 2304 | if (SROADest) { |
| 2305 | // From Other to Alloca. |
| 2306 | Value *Elt = new LoadInst(OtherElt, "tmp", false, OtherEltAlign, MI); |
| 2307 | new StoreInst(Elt, EltPtr, MI); |
| 2308 | } else { |
| 2309 | // From Alloca to Other. |
| 2310 | Value *Elt = new LoadInst(EltPtr, "tmp", MI); |
| 2311 | new StoreInst(Elt, OtherElt, false, OtherEltAlign, MI); |
| 2312 | } |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2313 | continue; |
| 2314 | } |
| 2315 | assert(isa<MemSetInst>(MI)); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2316 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2317 | // If the stored element is zero (common case), just store a null |
| 2318 | // constant. |
| 2319 | Constant *StoreVal; |
Gabor Greif | 6f14c8c | 2010-06-30 09:16:16 +0000 | [diff] [blame] | 2320 | if (ConstantInt *CI = dyn_cast<ConstantInt>(MI->getArgOperand(1))) { |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2321 | if (CI->isZero()) { |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2322 | StoreVal = Constant::getNullValue(EltTy); // 0.0, null, 0, <0,0> |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2323 | } else { |
| 2324 | // If EltTy is a vector type, get the element type. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2325 | Type *ValTy = EltTy->getScalarType(); |
Dan Gohman | 44118f0 | 2009-06-16 00:20:26 +0000 | [diff] [blame] | 2326 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2327 | // Construct an integer with the right value. |
| 2328 | unsigned EltSize = TD->getTypeSizeInBits(ValTy); |
| 2329 | APInt OneVal(EltSize, CI->getZExtValue()); |
| 2330 | APInt TotalVal(OneVal); |
| 2331 | // Set each byte. |
| 2332 | for (unsigned i = 0; 8*i < EltSize; ++i) { |
| 2333 | TotalVal = TotalVal.shl(8); |
| 2334 | TotalVal |= OneVal; |
| 2335 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2336 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2337 | // Convert the integer value to the appropriate type. |
Chris Lattner | d55c1c1 | 2010-04-16 01:05:38 +0000 | [diff] [blame] | 2338 | StoreVal = ConstantInt::get(CI->getContext(), TotalVal); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2339 | if (ValTy->isPointerTy()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2340 | StoreVal = ConstantExpr::getIntToPtr(StoreVal, ValTy); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2341 | else if (ValTy->isFloatingPointTy()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2342 | StoreVal = ConstantExpr::getBitCast(StoreVal, ValTy); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2343 | assert(StoreVal->getType() == ValTy && "Type mismatch!"); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2344 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2345 | // If the requested value was a vector constant, create it. |
Cameron Zwarich | c055a87 | 2011-10-11 21:26:40 +0000 | [diff] [blame] | 2346 | if (EltTy->isVectorTy()) { |
| 2347 | unsigned NumElts = cast<VectorType>(EltTy)->getNumElements(); |
Chris Lattner | 4ca829e | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2348 | StoreVal = ConstantVector::getSplat(NumElts, StoreVal); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2349 | } |
| 2350 | } |
| 2351 | new StoreInst(StoreVal, EltPtr, MI); |
| 2352 | continue; |
| 2353 | } |
| 2354 | // Otherwise, if we're storing a byte variable, use a memset call for |
| 2355 | // this element. |
| 2356 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2357 | |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2358 | unsigned EltSize = TD->getTypeAllocSize(EltTy); |
Eli Friedman | 75f69e3 | 2011-11-12 02:07:50 +0000 | [diff] [blame] | 2359 | if (!EltSize) |
| 2360 | continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2361 | |
Chris Lattner | 61db1f5 | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 2362 | IRBuilder<> Builder(MI); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2363 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2364 | // Finally, insert the meminst for this element. |
Chris Lattner | 61db1f5 | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 2365 | if (isa<MemSetInst>(MI)) { |
| 2366 | Builder.CreateMemSet(EltPtr, MI->getArgOperand(1), EltSize, |
| 2367 | MI->isVolatile()); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2368 | } else { |
Chris Lattner | 61db1f5 | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 2369 | assert(isa<MemTransferInst>(MI)); |
| 2370 | Value *Dst = SROADest ? EltPtr : OtherElt; // Dest ptr |
| 2371 | Value *Src = SROADest ? OtherElt : EltPtr; // Src ptr |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2372 | |
Chris Lattner | 61db1f5 | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 2373 | if (isa<MemCpyInst>(MI)) |
| 2374 | Builder.CreateMemCpy(Dst, Src, EltSize, OtherEltAlign,MI->isVolatile()); |
| 2375 | else |
| 2376 | Builder.CreateMemMove(Dst, Src, EltSize,OtherEltAlign,MI->isVolatile()); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2377 | } |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 2378 | } |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2379 | DeadInsts.push_back(MI); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 2380 | } |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2381 | |
Bob Wilson | 39fdd69 | 2009-12-04 21:57:37 +0000 | [diff] [blame] | 2382 | /// RewriteStoreUserOfWholeAlloca - We found a store of an integer that |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2383 | /// overwrites the entire allocation. Extract out the pieces of the stored |
| 2384 | /// integer and store them individually. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 2385 | void SROA::RewriteStoreUserOfWholeAlloca(StoreInst *SI, AllocaInst *AI, |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2386 | SmallVector<AllocaInst*, 32> &NewElts){ |
| 2387 | // Extract each element out of the integer according to its structure offset |
| 2388 | // and store the element value to the individual alloca. |
| 2389 | Value *SrcVal = SI->getOperand(0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2390 | Type *AllocaEltTy = AI->getAllocatedType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2391 | uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2392 | |
Chris Lattner | 7072853 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2393 | IRBuilder<> Builder(SI); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2394 | |
Eli Friedman | 41b33f4 | 2009-06-01 09:14:32 +0000 | [diff] [blame] | 2395 | // Handle tail padding by extending the operand |
| 2396 | if (TD->getTypeSizeInBits(SrcVal->getType()) != AllocaSizeBits) |
Chris Lattner | 7072853 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2397 | SrcVal = Builder.CreateZExt(SrcVal, |
| 2398 | IntegerType::get(SI->getContext(), AllocaSizeBits)); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2399 | |
David Greene | 504c7d8 | 2010-01-05 01:27:09 +0000 | [diff] [blame] | 2400 | DEBUG(dbgs() << "PROMOTING STORE TO WHOLE ALLOCA: " << *AI << '\n' << *SI |
Nick Lewycky | 5913625 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 2401 | << '\n'); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2402 | |
| 2403 | // There are two forms here: AI could be an array or struct. Both cases |
| 2404 | // have different ways to compute the element offset. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2405 | if (StructType *EltSTy = dyn_cast<StructType>(AllocaEltTy)) { |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2406 | const StructLayout *Layout = TD->getStructLayout(EltSTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2407 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2408 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 2409 | // Get the number of bits to shift SrcVal to get the value. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2410 | Type *FieldTy = EltSTy->getElementType(i); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2411 | uint64_t Shift = Layout->getElementOffsetInBits(i); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2412 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2413 | if (TD->isBigEndian()) |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2414 | Shift = AllocaSizeBits-Shift-TD->getTypeAllocSizeInBits(FieldTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2415 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2416 | Value *EltVal = SrcVal; |
| 2417 | if (Shift) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2418 | Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift); |
Chris Lattner | 7072853 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2419 | EltVal = Builder.CreateLShr(EltVal, ShiftVal, "sroa.store.elt"); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2420 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2421 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2422 | // Truncate down to an integer of the right size. |
| 2423 | uint64_t FieldSizeBits = TD->getTypeSizeInBits(FieldTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2424 | |
Chris Lattner | 583dd60 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 2425 | // Ignore zero sized fields like {}, they obviously contain no data. |
| 2426 | if (FieldSizeBits == 0) continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2427 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2428 | if (FieldSizeBits != AllocaSizeBits) |
Chris Lattner | 7072853 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2429 | EltVal = Builder.CreateTrunc(EltVal, |
| 2430 | IntegerType::get(SI->getContext(), FieldSizeBits)); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2431 | Value *DestField = NewElts[i]; |
| 2432 | if (EltVal->getType() == FieldTy) { |
| 2433 | // Storing to an integer field of this size, just do it. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2434 | } else if (FieldTy->isFloatingPointTy() || FieldTy->isVectorTy()) { |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2435 | // Bitcast to the right element type (for fp/vector values). |
Chris Lattner | 7072853 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2436 | EltVal = Builder.CreateBitCast(EltVal, FieldTy); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2437 | } else { |
| 2438 | // Otherwise, bitcast the dest pointer (for aggregates). |
Chris Lattner | 7072853 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2439 | DestField = Builder.CreateBitCast(DestField, |
| 2440 | PointerType::getUnqual(EltVal->getType())); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2441 | } |
| 2442 | new StoreInst(EltVal, DestField, SI); |
| 2443 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2444 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2445 | } else { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2446 | ArrayType *ATy = cast<ArrayType>(AllocaEltTy); |
| 2447 | Type *ArrayEltTy = ATy->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2448 | uint64_t ElementOffset = TD->getTypeAllocSizeInBits(ArrayEltTy); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2449 | uint64_t ElementSizeBits = TD->getTypeSizeInBits(ArrayEltTy); |
| 2450 | |
| 2451 | uint64_t Shift; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2452 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2453 | if (TD->isBigEndian()) |
| 2454 | Shift = AllocaSizeBits-ElementOffset; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2455 | else |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2456 | Shift = 0; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2457 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2458 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
Chris Lattner | 583dd60 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 2459 | // Ignore zero sized fields like {}, they obviously contain no data. |
| 2460 | if (ElementSizeBits == 0) continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2461 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2462 | Value *EltVal = SrcVal; |
| 2463 | if (Shift) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2464 | Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift); |
Chris Lattner | 7072853 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2465 | EltVal = Builder.CreateLShr(EltVal, ShiftVal, "sroa.store.elt"); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2466 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2467 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2468 | // Truncate down to an integer of the right size. |
| 2469 | if (ElementSizeBits != AllocaSizeBits) |
Chris Lattner | 7072853 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2470 | EltVal = Builder.CreateTrunc(EltVal, |
| 2471 | IntegerType::get(SI->getContext(), |
| 2472 | ElementSizeBits)); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2473 | Value *DestField = NewElts[i]; |
| 2474 | if (EltVal->getType() == ArrayEltTy) { |
| 2475 | // Storing to an integer field of this size, just do it. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2476 | } else if (ArrayEltTy->isFloatingPointTy() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2477 | ArrayEltTy->isVectorTy()) { |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2478 | // Bitcast to the right element type (for fp/vector values). |
Chris Lattner | 7072853 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2479 | EltVal = Builder.CreateBitCast(EltVal, ArrayEltTy); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2480 | } else { |
| 2481 | // Otherwise, bitcast the dest pointer (for aggregates). |
Chris Lattner | 7072853 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2482 | DestField = Builder.CreateBitCast(DestField, |
| 2483 | PointerType::getUnqual(EltVal->getType())); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2484 | } |
| 2485 | new StoreInst(EltVal, DestField, SI); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2486 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2487 | if (TD->isBigEndian()) |
| 2488 | Shift -= ElementOffset; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2489 | else |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2490 | Shift += ElementOffset; |
| 2491 | } |
| 2492 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2493 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2494 | DeadInsts.push_back(SI); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
Bob Wilson | 39fdd69 | 2009-12-04 21:57:37 +0000 | [diff] [blame] | 2497 | /// RewriteLoadUserOfWholeAlloca - We found a load of the entire allocation to |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2498 | /// an integer. Load the individual pieces to form the aggregate value. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 2499 | void SROA::RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocaInst *AI, |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2500 | SmallVector<AllocaInst*, 32> &NewElts) { |
| 2501 | // Extract each element out of the NewElts according to its structure offset |
| 2502 | // and form the result value. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2503 | Type *AllocaEltTy = AI->getAllocatedType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2504 | uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2505 | |
David Greene | 504c7d8 | 2010-01-05 01:27:09 +0000 | [diff] [blame] | 2506 | DEBUG(dbgs() << "PROMOTING LOAD OF WHOLE ALLOCA: " << *AI << '\n' << *LI |
Nick Lewycky | 5913625 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 2507 | << '\n'); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2508 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2509 | // There are two forms here: AI could be an array or struct. Both cases |
| 2510 | // have different ways to compute the element offset. |
| 2511 | const StructLayout *Layout = 0; |
| 2512 | uint64_t ArrayEltBitOffset = 0; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2513 | if (StructType *EltSTy = dyn_cast<StructType>(AllocaEltTy)) { |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2514 | Layout = TD->getStructLayout(EltSTy); |
| 2515 | } else { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2516 | Type *ArrayEltTy = cast<ArrayType>(AllocaEltTy)->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2517 | ArrayEltBitOffset = TD->getTypeAllocSizeInBits(ArrayEltTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | Value *ResultVal = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2521 | Constant::getNullValue(IntegerType::get(LI->getContext(), AllocaSizeBits)); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2522 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2523 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 2524 | // Load the value from the alloca. If the NewElt is an aggregate, cast |
| 2525 | // the pointer to an integer of the same size before doing the load. |
| 2526 | Value *SrcField = NewElts[i]; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2527 | Type *FieldTy = |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2528 | cast<PointerType>(SrcField->getType())->getElementType(); |
Chris Lattner | 583dd60 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 2529 | uint64_t FieldSizeBits = TD->getTypeSizeInBits(FieldTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2530 | |
Chris Lattner | 583dd60 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 2531 | // Ignore zero sized fields like {}, they obviously contain no data. |
| 2532 | if (FieldSizeBits == 0) continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2533 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2534 | IntegerType *FieldIntTy = IntegerType::get(LI->getContext(), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2535 | FieldSizeBits); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2536 | if (!FieldTy->isIntegerTy() && !FieldTy->isFloatingPointTy() && |
| 2537 | !FieldTy->isVectorTy()) |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 2538 | SrcField = new BitCastInst(SrcField, |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 2539 | PointerType::getUnqual(FieldIntTy), |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2540 | "", LI); |
| 2541 | SrcField = new LoadInst(SrcField, "sroa.load.elt", LI); |
| 2542 | |
| 2543 | // If SrcField is a fp or vector of the right size but that isn't an |
| 2544 | // integer type, bitcast to an integer so we can shift it. |
| 2545 | if (SrcField->getType() != FieldIntTy) |
| 2546 | SrcField = new BitCastInst(SrcField, FieldIntTy, "", LI); |
| 2547 | |
| 2548 | // Zero extend the field to be the same size as the final alloca so that |
| 2549 | // we can shift and insert it. |
| 2550 | if (SrcField->getType() != ResultVal->getType()) |
| 2551 | SrcField = new ZExtInst(SrcField, ResultVal->getType(), "", LI); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2552 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2553 | // Determine the number of bits to shift SrcField. |
| 2554 | uint64_t Shift; |
| 2555 | if (Layout) // Struct case. |
| 2556 | Shift = Layout->getElementOffsetInBits(i); |
| 2557 | else // Array case. |
| 2558 | Shift = i*ArrayEltBitOffset; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2559 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2560 | if (TD->isBigEndian()) |
| 2561 | Shift = AllocaSizeBits-Shift-FieldIntTy->getBitWidth(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2562 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2563 | if (Shift) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2564 | Value *ShiftVal = ConstantInt::get(SrcField->getType(), Shift); |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2565 | SrcField = BinaryOperator::CreateShl(SrcField, ShiftVal, "", LI); |
| 2566 | } |
| 2567 | |
Chris Lattner | 1495247 | 2010-06-27 07:58:26 +0000 | [diff] [blame] | 2568 | // Don't create an 'or x, 0' on the first iteration. |
| 2569 | if (!isa<Constant>(ResultVal) || |
| 2570 | !cast<Constant>(ResultVal)->isNullValue()) |
| 2571 | ResultVal = BinaryOperator::CreateOr(SrcField, ResultVal, "", LI); |
| 2572 | else |
| 2573 | ResultVal = SrcField; |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2574 | } |
Eli Friedman | 41b33f4 | 2009-06-01 09:14:32 +0000 | [diff] [blame] | 2575 | |
| 2576 | // Handle tail padding by truncating the result |
| 2577 | if (TD->getTypeSizeInBits(LI->getType()) != AllocaSizeBits) |
| 2578 | ResultVal = new TruncInst(ResultVal, LI->getType(), "", LI); |
| 2579 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2580 | LI->replaceAllUsesWith(ResultVal); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2581 | DeadInsts.push_back(LI); |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 2584 | /// HasPadding - Return true if the specified type has any structure or |
Bob Wilson | 694a10e | 2011-01-13 17:45:08 +0000 | [diff] [blame] | 2585 | /// alignment padding in between the elements that would be split apart |
| 2586 | /// by SROA; return false otherwise. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2587 | static bool HasPadding(Type *Ty, const TargetData &TD) { |
| 2588 | if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
Bob Wilson | 694a10e | 2011-01-13 17:45:08 +0000 | [diff] [blame] | 2589 | Ty = ATy->getElementType(); |
| 2590 | return TD.getTypeSizeInBits(Ty) != TD.getTypeAllocSizeInBits(Ty); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 2591 | } |
Bob Wilson | 694a10e | 2011-01-13 17:45:08 +0000 | [diff] [blame] | 2592 | |
| 2593 | // SROA currently handles only Arrays and Structs. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2594 | StructType *STy = cast<StructType>(Ty); |
Bob Wilson | 694a10e | 2011-01-13 17:45:08 +0000 | [diff] [blame] | 2595 | const StructLayout *SL = TD.getStructLayout(STy); |
| 2596 | unsigned PrevFieldBitOffset = 0; |
| 2597 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 2598 | unsigned FieldBitOffset = SL->getElementOffsetInBits(i); |
| 2599 | |
| 2600 | // Check to see if there is any padding between this element and the |
| 2601 | // previous one. |
| 2602 | if (i) { |
| 2603 | unsigned PrevFieldEnd = |
| 2604 | PrevFieldBitOffset+TD.getTypeSizeInBits(STy->getElementType(i-1)); |
| 2605 | if (PrevFieldEnd < FieldBitOffset) |
| 2606 | return true; |
| 2607 | } |
| 2608 | PrevFieldBitOffset = FieldBitOffset; |
| 2609 | } |
| 2610 | // Check for tail padding. |
| 2611 | if (unsigned EltCount = STy->getNumElements()) { |
| 2612 | unsigned PrevFieldEnd = PrevFieldBitOffset + |
| 2613 | TD.getTypeSizeInBits(STy->getElementType(EltCount-1)); |
| 2614 | if (PrevFieldEnd < SL->getSizeInBits()) |
| 2615 | return true; |
| 2616 | } |
| 2617 | return false; |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 2618 | } |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 2619 | |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 2620 | /// isSafeStructAllocaToScalarRepl - Check to see if the specified allocation of |
| 2621 | /// an aggregate can be broken down into elements. Return 0 if not, 3 if safe, |
| 2622 | /// or 1 if safe after canonicalization has been performed. |
Victor Hernandez | 6c146ee | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 2623 | bool SROA::isSafeAllocaToScalarRepl(AllocaInst *AI) { |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 2624 | // Loop over the use list of the alloca. We can only transform it if all of |
| 2625 | // the users are safe to transform. |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 2626 | AllocaInfo Info(AI); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2627 | |
Chris Lattner | 6c95d24 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 2628 | isSafeForScalarRepl(AI, 0, Info); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2629 | if (Info.isUnsafe) { |
David Greene | 504c7d8 | 2010-01-05 01:27:09 +0000 | [diff] [blame] | 2630 | DEBUG(dbgs() << "Cannot transform: " << *AI << '\n'); |
Victor Hernandez | 6c146ee | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 2631 | return false; |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 2632 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2633 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 2634 | // Okay, we know all the users are promotable. If the aggregate is a memcpy |
| 2635 | // source and destination, we have to be careful. In particular, the memcpy |
| 2636 | // could be moving around elements that live in structure padding of the LLVM |
| 2637 | // types, but may actually be used. In these cases, we refuse to promote the |
| 2638 | // struct. |
| 2639 | if (Info.isMemCpySrc && Info.isMemCpyDst && |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2640 | HasPadding(AI->getAllocatedType(), *TD)) |
Victor Hernandez | 6c146ee | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 2641 | return false; |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 2642 | |
Chris Lattner | 396a056 | 2011-01-16 17:46:19 +0000 | [diff] [blame] | 2643 | // If the alloca never has an access to just *part* of it, but is accessed |
| 2644 | // via loads and stores, then we should use ConvertToScalarInfo to promote |
Chris Lattner | 7e9b427 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 2645 | // the alloca instead of promoting each piece at a time and inserting fission |
| 2646 | // and fusion code. |
| 2647 | if (!Info.hasSubelementAccess && Info.hasALoadOrStore) { |
| 2648 | // If the struct/array just has one element, use basic SRoA. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2649 | if (StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) { |
Chris Lattner | 7e9b427 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 2650 | if (ST->getNumElements() > 1) return false; |
| 2651 | } else { |
| 2652 | if (cast<ArrayType>(AI->getAllocatedType())->getNumElements() > 1) |
| 2653 | return false; |
| 2654 | } |
| 2655 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2656 | |
Victor Hernandez | 6c146ee | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 2657 | return true; |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 2658 | } |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 2659 | |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 2660 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2661 | |
| 2662 | /// PointsToConstantGlobal - Return true if V (possibly indirectly) points to |
| 2663 | /// some part of a constant global variable. This intentionally only accepts |
| 2664 | /// constant expressions because we don't can't rewrite arbitrary instructions. |
| 2665 | static bool PointsToConstantGlobal(Value *V) { |
| 2666 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 2667 | return GV->isConstant(); |
| 2668 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2669 | if (CE->getOpcode() == Instruction::BitCast || |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2670 | CE->getOpcode() == Instruction::GetElementPtr) |
| 2671 | return PointsToConstantGlobal(CE->getOperand(0)); |
| 2672 | return false; |
| 2673 | } |
| 2674 | |
| 2675 | /// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived) |
| 2676 | /// pointer to an alloca. Ignore any reads of the pointer, return false if we |
| 2677 | /// see any stores or other unknown uses. If we see pointer arithmetic, keep |
| 2678 | /// track of whether it moves the pointer (with isOffset) but otherwise traverse |
| 2679 | /// the uses. If we see a memcpy/memmove that targets an unoffseted pointer to |
Nick Lewycky | 081f800 | 2010-11-24 22:04:20 +0000 | [diff] [blame] | 2680 | /// the alloca, and if the source pointer is a pointer to a constant global, we |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2681 | /// can optimize this. |
Nick Lewycky | 9174d5c | 2011-06-27 05:40:02 +0000 | [diff] [blame] | 2682 | static bool |
| 2683 | isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy, |
| 2684 | bool isOffset, |
| 2685 | SmallVector<Instruction *, 4> &LifetimeMarkers) { |
| 2686 | // We track lifetime intrinsics as we encounter them. If we decide to go |
| 2687 | // ahead and replace the value with the global, this lets the caller quickly |
| 2688 | // eliminate the markers. |
| 2689 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2690 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) { |
Gabor Greif | 8a8a435 | 2010-04-06 19:32:30 +0000 | [diff] [blame] | 2691 | User *U = cast<Instruction>(*UI); |
| 2692 | |
Chris Lattner | 2e61849 | 2010-11-18 06:20:47 +0000 | [diff] [blame] | 2693 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 2694 | // Ignore non-volatile loads, they are always ok. |
Eli Friedman | 2bc3d52 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 2695 | if (!LI->isSimple()) return false; |
Chris Lattner | 2e61849 | 2010-11-18 06:20:47 +0000 | [diff] [blame] | 2696 | continue; |
| 2697 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2698 | |
Gabor Greif | 8a8a435 | 2010-04-06 19:32:30 +0000 | [diff] [blame] | 2699 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(U)) { |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2700 | // If uses of the bitcast are ok, we are ok. |
Nick Lewycky | 9174d5c | 2011-06-27 05:40:02 +0000 | [diff] [blame] | 2701 | if (!isOnlyCopiedFromConstantGlobal(BCI, TheCopy, isOffset, |
| 2702 | LifetimeMarkers)) |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2703 | return false; |
| 2704 | continue; |
| 2705 | } |
Gabor Greif | 8a8a435 | 2010-04-06 19:32:30 +0000 | [diff] [blame] | 2706 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) { |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2707 | // If the GEP has all zero indices, it doesn't offset the pointer. If it |
| 2708 | // doesn't, it does. |
| 2709 | if (!isOnlyCopiedFromConstantGlobal(GEP, TheCopy, |
Nick Lewycky | 9174d5c | 2011-06-27 05:40:02 +0000 | [diff] [blame] | 2710 | isOffset || !GEP->hasAllZeroIndices(), |
| 2711 | LifetimeMarkers)) |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2712 | return false; |
| 2713 | continue; |
| 2714 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2715 | |
Chris Lattner | 6248065 | 2010-11-18 06:41:51 +0000 | [diff] [blame] | 2716 | if (CallSite CS = U) { |
Nick Lewycky | 081f800 | 2010-11-24 22:04:20 +0000 | [diff] [blame] | 2717 | // If this is the function being called then we treat it like a load and |
| 2718 | // ignore it. |
| 2719 | if (CS.isCallee(UI)) |
| 2720 | continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2721 | |
Duncan Sands | 5389210 | 2011-05-06 10:30:37 +0000 | [diff] [blame] | 2722 | // If this is a readonly/readnone call site, then we know it is just a |
| 2723 | // load (but one that potentially returns the value itself), so we can |
| 2724 | // ignore it if we know that the value isn't captured. |
| 2725 | unsigned ArgNo = CS.getArgumentNo(UI); |
| 2726 | if (CS.onlyReadsMemory() && |
Nick Lewycky | 173862e | 2011-11-20 19:09:04 +0000 | [diff] [blame] | 2727 | (CS.getInstruction()->use_empty() || CS.doesNotCapture(ArgNo))) |
Duncan Sands | 5389210 | 2011-05-06 10:30:37 +0000 | [diff] [blame] | 2728 | continue; |
| 2729 | |
Chris Lattner | 6248065 | 2010-11-18 06:41:51 +0000 | [diff] [blame] | 2730 | // If this is being passed as a byval argument, the caller is making a |
| 2731 | // copy, so it is only a read of the alloca. |
Nick Lewycky | 173862e | 2011-11-20 19:09:04 +0000 | [diff] [blame] | 2732 | if (CS.isByValArgument(ArgNo)) |
Chris Lattner | 6248065 | 2010-11-18 06:41:51 +0000 | [diff] [blame] | 2733 | continue; |
| 2734 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2735 | |
Nick Lewycky | 9174d5c | 2011-06-27 05:40:02 +0000 | [diff] [blame] | 2736 | // Lifetime intrinsics can be handled by the caller. |
| 2737 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) { |
| 2738 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 2739 | II->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 2740 | assert(II->use_empty() && "Lifetime markers have no result to use!"); |
| 2741 | LifetimeMarkers.push_back(II); |
| 2742 | continue; |
| 2743 | } |
| 2744 | } |
| 2745 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2746 | // If this is isn't our memcpy/memmove, reject it as something we can't |
| 2747 | // handle. |
Chris Lattner | 31d8010 | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 2748 | MemTransferInst *MI = dyn_cast<MemTransferInst>(U); |
| 2749 | if (MI == 0) |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2750 | return false; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2751 | |
Chris Lattner | 2e61849 | 2010-11-18 06:20:47 +0000 | [diff] [blame] | 2752 | // If the transfer is using the alloca as a source of the transfer, then |
Chris Lattner | 2e29ebd | 2010-11-18 07:32:33 +0000 | [diff] [blame] | 2753 | // ignore it since it is a load (unless the transfer is volatile). |
Chris Lattner | 2e61849 | 2010-11-18 06:20:47 +0000 | [diff] [blame] | 2754 | if (UI.getOperandNo() == 1) { |
| 2755 | if (MI->isVolatile()) return false; |
| 2756 | continue; |
| 2757 | } |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2758 | |
| 2759 | // If we already have seen a copy, reject the second one. |
| 2760 | if (TheCopy) return false; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2761 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2762 | // If the pointer has been offset from the start of the alloca, we can't |
| 2763 | // safely handle this. |
| 2764 | if (isOffset) return false; |
| 2765 | |
| 2766 | // If the memintrinsic isn't using the alloca as the dest, reject it. |
Gabor Greif | a6aac4c | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 2767 | if (UI.getOperandNo() != 0) return false; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2768 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2769 | // If the source of the memcpy/move is not a constant global, reject it. |
Chris Lattner | 31d8010 | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 2770 | if (!PointsToConstantGlobal(MI->getSource())) |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2771 | return false; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2772 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2773 | // Otherwise, the transform is safe. Remember the copy instruction. |
| 2774 | TheCopy = MI; |
| 2775 | } |
| 2776 | return true; |
| 2777 | } |
| 2778 | |
| 2779 | /// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only |
| 2780 | /// modified by a copy from a constant global. If we can prove this, we can |
| 2781 | /// replace any uses of the alloca with uses of the global directly. |
Nick Lewycky | 9174d5c | 2011-06-27 05:40:02 +0000 | [diff] [blame] | 2782 | MemTransferInst * |
| 2783 | SROA::isOnlyCopiedFromConstantGlobal(AllocaInst *AI, |
| 2784 | SmallVector<Instruction*, 4> &ToDelete) { |
Chris Lattner | 31d8010 | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 2785 | MemTransferInst *TheCopy = 0; |
Nick Lewycky | 9174d5c | 2011-06-27 05:40:02 +0000 | [diff] [blame] | 2786 | if (::isOnlyCopiedFromConstantGlobal(AI, TheCopy, false, ToDelete)) |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 2787 | return TheCopy; |
| 2788 | return 0; |
| 2789 | } |