Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Utils/VNCoercion.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "llvm/Transforms/Utils/VNCoercion.h"
2
#include "llvm/Analysis/AliasAnalysis.h"
3
#include "llvm/Analysis/ConstantFolding.h"
4
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
5
#include "llvm/Analysis/ValueTracking.h"
6
#include "llvm/IR/IRBuilder.h"
7
#include "llvm/IR/IntrinsicInst.h"
8
#include "llvm/Support/Debug.h"
9
10
#define DEBUG_TYPE "vncoerce"
11
namespace llvm {
12
namespace VNCoercion {
13
14
/// Return true if coerceAvailableValueToLoadType will succeed.
15
bool canCoerceMustAliasedValueToLoad(Value *StoredVal, Type *LoadTy,
16
483k
                                     const DataLayout &DL) {
17
483k
  Type *StoredTy = StoredVal->getType();
18
483k
  if (StoredTy == LoadTy)
19
423k
    return true;
20
59.8k
21
59.8k
  // If the loaded or stored value is an first class array or struct, don't try
22
59.8k
  // to transform them.  We need to be able to bitcast to integer.
23
59.8k
  if (LoadTy->isStructTy() || LoadTy->isArrayTy() || StoredTy->isStructTy() ||
24
59.8k
      
StoredTy->isArrayTy()59.8k
)
25
2
    return false;
26
59.8k
27
59.8k
  uint64_t StoreSize = DL.getTypeSizeInBits(StoredTy);
28
59.8k
29
59.8k
  // The store size must be byte-aligned to support future type casts.
30
59.8k
  if (llvm::alignTo(StoreSize, 8) != StoreSize)
31
2
    return false;
32
59.8k
33
59.8k
  // The store has to be at least as big as the load.
34
59.8k
  if (StoreSize < DL.getTypeSizeInBits(LoadTy))
35
3.47k
    return false;
36
56.3k
37
56.3k
  // Don't coerce non-integral pointers to integers or vice versa.
38
56.3k
  if (DL.isNonIntegralPointerType(StoredVal->getType()->getScalarType()) !=
39
56.3k
      DL.isNonIntegralPointerType(LoadTy->getScalarType())) {
40
6
    // As a special case, allow coercion of memset used to initialize
41
6
    // an array w/null.  Despite non-integral pointers not generally having a
42
6
    // specific bit pattern, we do assume null is zero.
43
6
    if (auto *CI = dyn_cast<Constant>(StoredVal))
44
4
      return CI->isNullValue();
45
2
    return false;
46
2
  }
47
56.3k
  
48
56.3k
  return true;
49
56.3k
}
50
51
template <class T, class HelperClass>
52
static T *coerceAvailableValueToLoadTypeHelper(T *StoredVal, Type *LoadedTy,
53
                                               HelperClass &Helper,
54
23.3k
                                               const DataLayout &DL) {
55
23.3k
  assert(canCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, DL) &&
56
23.3k
         "precondition violation - materialization can't fail");
57
23.3k
  if (auto *C = dyn_cast<Constant>(StoredVal))
58
3.52k
    if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
59
500
      StoredVal = FoldedStoredVal;
60
23.3k
61
23.3k
  // If this is already the right type, just return it.
62
23.3k
  Type *StoredValTy = StoredVal->getType();
63
23.3k
64
23.3k
  uint64_t StoredValSize = DL.getTypeSizeInBits(StoredValTy);
65
23.3k
  uint64_t LoadedValSize = DL.getTypeSizeInBits(LoadedTy);
66
23.3k
67
23.3k
  // If the store and reload are the same size, we can always reuse it.
68
23.3k
  if (StoredValSize == LoadedValSize) {
69
23.3k
    // Pointer to Pointer -> use bitcast.
70
23.3k
    if (StoredValTy->isPtrOrPtrVectorTy() && 
LoadedTy->isPtrOrPtrVectorTy()9.89k
) {
71
9.89k
      StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
72
13.4k
    } else {
73
13.4k
      // Convert source pointers to integers, which can be bitcast.
74
13.4k
      if (StoredValTy->isPtrOrPtrVectorTy()) {
75
0
        StoredValTy = DL.getIntPtrType(StoredValTy);
76
0
        StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
77
0
      }
78
13.4k
79
13.4k
      Type *TypeToCastTo = LoadedTy;
80
13.4k
      if (TypeToCastTo->isPtrOrPtrVectorTy())
81
5.58k
        TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
82
13.4k
83
13.4k
      if (StoredValTy != TypeToCastTo)
84
815
        StoredVal = Helper.CreateBitCast(StoredVal, TypeToCastTo);
85
13.4k
86
13.4k
      // Cast to pointer if the load needs a pointer type.
87
13.4k
      if (LoadedTy->isPtrOrPtrVectorTy())
88
5.58k
        StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
89
13.4k
    }
90
23.3k
91
23.3k
    if (auto *C = dyn_cast<ConstantExpr>(StoredVal))
92
489
      if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
93
489
        StoredVal = FoldedStoredVal;
94
23.3k
95
23.3k
    return StoredVal;
96
23.3k
  }
97
4
  // If the loaded value is smaller than the available value, then we can
98
4
  // extract out a piece from it.  If the available value is too small, then we
99
4
  // can't do anything.
100
4
  assert(StoredValSize >= LoadedValSize &&
101
4
         "canCoerceMustAliasedValueToLoad fail");
102
4
103
4
  // Convert source pointers to integers, which can be manipulated.
104
4
  if (StoredValTy->isPtrOrPtrVectorTy()) {
105
0
    StoredValTy = DL.getIntPtrType(StoredValTy);
106
0
    StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
107
0
  }
108
4
109
4
  // Convert vectors and fp to integer, which can be manipulated.
110
4
  if (!StoredValTy->isIntegerTy()) {
111
0
    StoredValTy = IntegerType::get(StoredValTy->getContext(), StoredValSize);
112
0
    StoredVal = Helper.CreateBitCast(StoredVal, StoredValTy);
113
0
  }
114
4
115
4
  // If this is a big-endian system, we need to shift the value down to the low
116
4
  // bits so that a truncate will work.
117
4
  if (DL.isBigEndian()) {
118
1
    uint64_t ShiftAmt = DL.getTypeStoreSizeInBits(StoredValTy) -
119
1
                        DL.getTypeStoreSizeInBits(LoadedTy);
120
1
    StoredVal = Helper.CreateLShr(
121
1
        StoredVal, ConstantInt::get(StoredVal->getType(), ShiftAmt));
122
1
  }
123
4
124
4
  // Truncate the integer to the right size now.
125
4
  Type *NewIntTy = IntegerType::get(StoredValTy->getContext(), LoadedValSize);
126
4
  StoredVal = Helper.CreateTruncOrBitCast(StoredVal, NewIntTy);
127
4
128
4
  if (LoadedTy != NewIntTy) {
129
0
    // If the result is a pointer, inttoptr.
130
0
    if (LoadedTy->isPtrOrPtrVectorTy())
131
0
      StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
132
0
    else
133
0
      // Otherwise, bitcast.
134
0
      StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
135
0
  }
136
4
137
4
  if (auto *C = dyn_cast<Constant>(StoredVal))
138
1
    if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
139
0
      StoredVal = FoldedStoredVal;
140
4
141
4
  return StoredVal;
142
4
}
VNCoercion.cpp:llvm::Value* llvm::VNCoercion::coerceAvailableValueToLoadTypeHelper<llvm::Value, llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter> >(llvm::Value*, llvm::Type*, llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter>&, llvm::DataLayout const&)
Line
Count
Source
54
23.3k
                                               const DataLayout &DL) {
55
23.3k
  assert(canCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, DL) &&
56
23.3k
         "precondition violation - materialization can't fail");
