Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Analysis/Loads.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Loads.cpp - Local load analysis ------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file defines simple local analyses for load instructions.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Analysis/Loads.h"
14
#include "llvm/Analysis/AliasAnalysis.h"
15
#include "llvm/Analysis/ValueTracking.h"
16
#include "llvm/IR/DataLayout.h"
17
#include "llvm/IR/GlobalAlias.h"
18
#include "llvm/IR/GlobalVariable.h"
19
#include "llvm/IR/IntrinsicInst.h"
20
#include "llvm/IR/LLVMContext.h"
21
#include "llvm/IR/Module.h"
22
#include "llvm/IR/Operator.h"
23
#include "llvm/IR/Statepoint.h"
24
25
using namespace llvm;
26
27
static bool isAligned(const Value *Base, const APInt &Offset, unsigned Align,
28
1.57M
                      const DataLayout &DL) {
29
1.57M
  APInt BaseAlign(Offset.getBitWidth(), Base->getPointerAlignment(DL));
30
1.57M
31
1.57M
  if (!BaseAlign) {
32
240k
    Type *Ty = Base->getType()->getPointerElementType();
33
240k
    if (!Ty->isSized())
34
2
      return false;
35
240k
    BaseAlign = DL.getABITypeAlignment(Ty);
36
240k
  }
37
1.57M
38
1.57M
  APInt Alignment(Offset.getBitWidth(), Align);
39
1.57M
40
1.57M
  assert(Alignment.isPowerOf2() && "must be a power of 2!");
41
1.57M
  return BaseAlign.uge(Alignment) && 
!(Offset & (Alignment-1))1.57M
;
42
1.57M
}
43
44
1.57M
static bool isAligned(const Value *Base, unsigned Align, const DataLayout &DL) {
45
1.57M
  Type *Ty = Base->getType();
46
1.57M
  assert(Ty->isSized() && "must be sized");
47
1.57M
  APInt Offset(DL.getTypeStoreSizeInBits(Ty), 0);
48
1.57M
  return isAligned(Base, Offset, Align, DL);
49
1.57M
}
50
51
/// Test if V is always a pointer to allocated and suitably aligned memory for
52
/// a simple load or store.
53
static bool isDereferenceableAndAlignedPointer(
54
    const Value *V, unsigned Align, const APInt &Size, const DataLayout &DL,
55
    const Instruction *CtxI, const DominatorTree *DT,
56
7.09M
    SmallPtrSetImpl<const Value *> &Visited) {
57
7.09M
  // Already visited?  Bail out, we've likely hit unreachable code.
58
7.09M
  if (!Visited.insert(V).second)
59
5
    return false;
60
7.09M
61
7.09M
  // Note that it is not safe to speculate into a malloc'd region because
62
7.09M
  // malloc may return null.
63
7.09M
64
7.09M
  // bitcast instructions are no-ops as far as dereferenceability is concerned.
65
7.09M
  if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V))
66
537k
    return isDereferenceableAndAlignedPointer(BC->getOperand(0), Align, Size,
67
537k
                                              DL, CtxI, DT, Visited);
68
6.56M
69
6.56M
  bool CheckForNonNull = false;
70
6.56M
  APInt KnownDerefBytes(Size.getBitWidth(),
71
6.56M
                        V->getPointerDereferenceableBytes(DL, CheckForNonNull));
72
6.56M
  if (KnownDerefBytes.getBoolValue()) {
73
1.57M
    if (KnownDerefBytes.uge(Size))
74
1.57M
      if (!CheckForNonNull || 
isKnownNonZero(V, DL, 0, nullptr, CtxI, DT)45
)
75
1.57M
        return isAligned(V, Align, DL);
76
4.98M
  }
77
4.98M
78
4.98M
  // For GEPs, determine if the indexing lands within the allocated object.
79
4.98M
  if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
80
2.93M
    const Value *Base = GEP->getPointerOperand();
