@@ -69,90 +69,6 @@ STATISTIC(NumMemSetInfer, "Number of memsets inferred");
69
69
STATISTIC (NumMoveToCpy, " Number of memmoves converted to memcpy" );
70
70
STATISTIC (NumCpyToSet, " Number of memcpys converted to memset" );
71
71
72
- static int64_t GetOffsetFromIndex (const GEPOperator *GEP, unsigned Idx,
73
- bool &VariableIdxFound,
74
- const DataLayout &DL) {
75
- // Skip over the first indices.
76
- gep_type_iterator GTI = gep_type_begin (GEP);
77
- for (unsigned i = 1 ; i != Idx; ++i, ++GTI)
78
- /* skip along*/ ;
79
-
80
- // Compute the offset implied by the rest of the indices.
81
- int64_t Offset = 0 ;
82
- for (unsigned i = Idx, e = GEP->getNumOperands (); i != e; ++i, ++GTI) {
83
- ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand (i));
84
- if (!OpC)
85
- return VariableIdxFound = true ;
86
- if (OpC->isZero ()) continue ; // No offset.
87
-
88
- // Handle struct indices, which add their field offset to the pointer.
89
- if (StructType *STy = GTI.getStructTypeOrNull ()) {
90
- Offset += DL.getStructLayout (STy)->getElementOffset (OpC->getZExtValue ());
91
- continue ;
92
- }
93
-
94
- // Otherwise, we have a sequential type like an array or vector. Multiply
95
- // the index by the ElementSize.
96
- uint64_t Size = DL.getTypeAllocSize (GTI.getIndexedType ());
97
- Offset += Size *OpC->getSExtValue ();
98
- }
99
-
100
- return Offset;
101
- }
102
-
103
- // / Return true if Ptr1 is provably equal to Ptr2 plus a constant offset, and
104
- // / return that constant offset. For example, Ptr1 might be &A[42], and Ptr2
105
- // / might be &A[40]. In this case offset would be -8.
106
- static bool IsPointerOffset (Value *Ptr1, Value *Ptr2, int64_t &Offset,
107
- const DataLayout &DL) {
108
- Ptr1 = Ptr1->stripPointerCasts ();
109
- Ptr2 = Ptr2->stripPointerCasts ();
110
-
111
- // Handle the trivial case first.
112
- if (Ptr1 == Ptr2) {
113
- Offset = 0 ;
114
- return true ;
115
- }
116
-
117
- GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);
118
- GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);
119
-
120
- bool VariableIdxFound = false ;
121
-
122
- // If one pointer is a GEP and the other isn't, then see if the GEP is a
123
- // constant offset from the base, as in "P" and "gep P, 1".
124
- if (GEP1 && !GEP2 && GEP1->getOperand (0 )->stripPointerCasts () == Ptr2) {
125
- Offset = -GetOffsetFromIndex (GEP1, 1 , VariableIdxFound, DL);
126
- return !VariableIdxFound;
127
- }
128
-
129
- if (GEP2 && !GEP1 && GEP2->getOperand (0 )->stripPointerCasts () == Ptr1) {
130
- Offset = GetOffsetFromIndex (GEP2, 1 , VariableIdxFound, DL);
131
- return !VariableIdxFound;
132
- }
133
-
134
- // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
135
- // base. After that base, they may have some number of common (and
136
- // potentially variable) indices. After that they handle some constant
137
- // offset, which determines their offset from each other. At this point, we
138
- // handle no other case.
139
- if (!GEP1 || !GEP2 || GEP1->getOperand (0 ) != GEP2->getOperand (0 ))
140
- return false ;
141
-
142
- // Skip any common indices and track the GEP types.
143
- unsigned Idx = 1 ;
144
- for (; Idx != GEP1->getNumOperands () && Idx != GEP2->getNumOperands (); ++Idx)
145
- if (GEP1->getOperand (Idx) != GEP2->getOperand (Idx))
146
- break ;
147
-
148
- int64_t Offset1 = GetOffsetFromIndex (GEP1, Idx, VariableIdxFound, DL);
149
- int64_t Offset2 = GetOffsetFromIndex (GEP2, Idx, VariableIdxFound, DL);
150
- if (VariableIdxFound) return false ;
151
-
152
- Offset = Offset2-Offset1;
153
- return true ;
154
- }
155
-
156
72
namespace {
157
73
158
74
// / Represents a range of memset'd bytes with the ByteVal value.
@@ -420,7 +336,7 @@ Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst,
420
336
421
337
// Check to see if this store is to a constant offset from the start ptr.
422
338
int64_t Offset;
423
- if (!IsPointerOffset (StartPtr, NextStore->getPointerOperand (), Offset,
339
+ if (!isPointerOffset (StartPtr, NextStore->getPointerOperand (), Offset,
424
340
DL))
425
341
break ;
426
342
@@ -434,7 +350,7 @@ Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst,
434
350
435
351
// Check to see if this store is to a constant offset from the start ptr.
436
352
int64_t Offset;
437
- if (!IsPointerOffset (StartPtr, MSI->getDest (), Offset, DL))
353
+ if (!isPointerOffset (StartPtr, MSI->getDest (), Offset, DL))
438
354
break ;
439
355
440
356
Ranges.addMemSet (Offset, MSI);
0 commit comments