57
23.3k
  if (auto *C = dyn_cast<Constant>(StoredVal))
58
3.51k
    if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
59
499
      StoredVal = FoldedStoredVal;
60
23.3k
61
23.3k
  // If this is already the right type, just return it.
62
23.3k
  Type *StoredValTy = StoredVal->getType();
63
23.3k
64
23.3k
  uint64_t StoredValSize = DL.getTypeSizeInBits(StoredValTy);
65
23.3k
  uint64_t LoadedValSize = DL.getTypeSizeInBits(LoadedTy);
66
23.3k
67
23.3k
  // If the store and reload are the same size, we can always reuse it.
68
23.3k
  if (StoredValSize == LoadedValSize) {
69
23.3k
    // Pointer to Pointer -> use bitcast.
70
23.3k
    if (StoredValTy->isPtrOrPtrVectorTy() && 
LoadedTy->isPtrOrPtrVectorTy()9.89k
) {
71
9.89k
      StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
72
13.4k
    } else {
73
13.4k
      // Convert source pointers to integers, which can be bitcast.
74
13.4k
      if (StoredValTy->isPtrOrPtrVectorTy()) {
75
0
        StoredValTy = DL.getIntPtrType(StoredValTy);
76
0
        StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
77
0
      }
78
13.4k
79
13.4k
      Type *TypeToCastTo = LoadedTy;
80
13.4k
      if (TypeToCastTo->isPtrOrPtrVectorTy())
81
5.58k
        TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
82
13.4k
83
13.4k
      if (StoredValTy != TypeToCastTo)
84
815
        StoredVal = Helper.CreateBitCast(StoredVal, TypeToCastTo);
85
13.4k
86
13.4k
      // Cast to pointer if the load needs a pointer type.
87
13.4k
      if (LoadedTy->isPtrOrPtrVectorTy())
88
5.58k
        StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
89
13.4k
    }
90
23.3k
91
23.3k
    if (auto *C = dyn_cast<ConstantExpr>(StoredVal))
92
489
      if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
93
489
        StoredVal = FoldedStoredVal;
94
23.3k
95
23.3k
    return StoredVal;
96
23.3k
  }
97
4
  // If the loaded value is smaller than the available value, then we can
98
4
  // extract out a piece from it.  If the available value is too small, then we
99
4
  // can't do anything.
100
4
  assert(StoredValSize >= LoadedValSize &&
101
4
         "canCoerceMustAliasedValueToLoad fail");
102
4
103
4
  // Convert source pointers to integers, which can be manipulated.
104
4
  if (StoredValTy->isPtrOrPtrVectorTy()) {
105
0
    StoredValTy = DL.getIntPtrType(StoredValTy);
106
0
    StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
107
0
  }
108
4
109
4
  // Convert vectors and fp to integer, which can be manipulated.
110
4
  if (!StoredValTy->isIntegerTy()) {
111
0
    StoredValTy = IntegerType::get(StoredValTy->getContext(), StoredValSize);
112
0
    StoredVal = Helper.CreateBitCast(StoredVal, StoredValTy);
113
0
  }
114
4
115
4
  // If this is a big-endian system, we need to shift the value down to the low
116
4
  // bits so that a truncate will work.
117
4
  if (DL.isBigEndian()) {
118
1
    uint64_t ShiftAmt = DL.getTypeStoreSizeInBits(StoredValTy) -
119
1
                        DL.getTypeStoreSizeInBits(LoadedTy);
120
1
    StoredVal = Helper.CreateLShr(
121
1
        StoredVal, ConstantInt::get(StoredVal->getType(), ShiftAmt));
122
1
  }
123
4
124
4
  // Truncate the integer to the right size now.
125
4
  Type *NewIntTy = IntegerType::get(StoredValTy->getContext(), LoadedValSize);
126
4
  StoredVal = Helper.CreateTruncOrBitCast(StoredVal, NewIntTy);
127
4
128
4
  if (LoadedTy != NewIntTy) {
129
0
    // If the result is a pointer, inttoptr.
130
0
    if (LoadedTy->isPtrOrPtrVectorTy())
131
0
      StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
132
0
    else
133
0
      // Otherwise, bitcast.
134
0
      StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
135
0
  }
136
4
137
4
  if (auto *C = dyn_cast<Constant>(StoredVal))
138
1
    if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
139
0
      StoredVal = FoldedStoredVal;
140
4
141
4
  return StoredVal;
142
4
}
VNCoercion.cpp:llvm::Constant* llvm::VNCoercion::coerceAvailableValueToLoadTypeHelper<llvm::Constant, llvm::ConstantFolder>(llvm::Constant*, llvm::Type*, llvm::ConstantFolder&, llvm::DataLayout const&)
Line
Count
Source
54
5
                                               const DataLayout &DL) {
55
5
  assert(canCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, DL) &&
56
5
         "precondition violation - materialization can't fail");