81
2.93M
82
2.93M
    APInt Offset(DL.getIndexTypeSizeInBits(GEP->getType()), 0);
83
2.93M
    if (!GEP->accumulateConstantOffset(DL, Offset) || 
Offset.isNegative()2.48M
||
84
2.93M
        
!Offset.urem(APInt(Offset.getBitWidth(), Align)).isMinValue()2.44M
)
85
489k
      return false;
86
2.44M
87
2.44M
    // If the base pointer is dereferenceable for Offset+Size bytes, then the
88
2.44M
    // GEP (== Base + Offset) is dereferenceable for Size bytes.  If the base
89
2.44M
    // pointer is aligned to Align bytes, and the Offset is divisible by Align
90
2.44M
    // then the GEP (== Base + Offset == k_0 * Align + k_1 * Align) is also
91
2.44M
    // aligned to Align bytes.
92
2.44M
93
2.44M
    // Offset and Size may have different bit widths if we have visited an
94
2.44M
    // addrspacecast, so we can't do arithmetic directly on the APInt values.
95
2.44M
    return isDereferenceableAndAlignedPointer(
96
2.44M
        Base, Align, Offset + Size.sextOrTrunc(Offset.getBitWidth()),
97
2.44M
        DL, CtxI, DT, Visited);
98
2.44M
  }
99
2.05M
100
2.05M
  // For gc.relocate, look through relocations
101
2.05M
  if (const GCRelocateInst *RelocateInst = dyn_cast<GCRelocateInst>(V))
102
7
    return isDereferenceableAndAlignedPointer(
103
7
        RelocateInst->getDerivedPtr(), Align, Size, DL, CtxI, DT, Visited);
104
2.05M
105
2.05M
  if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V))
106
202
    return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Align, Size,
107
202
                                              DL, CtxI, DT, Visited);
108
2.05M
109
2.05M
  if (const auto *Call = dyn_cast<CallBase>(V))
110
154k
    if (auto *RP = getArgumentAliasingToReturnedPointer(Call))
111
116
      return isDereferenceableAndAlignedPointer(RP, Align, Size, DL, CtxI, DT,
112
116
                                                Visited);
113
2.05M
114
2.05M
  // If we don't know, assume the worst.
115
2.05M
  return false;
116
2.05M
}
117
118
bool llvm::isDereferenceableAndAlignedPointer(const Value *V, unsigned Align,
119
                                              const APInt &Size,
120
                                              const DataLayout &DL,
121
                                              const Instruction *CtxI,
122
106k
                                              const DominatorTree *DT) {
123
106k
  SmallPtrSet<const Value *, 32> Visited;
124
106k
  return ::isDereferenceableAndAlignedPointer(V, Align, Size, DL, CtxI, DT,
125
106k
                                              Visited);
126
106k
}
127
128
bool llvm::isDereferenceableAndAlignedPointer(const Value *V, Type *Ty,
129
                                              unsigned Align,
130
                                              const DataLayout &DL,
131
                                              const Instruction *CtxI,
132
4.01M
                                              const DominatorTree *DT) {
133
4.01M
  // When dereferenceability information is provided by a dereferenceable
134
4.01M
  // attribute, we know exactly how many bytes are dereferenceable. If we can
135
4.01M
  // determine the exact offset to the attributed variable, we can use that
136
4.01M
  // information here.
137
4.01M
138
4.01M
  // Require ABI alignment for loads without alignment specification
139
4.01M
  if (Align == 0)
140
57.8k
    Align = DL.getABITypeAlignment(Ty);
141
4.01M
142
4.01M
  if (!Ty->isSized())
143
0
    return false;
144
4.01M
145
4.01M
  SmallPtrSet<const Value *, 32> Visited;
146
4.01M
  return ::isDereferenceableAndAlignedPointer(
147
4.01M
      V, Align,
148
4.01M
      APInt(DL.getIndexTypeSizeInBits(V->getType()), DL.getTypeStoreSize(Ty)),
149
4.01M
      DL, CtxI, DT, Visited);
150
4.01M
}
151
152
bool llvm::isDereferenceablePointer(const Value *V, Type *Ty,
153
                                    const DataLayout &DL,
154
                                    const Instruction *CtxI,
155
634k
                                    const DominatorTree *DT) {
156
634k
  return isDereferenceableAndAlignedPointer(V, Ty, 1, DL, CtxI, DT);
157
634k
}
158
159
/// Test if A and B will obviously have the same value.
160
///
161
/// This includes recognizing that %t0 and %t1 will have the same
162
/// value in code like this:
163
/// \code
164
///   %t0 = getelementptr \@a, 0, 3
165
///   store i32 0, i32* %t0
166
///   %t1 = getelementptr \@a, 0, 3
167
///   %t2 = load i32* %t1
168
/// \endcode
169
///
170
11.0M
static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
171
11.0M
  // Test if the values are trivially equivalent.
