Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Analysis/Utils/Local.h
Line
Count
Source
1
//===- Local.h - Functions to perform local transformations -----*- C++ -*-===//
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 family of functions perform various local transformations to the
10
// program.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_ANALYSIS_UTILS_LOCAL_H
15
#define LLVM_ANALYSIS_UTILS_LOCAL_H
16
17
#include "llvm/IR/DataLayout.h"
18
#include "llvm/IR/GetElementPtrTypeIterator.h"
19
20
namespace llvm {
21
22
/// Given a getelementptr instruction/constantexpr, emit the code necessary to
23
/// compute the offset from the base pointer (without adding in the base
24
/// pointer). Return the result as a signed integer of intptr size.
25
/// When NoAssumptions is true, no assumptions about index computation not
26
/// overflowing is made.
27
template <typename IRBuilderTy>
28
Value *EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP,
29
318
                     bool NoAssumptions = false) {
30
318
  GEPOperator *GEPOp = cast<GEPOperator>(GEP);
31
318
  Type *IntPtrTy = DL.getIntPtrType(GEP->getType());
32
318
  Value *Result = Constant::getNullValue(IntPtrTy);
33
318
34
318
  // If the GEP is inbounds, we know that none of the addressing operations will
35
318
  // overflow in an unsigned sense.
36
318
  bool isInBounds = GEPOp->isInBounds() && 
!NoAssumptions313
;
37
318
38
318
  // Build a mask for high order bits.
39
318
  unsigned IntPtrWidth = IntPtrTy->getScalarType()->getIntegerBitWidth();
40
318
  uint64_t PtrSizeMask =
41
318
      std::numeric_limits<uint64_t>::max() >> (64 - IntPtrWidth);
42
318
43
318
  gep_type_iterator GTI = gep_type_begin(GEP);
44
697
  for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
45
379
       ++i, ++GTI) {
46
379
    Value *Op = *i;
47
379
    uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
48
379
    if (Constant *OpC = dyn_cast<Constant>(Op)) {
49
88
      if (OpC->isZeroValue())
50
53
        continue;
51
35
52
35
      // Handle a struct index, which adds its field offset to the pointer.
53
35
      if (StructType *STy = GTI.getStructTypeOrNull()) {
54
5
        if (OpC->getType()->isVectorTy())
55
3
          OpC = OpC->getSplatValue();
56
5
57
5
        uint64_t OpValue = cast<ConstantInt>(OpC)->getZExtValue();
58
5
        Size = DL.getStructLayout(STy)->getElementOffset(OpValue);
59
5
60
5
        if (Size)
61
5
          Result = Builder->CreateAdd(Result, ConstantInt::get(IntPtrTy, Size),
62
5
                                      GEP->getName()+".offs");
63
5
        continue;
64
5
      }
65
30
66
30
      Constant *Scale = ConstantInt::get(IntPtrTy, Size);
67
30
      Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
68
30
      Scale = ConstantExpr::getMul(OC, Scale, isInBounds/*NUW*/);
69
30
      // Emit an add instruction.
70
30
      Result = Builder->CreateAdd(Result, Scale, GEP->getName()+".offs");
71
30
      continue;
72
30
    }
73
291
    // Convert to correct type.
74
291
    if (Op->getType() != IntPtrTy)
75
3
      Op = Builder->CreateIntCast(Op, IntPtrTy, true, Op->getName()+".c");
76
291
    if (Size != 1) {
77
98
      // We'll let instcombine(mul) convert this to a shl if possible.
78
98
      Op = Builder->CreateMul(Op, ConstantInt::get(IntPtrTy, Size),
79
98
                              GEP->getName()+".idx", isInBounds /*NUW*/);
80
98
    }
81
291
82
291
    // Emit an add instruction.
83
291
    Result = Builder->CreateAdd(Op, Result, GEP->getName()+".offs");
84
291
  }
85
318
  return Result;
86
318
}
87
88
}
89
90
#endif // LLVM_TRANSFORMS_UTILS_LOCAL_H