57
5
  if (auto *C = dyn_cast<Constant>(StoredVal))
58
5
    if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
59
1
      StoredVal = FoldedStoredVal;
60
5
61
5
  // If this is already the right type, just return it.
62
5
  Type *StoredValTy = StoredVal->getType();
63
5
64
5
  uint64_t StoredValSize = DL.getTypeSizeInBits(StoredValTy);
65
5
  uint64_t LoadedValSize = DL.getTypeSizeInBits(LoadedTy);
66
5
67
5
  // If the store and reload are the same size, we can always reuse it.
68
5
  if (StoredValSize == LoadedValSize) {
69
5
    // Pointer to Pointer -> use bitcast.
70
5
    if (StoredValTy->isPtrOrPtrVectorTy() && 
LoadedTy->isPtrOrPtrVectorTy()0
) {
71
0
      StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
72
5
    } else {
73
5
      // Convert source pointers to integers, which can be bitcast.
74
5
      if (StoredValTy->isPtrOrPtrVectorTy()) {
75
0
        StoredValTy = DL.getIntPtrType(StoredValTy);
76
0
        StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
77
0
      }
78
5
79
5
      Type *TypeToCastTo = LoadedTy;
80
5
      if (TypeToCastTo->isPtrOrPtrVectorTy())
81
0
        TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
82
5
83
5
      if (StoredValTy != TypeToCastTo)
84
0
        StoredVal = Helper.CreateBitCast(StoredVal, TypeToCastTo);
85
5
86
5
      // Cast to pointer if the load needs a pointer type.
87
5
      if (LoadedTy->isPtrOrPtrVectorTy())
88
0
        StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
89
5
    }
90
5
91
5
    if (auto *C = dyn_cast<ConstantExpr>(StoredVal))
92
0
      if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
93
0
        StoredVal = FoldedStoredVal;
94
5
95
5
    return StoredVal;
96
5
  }
97
0
  // If the loaded value is smaller than the available value, then we can
98
0
  // extract out a piece from it.  If the available value is too small, then we
99
0
  // can't do anything.
100
0
  assert(StoredValSize >= LoadedValSize &&
101
0
         "canCoerceMustAliasedValueToLoad fail");
102
0
103
0
  // Convert source pointers to integers, which can be manipulated.
104
0
  if (StoredValTy->isPtrOrPtrVectorTy()) {
105
0
    StoredValTy = DL.getIntPtrType(StoredValTy);
106
0
    StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
107
0
  }
108
0
109
0
  // Convert vectors and fp to integer, which can be manipulated.
110
0
  if (!StoredValTy->isIntegerTy()) {
111
0
    StoredValTy = IntegerType::get(StoredValTy->getContext(), StoredValSize);
112
0
    StoredVal = Helper.CreateBitCast(StoredVal, StoredValTy);
113
0
  }
114
0
115
0
  // If this is a big-endian system, we need to shift the value down to the low
116
0
  // bits so that a truncate will work.
117
0
  if (DL.isBigEndian()) {
118
0
    uint64_t ShiftAmt = DL.getTypeStoreSizeInBits(StoredValTy) -
119
0
                        DL.getTypeStoreSizeInBits(LoadedTy);
120
0
    StoredVal = Helper.CreateLShr(
121
0
        StoredVal, ConstantInt::get(StoredVal->getType(), ShiftAmt));
122
0
  }
123
0
124
0
  // Truncate the integer to the right size now.
125
0
  Type *NewIntTy = IntegerType::get(StoredValTy->getContext(), LoadedValSize);
126
0
  StoredVal = Helper.CreateTruncOrBitCast(StoredVal, NewIntTy);
127
0
128
0
  if (LoadedTy != NewIntTy) {
129
0
    // If the result is a pointer, inttoptr.
130
0
    if (LoadedTy->isPtrOrPtrVectorTy())
131
0
      StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
132
0
    else
133
0
      // Otherwise, bitcast.
134
0
      StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
135
0
  }
136
0
137
0
  if (auto *C = dyn_cast<Constant>(StoredVal))
138
0
    if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
139
0
      StoredVal = FoldedStoredVal;
140
0
141
0
  return StoredVal;
142
0
}
143
144
/// If we saw a store of a value to memory, and
145
/// then a load from a must-aliased pointer of a different type, try to coerce
146
/// the stored value.  LoadedTy is the type of the load we want to replace.
147
/// IRB is IRBuilder used to insert new instructions.
148
///
149
/// If we can't do it, return null.
150
Value *coerceAvailableValueToLoadType(Value *StoredVal, Type *LoadedTy,
151
0
                                      IRBuilder<> &IRB, const DataLayout &DL) {
152
0
  return coerceAvailableValueToLoadTypeHelper(StoredVal, LoadedTy, IRB, DL);
153
0
}
154
155
/// This function is called when we have a memdep query of a load that ends up
156
/// being a clobbering memory write (store, memset, memcpy, memmove).  This
157
/// means that the write *may* provide bits used by the load but we can't be
158
/// sure because the pointers don't must-alias.
159
///
160
/// Check this case to see if there is anything more we can do before we give
161
/// up.  This returns -1 if we have to give up, or a byte number in the stored
162
/// value of the piece that feeds the load.
163
static int analyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr,
164
                                          Value *WritePtr,
165
                                          uint64_t WriteSizeInBits,
166
715k
                                          const DataLayout &DL) {
167
715k
  // If the loaded or stored value is a first class array or struct, don't try
168
715k
  // to transform them.  We need to be able to bitcast to integer.
169
715k
  if (LoadTy->isStructTy() || LoadTy->isArrayTy())
170
0
    return -1;
171
715k
172
715k
  int64_t StoreOffset = 0, LoadOffset = 0;
173
715k
  Value *StoreBase =
174
715k
      GetPointerBaseWithConstantOffset(WritePtr, StoreOffset, DL);
175
715k
  Value *LoadBase = GetPointerBaseWithConstantOffset(LoadPtr, LoadOffset, DL);
176
715k
  if (StoreBase != LoadBase)
177
706k
    return -1;
178
9.24k
179
9.24k
  // If the load and store are to the exact same address, they should have been
180
9.24k
  // a must alias.  AA must have gotten confused.
181
9.24k
  // FIXME: Study to see if/when this happens.  One case is forwarding a memset
182
9.24k
  // to a load from the base of the memset.
183
9.24k
184
9.24k
  // If the load and store don't overlap at all, the store doesn't provide
185
9.24k
  // anything to the load.  In this case, they really don't alias at all, AA
186
9.24k
  // must have gotten confused.
187
9.24k
  uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy);