172
11.0M
  if (A == B)
173
22.3k
    return true;
174
11.0M
175
11.0M
  // Test if the values come from identical arithmetic instructions.
176
11.0M
  // Use isIdenticalToWhenDefined instead of isIdenticalTo because
177
11.0M
  // this function is only used when one address use dominates the
178
11.0M
  // other, which means that they'll always either have the same
179
11.0M
  // value or one of them will have an undefined value.
180
11.0M
  
if (11.0M
isa<BinaryOperator>(A)11.0M
|| isa<CastInst>(A) ||
isa<PHINode>(A)11.0M
||
181
11.0M
      
isa<GetElementPtrInst>(A)10.7M
)
182
8.01M
    if (const Instruction *BI = dyn_cast<Instruction>(B))
183
7.09M
      if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
184
879
        return true;
185
11.0M
186
11.0M
  // Otherwise they may not be equivalent.
187
11.0M
  return false;
188
11.0M
}
189
190
/// Check if executing a load of this pointer value cannot trap.
191
///
192
/// If DT and ScanFrom are specified this method performs context-sensitive
193
/// analysis and returns true if it is safe to load immediately before ScanFrom.
194
///
195
/// If it is not obviously safe to load from the specified pointer, we do
196
/// a quick local scan of the basic block containing \c ScanFrom, to determine
197
/// if the address is already accessed.
198
///
199
/// This uses the pointee type to determine how many bytes need to be safe to
200
/// load from the pointer.
201
bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align, APInt &Size,
202
                                       const DataLayout &DL,
203
                                       Instruction *ScanFrom,
204
12.8k
                                       const DominatorTree *DT) {
205
12.8k
  // Zero alignment means that the load has the ABI alignment for the target
206
12.8k
  if (Align == 0)
207
67
    Align = DL.getABITypeAlignment(V->getType()->getPointerElementType());
208
12.8k
  assert(isPowerOf2_32(Align));
209
12.8k
210
12.8k
  // If DT is not specified we can't make context-sensitive query
211
12.8k
  const Instruction* CtxI = DT ? 
ScanFrom0
: nullptr;
212
12.8k
  if (isDereferenceableAndAlignedPointer(V, Align, Size, DL, CtxI, DT))
213
3.48k
    return true;
214
9.39k
215
9.39k
  int64_t ByteOffset = 0;
216
9.39k
  Value *Base = V;
217
9.39k
  Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL);
218
9.39k
219
9.39k
  if (ByteOffset < 0) // out of bounds
220
12
    return false;
221
9.38k
222
9.38k
  Type *BaseType = nullptr;
223
9.38k
  unsigned BaseAlign = 0;
224
9.38k
  if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
225
6
    // An alloca is safe to load from as load as it is suitably aligned.
226
6
    BaseType = AI->getAllocatedType();
227
6
    BaseAlign = AI->getAlignment();
228
9.37k
  } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
229
6
    // Global variables are not necessarily safe to load from if they are
