Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- PreISelIntrinsicLowering.cpp - Pre-ISel intrinsic lowering pass ----===//
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 pass implements IR lowering for the llvm.load.relative and llvm.objc.*
10
// intrinsics.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
15
#include "llvm/CodeGen/Passes.h"
16
#include "llvm/IR/Function.h"
17
#include "llvm/IR/Intrinsics.h"
18
#include "llvm/IR/IRBuilder.h"
19
#include "llvm/IR/Instructions.h"
20
#include "llvm/IR/Module.h"
21
#include "llvm/IR/Type.h"
22
#include "llvm/IR/User.h"
23
#include "llvm/Pass.h"
24
#include "llvm/Support/Casting.h"
25
26
using namespace llvm;
27
28
4
static bool lowerLoadRelative(Function &F) {
29
4
  if (F.use_empty())
30
0
    return false;
31
4
32
4
  bool Changed = false;
33
4
  Type *Int32Ty = Type::getInt32Ty(F.getContext());
34
4
  Type *Int32PtrTy = Int32Ty->getPointerTo();
35
4
  Type *Int8Ty = Type::getInt8Ty(F.getContext());
36
4
37
8
  for (auto I = F.use_begin(), E = F.use_end(); I != E;) {
38
4
    auto CI = dyn_cast<CallInst>(I->getUser());
39
4
    ++I;
40
4
    if (!CI || CI->getCalledValue() != &F)
41
0
      continue;
42
4
43
4
    IRBuilder<> B(CI);
44
4
    Value *OffsetPtr =
45
4
        B.CreateGEP(Int8Ty, CI->getArgOperand(0), CI->getArgOperand(1));
46
4
    Value *OffsetPtrI32 = B.CreateBitCast(OffsetPtr, Int32PtrTy);
47
4
    Value *OffsetI32 = B.CreateAlignedLoad(Int32Ty, OffsetPtrI32, 4);
48
4
49
4
    Value *ResultPtr = B.CreateGEP(Int8Ty, CI->getArgOperand(0), OffsetI32);
50
4
51
4
    CI->replaceAllUsesWith(ResultPtr);
52
4
    CI->eraseFromParent();
53
4
    Changed = true;
54
4
  }
55
4
56
4
  return Changed;
57
4
}
58
59
static bool lowerObjCCall(Function &F, const char *NewFn,
60
52
                          bool setNonLazyBind = false) {
61
52
  if (F.use_empty())
62
0
    return false;
63
52
64
52
  // If we haven't already looked up this function, check to see if the
65
52
  // program already contains a function with this name.
66
52
  Module *M = F.getParent();
67
52
  FunctionCallee FCache = M->getOrInsertFunction(NewFn, F.getFunctionType());
68
52
69
52
  if (Function *Fn = dyn_cast<Function>(FCache.getCallee())) {
70
52
    Fn->setLinkage(F.getLinkage());
71
52
    if (setNonLazyBind && 
!Fn->isWeakForLinker()4
) {
72
4
      // If we have Native ARC, set nonlazybind attribute for these APIs for
73
4
      // performance.
74
4
      Fn->addFnAttr(Attribute::NonLazyBind);
75
4
    }
76
52
  }
77
52
78
104
  for (auto I = F.use_begin(), E = F.use_end(); I != E;) {
79
52
    auto *CI = dyn_cast<CallInst>(I->getUser());
80
52
    assert(CI->getCalledFunction() && "Cannot lower an indirect call!");
81
52
    ++I;
82
52
83
52
    IRBuilder<> Builder(CI->getParent(), CI->getIterator());
84
52
    SmallVector<Value *, 8> Args(CI->arg_begin(), CI->arg_end());
85
52
    CallInst *NewCI = Builder.CreateCall(FCache, Args);
86
52
    NewCI->setName(CI->getName());
87
52
    NewCI->setTailCallKind(CI->getTailCallKind());
88
52
    if (!CI->use_empty())
89
39
      CI->replaceAllUsesWith(NewCI);
90
52
    CI->eraseFromParent();
91
52
  }
92
52
93
52
  return true;
94
52
}
95
96
36.1k
static bool lowerIntrinsics(Module &M) {
97
36.1k
  bool Changed = false;
98
700k
  for (Function &F : M) {
99
700k
    if (F.getName().startswith("llvm.load.relative.")) {
100
4
      Changed |= lowerLoadRelative(F);
101
4
      continue;
102
4
    }
103
700k
    switch (F.getIntrinsicID()) {
104
700k
    default:
105
700k
      break;
106
700k
    case Intrinsic::objc_autorelease:
107
2
      Changed |= lowerObjCCall(F, "objc_autorelease");
108
2
      break;
109
700k
    case Intrinsic::objc_autoreleasePoolPop:
110
3
      Changed |= lowerObjCCall(F, "objc_autoreleasePoolPop");
111
3
      break;
112
700k
    case Intrinsic::objc_autoreleasePoolPush:
113
3
      Changed |= lowerObjCCall(F, "objc_autoreleasePoolPush");
114
3
      break;
115
700k
    case Intrinsic::objc_autoreleaseReturnValue:
116
2
      Changed |= lowerObjCCall(F, "objc_autoreleaseReturnValue");
117
2
      break;
118
700k
    case Intrinsic::objc_copyWeak:
119
2
      Changed |= lowerObjCCall(F, "objc_copyWeak");
120
2
      break;
121
700k
    case Intrinsic::objc_destroyWeak:
122
2
      Changed |= lowerObjCCall(F, "objc_destroyWeak");
123
2
      break;
124
700k
    case Intrinsic::objc_initWeak:
125
2
      Changed |= lowerObjCCall(F, "objc_initWeak");
126
2
      break;
127
700k
    case Intrinsic::objc_loadWeak:
128
2
      Changed |= lowerObjCCall(F, "objc_loadWeak");
129
2
      break;
130
700k
    case Intrinsic::objc_loadWeakRetained:
131
2
      Changed |= lowerObjCCall(F, "objc_loadWeakRetained");
132
2
      break;
133
700k
    case Intrinsic::objc_moveWeak:
134
2
      Changed |= lowerObjCCall(F, "objc_moveWeak");
135
2
      break;
136
700k
    case Intrinsic::objc_release:
137
2
      Changed |= lowerObjCCall(F, "objc_release", true);
138
2
      break;
139
700k
    case Intrinsic::objc_retain:
140
2
      Changed |= lowerObjCCall(F, "objc_retain", true);
141
2
      break;
142
700k
    case Intrinsic::objc_retainAutorelease:
143
2
      Changed |= lowerObjCCall(F, "objc_retainAutorelease");
144
2
      break;
145
700k
    case Intrinsic::objc_retainAutoreleaseReturnValue:
146
2
      Changed |= lowerObjCCall(F, "objc_retainAutoreleaseReturnValue");
147
2
      break;
148
700k
    case Intrinsic::objc_retainAutoreleasedReturnValue:
149
2
      Changed |= lowerObjCCall(F, "objc_retainAutoreleasedReturnValue");
150
2
      break;
151
700k
    case Intrinsic::objc_retainBlock:
152
2
      Changed |= lowerObjCCall(F, "objc_retainBlock");
153
2
      break;
154
700k
    case Intrinsic::objc_storeStrong:
155
2
      Changed |= lowerObjCCall(F, "objc_storeStrong");
156
2
      break;
157
700k
    case Intrinsic::objc_storeWeak:
158
2
      Changed |= lowerObjCCall(F, "objc_storeWeak");
159
2
      break;
160
700k
    case Intrinsic::objc_unsafeClaimAutoreleasedReturnValue:
161
2
      Changed |= lowerObjCCall(F, "objc_unsafeClaimAutoreleasedReturnValue");
162
2
      break;
163
700k
    case Intrinsic::objc_retainedObject:
164
2
      Changed |= lowerObjCCall(F, "objc_retainedObject");
165
2
      break;
166
700k
    case Intrinsic::objc_unretainedObject:
167
2
      Changed |= lowerObjCCall(F, "objc_unretainedObject");
168
2
      break;
169
700k
    case Intrinsic::objc_unretainedPointer:
170
2
      Changed |= lowerObjCCall(F, "objc_unretainedPointer");
171
2
      break;
172
700k
    case Intrinsic::objc_retain_autorelease:
173
2
      Changed |= lowerObjCCall(F, "objc_retain_autorelease");
174
2
      break;
175
700k
    case Intrinsic::objc_sync_enter:
176
2
      Changed |= lowerObjCCall(F, "objc_sync_enter");
177
2
      break;
178
700k
    case Intrinsic::objc_sync_exit:
179
2
      Changed |= lowerObjCCall(F, "objc_sync_exit");
180
2
      break;
181
700k
    }
182
700k
  }
183
36.1k
  return Changed;
184
36.1k
}
185
186
namespace {
187
188
class PreISelIntrinsicLoweringLegacyPass : public ModulePass {
189
public:
190
  static char ID;
191
192
36.3k
  PreISelIntrinsicLoweringLegacyPass() : ModulePass(ID) {}
193
194
36.1k
  bool runOnModule(Module &M) override { return lowerIntrinsics(M); }
195
};
196
197
} // end anonymous namespace
198
199
char PreISelIntrinsicLoweringLegacyPass::ID;
200
201
INITIALIZE_PASS(PreISelIntrinsicLoweringLegacyPass,
202
                "pre-isel-intrinsic-lowering", "Pre-ISel Intrinsic Lowering",
203
                false, false)
204
205
36.3k
ModulePass *llvm::createPreISelIntrinsicLoweringPass() {
206
36.3k
  return new PreISelIntrinsicLoweringLegacyPass;
207
36.3k
}
208
209
PreservedAnalyses PreISelIntrinsicLoweringPass::run(Module &M,
210
2
                                                    ModuleAnalysisManager &AM) {
211
2
  if (!lowerIntrinsics(M))
212
0
    return PreservedAnalyses::all();
213
2
  else
214
2
    return PreservedAnalyses::none();
215
2
}