188
9.24k
189
9.24k
  if ((WriteSizeInBits & 7) | (LoadSize & 7))
190
2
    return -1;
191
9.23k
  uint64_t StoreSize = WriteSizeInBits / 8; // Convert to bytes.
192
9.23k
  LoadSize /= 8;
193
9.23k
194
9.23k
  bool isAAFailure = false;
195
9.23k
  if (StoreOffset < LoadOffset)
196
4.53k
    isAAFailure = StoreOffset + int64_t(StoreSize) <= LoadOffset;
197
4.70k
  else
198
4.70k
    isAAFailure = LoadOffset + int64_t(LoadSize) <= StoreOffset;
199
9.23k
200
9.23k
  if (isAAFailure)
201
841
    return -1;
202
8.39k
203
8.39k
  // If the Load isn't completely contained within the stored bits, we don't
204
8.39k
  // have all the bits to feed it.  We could do something crazy in the future
205
8.39k
  // (issue a smaller load then merge the bits in) but this seems unlikely to be
206
8.39k
  // valuable.
207
8.39k
  if (StoreOffset > LoadOffset ||
208
8.39k
      
StoreOffset + StoreSize < LoadOffset + LoadSize7.45k
)
209
1.00k
    return -1;
210
7.39k
211
7.39k
  // Okay, we can do this transformation.  Return the number of bytes into the
212
7.39k
  // store that the load is.
213
7.39k
  return LoadOffset - StoreOffset;
214
7.39k
}
215
216
/// This function is called when we have a
217
/// memdep query of a load that ends up being a clobbering store.
218
int analyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr,
219
691k
                                   StoreInst *DepSI, const DataLayout &DL) {
220
691k
  auto *StoredVal = DepSI->getValueOperand();
221
691k
  
222
691k
  // Cannot handle reading from store of first-class aggregate yet.
223
691k
  if (StoredVal->getType()->isStructTy() ||
224
691k
      StoredVal->getType()->isArrayTy())
225
0
    return -1;
226
691k
227
691k
  // Don't coerce non-integral pointers to integers or vice versa.
228
691k
  if (DL.isNonIntegralPointerType(StoredVal->getType()->getScalarType()) !=
229
691k
      DL.isNonIntegralPointerType(LoadTy->getScalarType())) {
230
4
    // Allow casts of zero values to null as a special case
231
4
    auto *CI = dyn_cast<Constant>(StoredVal);
232
4
    if (!CI || 
!CI->isNullValue()2
)
233
3
      return -1;
234
691k
  }
235
691k
236
691k
  Value *StorePtr = DepSI->getPointerOperand();
237
691k
  uint64_t StoreSize =
238
691k
      DL.getTypeSizeInBits(DepSI->getValueOperand()->getType());
239
691k
  return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, StorePtr, StoreSize,
240
691k
                                        DL);
241
691k
}
242
243
/// This function is called when we have a
244
/// memdep query of a load that ends up being clobbered by another load.  See if
245
/// the other load can feed into the second load.
246
int analyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr, LoadInst *DepLI,
247
3.22k
                                  const DataLayout &DL) {
248
3.22k
  // Cannot handle reading from store of first-class aggregate yet.
249
3.22k
  if (DepLI->getType()->isStructTy() || DepLI->getType()->isArrayTy())
250
0
    return -1;
251
3.22k
252
3.22k
  // Don't coerce non-integral pointers to integers or vice versa.
253
3.22k
  if (DL.isNonIntegralPointerType(DepLI->getType()->getScalarType()) !=
254
3.22k
      DL.isNonIntegralPointerType(LoadTy->getScalarType()))
255
0
    return -1;
256
3.22k
257
3.22k
  Value *DepPtr = DepLI->getPointerOperand();
258
3.22k
  uint64_t DepSize = DL.getTypeSizeInBits(DepLI->getType());
259
3.22k
  int R = analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, DepSize, DL);
260
3.22k
  if (R != -1)
261
3
    return R;
262
3.21k
263
3.21k
  // If we have a load/load clobber an DepLI can be widened to cover this load,
264
3.21k
  // then we should widen it!
265
3.21k
  int64_t LoadOffs = 0;
266
3.21k
  const Value *LoadBase =
267
3.21k
      GetPointerBaseWithConstantOffset(LoadPtr, LoadOffs, DL);
268
3.21k
  unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
269
3.21k
270
3.21k
  unsigned Size = MemoryDependenceResults::getLoadLoadClobberFullWidthSize(
271
3.21k
      LoadBase, LoadOffs, LoadSize, DepLI);
272
3.21k
  if (Size == 0)
273
3.21k
    return -1;
274
0
275
0
  // Check non-obvious conditions enforced by MDA which we rely on for being
276
0
  // able to materialize this potentially available value
277
0
  assert(DepLI->isSimple() && "Cannot widen volatile/atomic load!");
278
0
  assert(DepLI->getType()->isIntegerTy() && "Can't widen non-integer load");
279
0
280
0
  return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, Size * 8, DL);
281
0
}
282
283
int analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr,
284
53.3k
                                     MemIntrinsic *MI, const DataLayout &DL) {
285
53.3k
  // If the mem operation is a non-constant size, we can't handle it.
286
53.3k
  ConstantInt *SizeCst = dyn_cast<ConstantInt>(MI->getLength());
287
53.3k
  if (!SizeCst)
288
9.30k
    return -1;
289
44.0k
  uint64_t MemSizeInBits = SizeCst->getZExtValue() * 8;
290
44.0k
291
44.0k
  // If this is memset, we just need to see if the offset is valid in the size
292
44.0k
  // of the memset..
293
44.0k
  if (MI->getIntrinsicID() == Intrinsic::memset) {
294
20.7k
    if (DL.isNonIntegralPointerType(LoadTy->getScalarType())) {
295
3
      auto *CI = dyn_cast<ConstantInt>(cast<MemSetInst>(MI)->getValue());
296
3
      if (!CI || !CI->isZero())
297
2
        return -1;
298
20.7k
    }
299
20.7k
    return analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(),
300
20.7k
                                          MemSizeInBits, DL);
301
20.7k
  }