230
6
    // interposed arbitrarily. Their size may change or they may be weak and
231
6
    // require a test to determine if they were in fact provided.
232
6
    if (!GV->isInterposable()) {
233
5
      BaseType = GV->getType()->getElementType();
234
5
      BaseAlign = GV->getAlignment();
235
5
    }
236
6
  }
237
9.38k
238
9.38k
  PointerType *AddrTy = cast<PointerType>(V->getType());
239
9.38k
  uint64_t LoadSize = DL.getTypeStoreSize(AddrTy->getElementType());
240
9.38k
241
9.38k
  // If we found a base allocated type from either an alloca or global variable,
242
9.38k
  // try to see if we are definitively within the allocated region. We need to
243
9.38k
  // know the size of the base type and the loaded type to do anything in this
244
9.38k
  // case.
245
9.38k
  if (BaseType && 
BaseType->isSized()11
) {
246
11
    if (BaseAlign == 0)
247
4
      BaseAlign = DL.getPrefTypeAlignment(BaseType);
248
11
249
11
    if (Align <= BaseAlign) {
250
8
      // Check if the load is within the bounds of the underlying object.
251
8
      if (ByteOffset + LoadSize <= DL.getTypeAllocSize(BaseType) &&
252
8
          ((ByteOffset % Align) == 0))
253
8
        return true;
254
9.37k
    }
255
11
  }
256
9.37k
257
9.37k
  if (!ScanFrom)
258
647
    return false;
259
8.72k
260
8.72k
  // Otherwise, be a little bit aggressive by scanning the local block where we
261
8.72k
  // want to check to see if the pointer is already being loaded or stored
262
8.72k
  // from/to.  If so, the previous load or store would have already trapped,
263
8.72k
  // so there is no harm doing an extra load (also, CSE will later eliminate
264
8.72k
  // the load entirely).
265
8.72k
  BasicBlock::iterator BBI = ScanFrom->getIterator(),
266
8.72k
                       E = ScanFrom->getParent()->begin();
267
8.72k
268
8.72k
  // We can at least always strip pointer casts even though we can't use the
269
8.72k
  // base here.
270
8.72k
  V = V->stripPointerCasts();
271
8.72k
272
92.6k
  while (BBI != E) {
273
84.9k
    --BBI;
274
84.9k
275
84.9k
    // If we see a free or a call which may write to memory (i.e. which might do
276
84.9k
    // a free) the pointer could be marked invalid.
277
84.9k
    if (isa<CallInst>(BBI) && 
BBI->mayWriteToMemory()981
&&
278
84.9k
        
!isa<DbgInfoIntrinsic>(BBI)965
)
279
965
      return false;
280
84.0k
281
84.0k
    Value *AccessedPtr;
282
84.0k
    unsigned AccessedAlign;
283
84.0k
    if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
284
13.1k
      // Ignore volatile loads. The execution of a volatile load cannot
285
13.1k
      // be used to prove an address is backed by regular memory; it can,
286
13.1k
      // for example, point to an MMIO register.
287
13.1k
      if (LI->isVolatile())
288
0
        continue;
289
13.1k
      AccessedPtr = LI->getPointerOperand();
290
13.1k
      AccessedAlign = LI->getAlignment();
291
70.8k
    } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
292
5.69k
      // Ignore volatile stores (see comment for loads).
293
5.69k
      if (SI->isVolatile())
294
1
        continue;
295
5.69k
      AccessedPtr = SI->getPointerOperand();
296
5.69k
      AccessedAlign = SI->getAlignment();
297
5.69k
    } else
298
65.1k
      continue;
299
18.8k
300
18.8k
    Type *AccessedTy = AccessedPtr->getType()->getPointerElementType();
301
18.8k
    if (AccessedAlign == 0)
302
11
      AccessedAlign = DL.getABITypeAlignment(AccessedTy);
303
18.8k
    if (AccessedAlign < Align)
304
1.64k
      continue;
305
17.1k
306
17.1k
    // Handle trivial cases.