302
23.2k
303
23.2k
  // If we have a memcpy/memmove, the only case we can handle is if this is a
304
23.2k
  // copy from constant memory.  In that case, we can read directly from the
305
23.2k
  // constant memory.
306
23.2k
  MemTransferInst *MTI = cast<MemTransferInst>(MI);
307
23.2k
308
23.2k
  Constant *Src = dyn_cast<Constant>(MTI->getSource());
309
23.2k
  if (!Src)
310
22.1k
    return -1;
311
1.12k
312
1.12k
  GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Src, DL));
313
1.12k
  if (!GV || !GV->isConstant() || 
!GV->hasDefinitiveInitializer()418
)
314
706
    return -1;
315
418
316
418
  // See if the access is within the bounds of the transfer.
317
418
  int Offset = analyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(),
318
418
                                              MemSizeInBits, DL);
319
418
  if (Offset == -1)
320
149
    return Offset;
321
269
322
269
  // Don't coerce non-integral pointers to integers or vice versa, and the
323
269
  // memtransfer is implicitly a raw byte code
324
269
  if (DL.isNonIntegralPointerType(LoadTy->getScalarType()))
325
6
    // TODO: Can allow nullptrs from constant zeros
326
6
    return -1;
327
263
328
263
  unsigned AS = Src->getType()->getPointerAddressSpace();
329
263
  // Otherwise, see if we can constant fold a load from the constant with the
330
263
  // offset applied as appropriate.
331
263
  Src =
332
263
      ConstantExpr::getBitCast(Src, Type::getInt8PtrTy(Src->getContext(), AS));
333
263
  Constant *OffsetCst =
334
263
      ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
335
263
  Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src,
336
263
                                       OffsetCst);
337
263
  Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
338
263
  if (ConstantFoldLoadFromConstPtr(Src, LoadTy, DL))
339
130
    return Offset;
340
133
  return -1;
341
133
}
342
343
template <class T, class HelperClass>
344
static T *getStoreValueForLoadHelper(T *SrcVal, unsigned Offset, Type *LoadTy,
345
                                     HelperClass &Helper,
346
20.9k
                                     const DataLayout &DL) {
347
20.9k
  LLVMContext &Ctx = SrcVal->getType()->getContext();
348
20.9k
349
20.9k
  // If two pointers are in the same address space, they have the same size,
350
20.9k
  // so we don't need to do any truncation, etc. This avoids introducing
351
20.9k
  // ptrtoint instructions for pointers that may be non-integral.
352
20.9k
  if (SrcVal->getType()->isPointerTy() && 
LoadTy->isPointerTy()14.8k
&&
353
20.9k
      cast<PointerType>(SrcVal->getType())->getAddressSpace() ==
354
9.89k
          cast<PointerType>(LoadTy)->getAddressSpace()) {
355
9.89k
    return SrcVal;
356
9.89k
  }
357
11.0k
358
11.0k
  uint64_t StoreSize = (DL.getTypeSizeInBits(SrcVal->getType()) + 7) / 8;
359
11.0k
  uint64_t LoadSize = (DL.getTypeSizeInBits(LoadTy) + 7) / 8;
360
11.0k
  // Compute which bits of the stored value are being used by the load.  Convert
361
11.0k
  // to an integer type to start with.
362
11.0k
  if (SrcVal->getType()->isPtrOrPtrVectorTy())
363
4.93k
    SrcVal = Helper.CreatePtrToInt(SrcVal, DL.getIntPtrType(SrcVal->getType()));
364
11.0k
  if (!SrcVal->getType()->isIntegerTy())
365
612
    SrcVal = Helper.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize * 8));
366
11.0k
367
11.0k
  // Shift the bits to the least significant depending on endianness.
368
11.0k
  unsigned ShiftAmt;
369
11.0k
  if (DL.isLittleEndian())
370
10.9k
    ShiftAmt = Offset * 8;
371
18
  else
372
18
    ShiftAmt = (StoreSize - LoadSize - Offset) * 8;
373
11.0k
  if (ShiftAmt)
374
67
    SrcVal = Helper.CreateLShr(SrcVal,
375
67
                               ConstantInt::get(SrcVal->getType(), ShiftAmt));
376
11.0k
377
11.0k
  if (LoadSize != StoreSize)
378
398
    SrcVal = Helper.CreateTruncOrBitCast(SrcVal,
379
398
                                         IntegerType::get(Ctx, LoadSize * 8));
380
11.0k
  return SrcVal;
381
11.0k
}
VNCoercion.cpp:llvm::Value* llvm::VNCoercion::getStoreValueForLoadHelper<llvm::Value, llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter> >(llvm::Value*, unsigned int, llvm::Type*, llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter>&, llvm::DataLayout const&)
Line
Count
Source
346
20.9k
                                     const DataLayout &DL) {
347
20.9k
  LLVMContext &Ctx = SrcVal->getType()->getContext();
348
20.9k
349
20.9k
  // If two pointers are in the same address space, they have the same size,
350
20.9k
  // so we don't need to do any truncation, etc. This avoids introducing
351
20.9k
  // ptrtoint instructions for pointers that may be non-integral.
352
20.9k
  if (SrcVal->getType()->isPointerTy() && 
LoadTy->isPointerTy()14.8k
&&
353
20.9k
      cast<PointerType>(SrcVal->getType())->getAddressSpace() ==
354
9.89k
          cast<PointerType>(LoadTy)->getAddressSpace()) {
355
9.89k
    return SrcVal;
356
9.89k
  }
357
11.0k
358
11.0k
  uint64_t StoreSize = (DL.getTypeSizeInBits(SrcVal->getType()) + 7) / 8;
359
11.0k
  uint64_t LoadSize = (DL.getTypeSizeInBits(LoadTy) + 7) / 8;
360
11.0k
  // Compute which bits of the stored value are being used by the load.  Convert
361
11.0k
  // to an integer type to start with.
362
11.0k
  if (SrcVal->getType()->isPtrOrPtrVectorTy())
363
4.93k
    SrcVal = Helper.CreatePtrToInt(SrcVal, DL.getIntPtrType(SrcVal->getType()));
364
11.0k
  if (!SrcVal->getType()->isIntegerTy())
365
611
    SrcVal = Helper.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize * 8));
366
11.0k
367
11.0k
  // Shift the bits to the least significant depending on endianness.
368
11.0k
  unsigned ShiftAmt;
369
11.0k
  if (DL.isLittleEndian())
370
10.9k
    ShiftAmt = Offset * 8;
371
18
  else
372
18
    ShiftAmt = (StoreSize - LoadSize - Offset) * 8;
373
11.0k
  if (ShiftAmt)
374
67
    SrcVal = Helper.CreateLShr(SrcVal,
375
67
                               ConstantInt::get(SrcVal->getType(), ShiftAmt));
376
11.0k
377
11.0k
  if (LoadSize != StoreSize)
378
397
    SrcVal = Helper.CreateTruncOrBitCast(SrcVal,
379
397
                                         IntegerType::get(Ctx, LoadSize * 8));
380
11.0k
  return SrcVal;
381
11.0k
}
VNCoercion.cpp:llvm::Constant* llvm::VNCoercion::getStoreValueForLoadHelper<llvm::Constant, llvm::ConstantFolder>(llvm::Constant*, unsigned int, llvm::Type*, llvm::ConstantFolder&, llvm::DataLayout const&)
Line
Count
Source
346
1
                                     const DataLayout &DL) {
347
1
  LLVMContext &Ctx = SrcVal->getType()->getContext();
348
1
349
1
  // If two pointers are in the same address space, they have the same size,
350
1
  // so we don't need to do any truncation, etc. This avoids introducing
351
1
  // ptrtoint instructions for pointers that may be non-integral.
352
1
  if (SrcVal->getType()->isPointerTy() && 
LoadTy->isPointerTy()0
&&
353
1
      cast<PointerType>(SrcVal->getType())->getAddressSpace() ==
354
0
          cast<PointerType>(LoadTy)->getAddressSpace()) {
355
0
    return SrcVal;
356
0
  }
357
1
358
1
  uint64_t StoreSize = (DL.getTypeSizeInBits(SrcVal->getType()) + 7) / 8;
359
1
  uint64_t LoadSize = (DL.getTypeSizeInBits(LoadTy) + 7) / 8;
360
1
  // Compute which bits of the stored value are being used by the load.  Convert
361
1
  // to an integer type to start with.
362
1
  if (SrcVal->getType()->isPtrOrPtrVectorTy())
363
0
    SrcVal = Helper.CreatePtrToInt(SrcVal, DL.getIntPtrType(SrcVal->getType()));
364
1
  if (!SrcVal->getType()->isIntegerTy())
365
1
    SrcVal = Helper.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize * 8));
366
1
367
1
  // Shift the bits to the least significant depending on endianness.
368
1
  unsigned ShiftAmt;
369
1
  if (DL.isLittleEndian())
370
1
    ShiftAmt = Offset * 8;
371
0
  else
372
0
    ShiftAmt = (StoreSize - LoadSize - Offset) * 8;
373
1
  if (ShiftAmt)
374
0
    SrcVal = Helper.CreateLShr(SrcVal,
375
0
                               ConstantInt::get(SrcVal->getType(), ShiftAmt));
376
1
377
1
  if (LoadSize != StoreSize)
378
1
    SrcVal = Helper.CreateTruncOrBitCast(SrcVal,
379
1
                                         IntegerType::get(Ctx, LoadSize * 8));
380
1
  return SrcVal;