307
17.1k
    if (AccessedPtr == V)
308
133
      return true;
309
17.0k
310
17.0k
    if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) &&
311
17.0k
        
LoadSize <= DL.getTypeStoreSize(AccessedTy)6
)
312
6
      return true;
313
17.0k
  }
314
8.72k
  
return false7.62k
;
315
8.72k
}
316
317
bool llvm::isSafeToLoadUnconditionally(Value *V, Type *Ty, unsigned Align,
318
                                       const DataLayout &DL,
319
                                       Instruction *ScanFrom,
320
12.8k
                                       const DominatorTree *DT) {
321
12.8k
  APInt Size(DL.getIndexTypeSizeInBits(V->getType()), DL.getTypeStoreSize(Ty));
322
12.8k
  return isSafeToLoadUnconditionally(V, Align, Size, DL, ScanFrom, DT);
323
12.8k
}
324
325
  /// DefMaxInstsToScan - the default number of maximum instructions
326
/// to scan in the block, used by FindAvailableLoadedValue().
327
/// FindAvailableLoadedValue() was introduced in r60148, to improve jump
328
/// threading in part by eliminating partially redundant loads.
329
/// At that point, the value of MaxInstsToScan was already set to '6'
330
/// without documented explanation.
331
cl::opt<unsigned>
332
llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden,
333
  cl::desc("Use this to specify the default maximum number of instructions "
334
           "to scan backward from a given instruction, when searching for "
335
           "available loaded value"));
336
337
Value *llvm::FindAvailableLoadedValue(LoadInst *Load,
338
                                      BasicBlock *ScanBB,
339
                                      BasicBlock::iterator &ScanFrom,
340
                                      unsigned MaxInstsToScan,
341
                                      AliasAnalysis *AA, bool *IsLoad,
342
18.8M
                                      unsigned *NumScanedInst) {
343
18.8M
  // Don't CSE load that is volatile or anything stronger than unordered.
344
18.8M
  if (!Load->isUnordered())
345
188k
    return nullptr;
346
18.6M
347
18.6M
  return FindAvailablePtrLoadStore(
348
18.6M
      Load->getPointerOperand(), Load->getType(), Load->isAtomic(), ScanBB,
349
18.6M
      ScanFrom, MaxInstsToScan, AA, IsLoad, NumScanedInst);
350
18.6M
}
351
352
Value *llvm::FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy,
353
                                       bool AtLeastAtomic, BasicBlock *ScanBB,
354
                                       BasicBlock::iterator &ScanFrom,
355
                                       unsigned MaxInstsToScan,
356
                                       AliasAnalysis *AA, bool *IsLoadCSE,