381
1
}
382
383
/// This function is called when we have a memdep query of a load that ends up
384
/// being a clobbering store.  This means that the store provides bits used by
385
/// the load but the pointers don't must-alias.  Check this case to see if
386
/// there is anything more we can do before we give up.
387
Value *getStoreValueForLoad(Value *SrcVal, unsigned Offset, Type *LoadTy,
388
20.9k
                            Instruction *InsertPt, const DataLayout &DL) {
389
20.9k
390
20.9k
  IRBuilder<> Builder(InsertPt);
391
20.9k
  SrcVal = getStoreValueForLoadHelper(SrcVal, Offset, LoadTy, Builder, DL);
392
20.9k
  return coerceAvailableValueToLoadTypeHelper(SrcVal, LoadTy, Builder, DL);
393
20.9k
}
394
395
Constant *getConstantStoreValueForLoad(Constant *SrcVal, unsigned Offset,
396
1
                                       Type *LoadTy, const DataLayout &DL) {
397
1
  ConstantFolder F;
398
1
  SrcVal = getStoreValueForLoadHelper(SrcVal, Offset, LoadTy, F, DL);
399
1
  return coerceAvailableValueToLoadTypeHelper(SrcVal, LoadTy, F, DL);
400
1
}
401
402
/// This function is called when we have a memdep query of a load that ends up
403
/// being a clobbering load.  This means that the load *may* provide bits used
404
/// by the load but we can't be sure because the pointers don't must-alias.
405
/// Check this case to see if there is anything more we can do before we give
406
/// up.
407
Value *getLoadValueForLoad(LoadInst *SrcVal, unsigned Offset, Type *LoadTy,
408
8.37k
                           Instruction *InsertPt, const DataLayout &DL) {
409
8.37k
  // If Offset+LoadTy exceeds the size of SrcVal, then we must be wanting to
410
8.37k
  // widen SrcVal out to a larger load.
411
8.37k
  unsigned SrcValStoreSize = DL.getTypeStoreSize(SrcVal->getType());
412
8.37k
  unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
413
8.37k
  if (Offset + LoadSize > SrcValStoreSize) {
414
0
    assert(SrcVal->isSimple() && "Cannot widen volatile/atomic load!");
415
0
    assert(SrcVal->getType()->isIntegerTy() && "Can't widen non-integer load");
416
0
    // If we have a load/load clobber an DepLI can be widened to cover this
417
0
    // load, then we should widen it to the next power of 2 size big enough!
418
0
    unsigned NewLoadSize = Offset + LoadSize;
419
0
    if (!isPowerOf2_32(NewLoadSize))
420
0
      NewLoadSize = NextPowerOf2(NewLoadSize);
421
0
422
0
    Value *PtrVal = SrcVal->getPointerOperand();
423
0
    // Insert the new load after the old load.  This ensures that subsequent
424
0
    // memdep queries will find the new load.  We can't easily remove the old
425
0
    // load completely because it is already in the value numbering table.
426
0
    IRBuilder<> Builder(SrcVal->getParent(), ++BasicBlock::iterator(SrcVal));
427
0
    Type *DestTy = IntegerType::get(LoadTy->getContext(), NewLoadSize * 8);
428
0
    Type *DestPTy =
429
0
        PointerType::get(DestTy, PtrVal->getType()->getPointerAddressSpace());
430
0
    Builder.SetCurrentDebugLocation(SrcVal->getDebugLoc());
431
0
    PtrVal = Builder.CreateBitCast(PtrVal, DestPTy);
432
0
    LoadInst *NewLoad = Builder.CreateLoad(DestTy, PtrVal);
433
0
    NewLoad->takeName(SrcVal);
434
0
    NewLoad->setAlignment(SrcVal->getAlignment());
435
0
436
0
    LLVM_DEBUG(dbgs() << "GVN WIDENED LOAD: " << *SrcVal << "\n");
437
0
    LLVM_DEBUG(dbgs() << "TO: " << *NewLoad << "\n");
438
0
439
0
    // Replace uses of the original load with the wider load.  On a big endian
440
0
    // system, we need to shift down to get the relevant bits.
441
0
    Value *RV = NewLoad;
442
0
    if (DL.isBigEndian())
443
0
      RV = Builder.CreateLShr(RV, (NewLoadSize - SrcValStoreSize) * 8);
444
0
    RV = Builder.CreateTrunc(RV, SrcVal->getType());
445
0
    SrcVal->replaceAllUsesWith(RV);
446
0
447
0
    SrcVal = NewLoad;
448
0
  }
449
8.37k
450
8.37k
  return getStoreValueForLoad(SrcVal, Offset, LoadTy, InsertPt, DL);
451
8.37k
}
452
453
Constant *getConstantLoadValueForLoad(Constant *SrcVal, unsigned Offset,
454
0
                                      Type *LoadTy, const DataLayout &DL) {
455
0
  unsigned SrcValStoreSize = DL.getTypeStoreSize(SrcVal->getType());
456
0
  unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
457
0
  if (Offset + LoadSize > SrcValStoreSize)
458
0
    return nullptr;
459
0
  return getConstantStoreValueForLoad(SrcVal, Offset, LoadTy, DL);
460
0
}
461
462
template <class T, class HelperClass>
463
T *getMemInstValueForLoadHelper(MemIntrinsic *SrcInst, unsigned Offset,
464
                                Type *LoadTy, HelperClass &Helper,
465
2.50k
                                const DataLayout &DL) {
466
2.50k
  LLVMContext &Ctx = LoadTy->getContext();
467
2.50k
  uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy) / 8;
468
2.50k
469
2.50k
  // We know that this method is only called when the mem transfer fully
470
2.50k
  // provides the bits for the load.
471
2.50k
  if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
472
2.44k
    // memset(P, 'x', 1234) -> splat('x'), even if x is a variable, and
473
2.44k
    // independently of what the offset is.
474
2.44k
    T *Val = cast<T>(MSI->getValue());
475
2.44k
    if (LoadSize != 1)
476
1.99k
      Val =
477
1.99k
          Helper.CreateZExtOrBitCast(Val, IntegerType::get(Ctx, LoadSize * 8));
478
2.44k
    T *OneElt = Val;
479
2.44k
480
2.44k
    // Splat the value out to the right number of bits.
481
7.86k
    for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize;) {
482
5.42k
      // If we can double the number of bytes set, do it.
483
5.42k
      if (NumBytesSet * 2 <= LoadSize) {
484
5.42k
        T *ShVal = Helper.CreateShl(
485
5.42k
            Val, ConstantInt::get(Val->getType(), NumBytesSet * 8));
486
5.42k
        Val = Helper.CreateOr(Val, ShVal);
487
5.42k
        NumBytesSet <<= 1;
488
5.42k
        continue;
489
5.42k
      }
490
0
491
0
      // Otherwise insert one byte at a time.
492
0
      T *ShVal = Helper.CreateShl(Val, ConstantInt::get(Val->getType(), 1 * 8));
493
0
      Val = Helper.CreateOr(OneElt, ShVal);
494
0
      ++NumBytesSet;
495
0
    }
496
2.44k
497
2.44k
    return coerceAvailableValueToLoadTypeHelper(Val, LoadTy, Helper, DL);
498
2.44k
  }
499
61
500
61
  // Otherwise, this is a memcpy/memmove from a constant global.
501
61
  MemTransferInst *MTI = cast<MemTransferInst>(SrcInst);
502
61
  Constant *Src = cast<Constant>(MTI->getSource());
503
61
  unsigned AS = Src->getType()->getPointerAddressSpace();
504
61
505
61
  // Otherwise, see if we can constant fold a load from the constant with the
506
61
  // offset applied as appropriate.
507
61
  Src =
508
61
      ConstantExpr::getBitCast(Src, Type::getInt8PtrTy(Src->getContext(), AS));
509
61
  Constant *OffsetCst =
510
61
      ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
511
61
  Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src,
512
61
                                       OffsetCst);
513
61
  Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
514
61
  return ConstantFoldLoadFromConstPtr(Src, LoadTy, DL);
515
61
}
llvm::Value* llvm::VNCoercion::getMemInstValueForLoadHelper<llvm::Value, llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter> >(llvm::MemIntrinsic*, unsigned int, llvm::Type*, llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter>&, llvm::DataLayout const&)
Line
Count
Source
465
2.49k
                                const DataLayout &DL) {
466
2.49k
  LLVMContext &Ctx = LoadTy->getContext();
467
2.49k
  uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy) / 8;
468
2.49k
469
2.49k
  // We know that this method is only called when the mem transfer fully
470
2.49k
  // provides the bits for the load.
471
2.49k
  if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
472
2.44k
    // memset(P, 'x', 1234) -> splat('x'), even if x is a variable, and
473
2.44k
    // independently of what the offset is.
474
2.44k
    T *Val = cast<T>(MSI->getValue());
475
2.44k
    if (LoadSize != 1)
476
1.99k
      Val =
477
1.99k
          Helper.CreateZExtOrBitCast(Val, IntegerType::get(Ctx, LoadSize * 8));
478
2.44k
    T *OneElt = Val;
479
2.44k
480
2.44k
    // Splat the value out to the right number of bits.
481
7.85k
    for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize;) {
482
5.41k
      // If we can double the number of bytes set, do it.
483
5.41k
      if (NumBytesSet * 2 <= LoadSize) {
484
5.41k
        T *ShVal = Helper.CreateShl(
485
5.41k
            Val, ConstantInt::get(Val->getType(), NumBytesSet * 8));
486
5.41k
        Val = Helper.CreateOr(Val, ShVal);
487
5.41k
        NumBytesSet <<= 1;
488
5.41k
        continue;
489
5.41k
      }
490
0
491
0
      // Otherwise insert one byte at a time.
492
0
      T *ShVal = Helper.CreateShl(Val, ConstantInt::get(Val->getType(), 1 * 8));
493
0
      Val = Helper.CreateOr(OneElt, ShVal);
494
0
      ++NumBytesSet;
495
0
    }
496
2.44k
497
2.44k
    return coerceAvailableValueToLoadTypeHelper(Val, LoadTy, Helper, DL);
498
2.44k
  }
499
55
500
55
  // Otherwise, this is a memcpy/memmove from a constant global.
501
55
  MemTransferInst *MTI = cast<MemTransferInst>(SrcInst);
502
55
  Constant *Src = cast<Constant>(MTI->getSource());
503
55
  unsigned AS = Src->getType()->getPointerAddressSpace();
504
55
505
55
  // Otherwise, see if we can constant fold a load from the constant with the
506
55
  // offset applied as appropriate.
507
55
  Src =
508
55
      ConstantExpr::getBitCast(Src, Type::getInt8PtrTy(Src->getContext(), AS));
509
55
  Constant *OffsetCst =
510
55
      ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
511
55
  Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src,
512
55
                                       OffsetCst);
513
55
  Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
514
55
  return ConstantFoldLoadFromConstPtr(Src, LoadTy, DL);
515
55
}
llvm::Constant* llvm::VNCoercion::getMemInstValueForLoadHelper<llvm::Constant, llvm::ConstantFolder>(llvm::MemIntrinsic*, unsigned int, llvm::Type*, llvm::ConstantFolder&, llvm::DataLayout const&)
Line
Count
Source
465
10
                                const DataLayout &DL) {
466
10
  LLVMContext &Ctx = LoadTy->getContext();
467
10
  uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy) / 8;
468
10
469
10
  // We know that this method is only called when the mem transfer fully
470
10
  // provides the bits for the load.
471
10
  if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
472
4
    // memset(P, 'x', 1234) -> splat('x'), even if x is a variable, and
473
4
    // independently of what the offset is.
474
4
    T *Val = cast<T>(MSI->getValue());
475
4
    if (LoadSize != 1)
476
4
      Val =
477
4
          Helper.CreateZExtOrBitCast(Val, IntegerType::get(Ctx, LoadSize * 8));
478
4
    T *OneElt = Val;
479
4
480
4
    // Splat the value out to the right number of bits.
481
10
    for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize;) {
482
6
      // If we can double the number of bytes set, do it.
483
6
      if (NumBytesSet * 2 <= LoadSize) {
484
6
        T *ShVal = Helper.CreateShl(
485
6
            Val, ConstantInt::get(Val->getType(), NumBytesSet * 8));
486
6
        Val = Helper.CreateOr(Val, ShVal);
487
6
        NumBytesSet <<= 1;
488
6
        continue;
489
6
      }
490
0
491
0
      // Otherwise insert one byte at a time.
492
0
      T *ShVal = Helper.CreateShl(Val, ConstantInt::get(Val->getType(), 1 * 8));
493
0
      Val = Helper.CreateOr(OneElt, ShVal);
494
0
      ++NumBytesSet;
495
0
    }
496
4
497
4
    return coerceAvailableValueToLoadTypeHelper(Val, LoadTy, Helper, DL);
498
4
  }
499
6
500
6
  // Otherwise, this is a memcpy/memmove from a constant global.
501
6
  MemTransferInst *MTI = cast<MemTransferInst>(SrcInst);
502
6
  Constant *Src = cast<Constant>(MTI->getSource());
503
6
  unsigned AS = Src->getType()->getPointerAddressSpace();
504
6
505
6
  // Otherwise, see if we can constant fold a load from the constant with the
506
6
  // offset applied as appropriate.
507
6
  Src =
508
6
      ConstantExpr::getBitCast(Src, Type::getInt8PtrTy(Src->getContext(), AS));
509
6
  Constant *OffsetCst =
510
6
      ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
511
6
  Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src,
512
6
                                       OffsetCst);
513
6
  Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
514
6
  return ConstantFoldLoadFromConstPtr(Src, LoadTy, DL);
515
6
}
516
517
/// This function is called when we have a
518
/// memdep query of a load that ends up being a clobbering mem intrinsic.
519
Value *getMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset,
520
                              Type *LoadTy, Instruction *InsertPt,
521
2.49k
                              const DataLayout &DL) {
522
2.49k
  IRBuilder<> Builder(InsertPt);
523
2.49k
  return getMemInstValueForLoadHelper<Value, IRBuilder<>>(SrcInst, Offset,
524
2.49k
                                                          LoadTy, Builder, DL);
525
2.49k
}
526
527
Constant *getConstantMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset,
528
10
                                         Type *LoadTy, const DataLayout &DL) {
529
10
  // The only case analyzeLoadFromClobberingMemInst cannot be converted to a
530
10
  // constant is when it's a memset of a non-constant.
531
10
  if (auto *MSI = dyn_cast<MemSetInst>(SrcInst))
532
4
    if (!isa<Constant>(MSI->getValue()))
533
0
      return nullptr;
534
10
  ConstantFolder F;
535
10
  return getMemInstValueForLoadHelper<Constant, ConstantFolder>(SrcInst, Offset,
536
10
                                                                LoadTy, F, DL);
537
10
}
538
} // namespace VNCoercion
539
} // namespace llvm