357
19.1M
                                       unsigned *NumScanedInst) {
358
19.1M
  if (MaxInstsToScan == 0)
359
0
    MaxInstsToScan = ~0U;
360
19.1M
361
19.1M
  const DataLayout &DL = ScanBB->getModule()->getDataLayout();
362
19.1M
363
19.1M
  // Try to get the store size for the type.
364
19.1M
  auto AccessSize = LocationSize::precise(DL.getTypeStoreSize(AccessTy));
365
19.1M
366
19.1M
  Value *StrippedPtr = Ptr->stripPointerCasts();
367
19.1M
368
63.1M
  while (ScanFrom != ScanBB->begin()) {
369
51.0M
    // We must ignore debug info directives when counting (otherwise they
370
51.0M
    // would affect codegen).
371
51.0M
    Instruction *Inst = &*--ScanFrom;
372
51.0M
    if (isa<DbgInfoIntrinsic>(Inst))
373
103
      continue;
374
51.0M
375
51.0M
    // Restore ScanFrom to expected value in case next test succeeds
376
51.0M
    ScanFrom++;
377
51.0M
378
51.0M
    if (NumScanedInst)
379
1.35M
      ++(*NumScanedInst);
380
51.0M
381
51.0M
    // Don't scan huge blocks.
382
51.0M
    if (MaxInstsToScan-- == 0)
383
2.86M
      return nullptr;
384
48.2M
385
48.2M
    --ScanFrom;
386
48.2M
    // If this is a load of Ptr, the loaded value is available.
387
48.2M
    // (This is true even if the load is volatile or atomic, although
388
48.2M
    // those cases are unlikely.)
389
48.2M
    if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
390
8.01M
      if (AreEquivalentAddressValues(
391
8.01M
              LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) &&
392
8.01M
          
CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)14.1k
) {
393
13.7k
394
13.7k
        // We can value forward from an atomic to a non-atomic, but not the
395
13.7k
        // other way around.
396
13.7k
        if (LI->isAtomic() < AtLeastAtomic)
397
0
          return nullptr;
398
13.7k
399
13.7k
        if (IsLoadCSE)
400
13.7k
            *IsLoadCSE = true;
401
13.7k
        return LI;
402
13.7k
      }
403
48.1M
404
48.1M
    if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
405
3.03M
      Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
406
3.03M
      // If this is a store through Ptr, the value is available!
407
3.03M
      // (This is true even if the store is volatile or atomic, although
408
3.03M
      // those cases are unlikely.)
409
3.03M
      if (AreEquivalentAddressValues(StorePtr, StrippedPtr) &&
410
3.03M
          CastInst::isBitOrNoopPointerCastable(SI->getValueOperand()->getType(),
411
9.11k
                                               AccessTy, DL)) {
412
8.58k
413
8.58k
        // We can value forward from an atomic to a non-atomic, but not the
414
8.58k
        // other way around.
415
8.58k
        if (SI->isAtomic() < AtLeastAtomic)
416
0
          return nullptr;
417
8.58k
418
8.58k
        if (IsLoadCSE)
419
8.57k
          *IsLoadCSE = false;
420
8.58k
        return SI->getOperand(0);
421
8.58k
      }
422
3.02M
423
3.02M
      // If both StrippedPtr and StorePtr reach all the way to an alloca or
424
3.02M
      // global and they are different, ignore the store. This is a trivial form
425
3.02M
      // of alias analysis that is important for reg2mem'd code.
426
3.02M
      if ((isa<AllocaInst>(StrippedPtr) || 
isa<GlobalVariable>(StrippedPtr)2.95M
) &&
427
3.02M
          
(544k
isa<AllocaInst>(StorePtr)544k
||
isa<GlobalVariable>(StorePtr)516k
) &&
428
3.02M
          
StrippedPtr != StorePtr186k
)
429
186k
        continue;
430
2.83M
431
2.83M
      // If we have alias analysis and it says the store won't modify the loaded
432
2.83M
      // value, ignore the store.
433
2.83M
      if (AA && !isModSet(AA->getModRefInfo(SI, StrippedPtr, AccessSize)))
434
1.63M
        continue;
435
1.20M
436
1.20M
      // Otherwise the store that may or may not alias the pointer, bail out.
437
1.20M
      ++ScanFrom;
438
1.20M
      return nullptr;
439
1.20M
    }
440
45.1M
441
45.1M
    // If this is some other instruction that may clobber Ptr, bail out.
442
45.1M
    if (Inst->mayWriteToMemory()) {
443
3.44M
      // If alias analysis claims that it really won't modify the load,
444
3.44M
      // ignore it.
445
3.44M
      if (AA && !isModSet(AA->getModRefInfo(Inst, StrippedPtr, AccessSize)))
446
509k
        continue;
447
2.93M
448
2.93M
      // May modify the pointer, bail out.
449
2.93M
      ++ScanFrom;
450
2.93M
      return nullptr;
451
2.93M
    }
452
45.1M
  }
453
19.1M
454
19.1M
  // Got to the start of the block, we didn't find it, but are done for this
455
19.1M
  // block.
456
19.1M
  
return nullptr12.0M
;
457
19.1M
}