14
14
#include " llvm/CodeGen/PreISelIntrinsicLowering.h"
15
15
#include " llvm/Analysis/ObjCARCInstKind.h"
16
16
#include " llvm/Analysis/ObjCARCUtil.h"
17
- #include " llvm/Analysis/TargetLibraryInfo.h"
18
17
#include " llvm/Analysis/TargetTransformInfo.h"
19
18
#include " llvm/CodeGen/Passes.h"
19
+ #include " llvm/CodeGen/TargetLowering.h"
20
+ #include " llvm/CodeGen/TargetPassConfig.h"
20
21
#include " llvm/IR/Function.h"
21
22
#include " llvm/IR/IRBuilder.h"
22
23
#include " llvm/IR/Instructions.h"
26
27
#include " llvm/InitializePasses.h"
27
28
#include " llvm/Pass.h"
28
29
#include " llvm/Support/Casting.h"
30
+ #include " llvm/Target/TargetMachine.h"
29
31
#include " llvm/Transforms/Utils/LowerMemIntrinsics.h"
30
32
31
33
using namespace llvm ;
@@ -41,19 +43,19 @@ static cl::opt<int64_t> MemIntrinsicExpandSizeThresholdOpt(
41
43
namespace {
42
44
43
45
struct PreISelIntrinsicLowering {
46
+ const TargetMachine &TM;
44
47
const function_ref<TargetTransformInfo &(Function &)> LookupTTI;
45
- const function_ref<TargetLibraryInfo &(Function &)> LookupLibInfo;
46
48
47
49
// / If this is true, assume it's preferably to leave memory intrinsic calls
48
50
// / for replacement with a library call later. Otherwise this depends on
49
- // / TargetLibraryInfo availability of the corresponding function.
51
+ // / TargetLoweringInfo availability of the corresponding function.
50
52
const bool UseMemIntrinsicLibFunc;
51
53
52
54
explicit PreISelIntrinsicLowering (
55
+ const TargetMachine &TM_,
53
56
function_ref<TargetTransformInfo &(Function &)> LookupTTI_,
54
- function_ref<TargetLibraryInfo &(Function &)> LookupLibInfo_,
55
57
bool UseMemIntrinsicLibFunc_ = true)
56
- : LookupTTI(LookupTTI_ ), LookupLibInfo(LookupLibInfo_ ),
58
+ : TM(TM_ ), LookupTTI(LookupTTI_ ),
57
59
UseMemIntrinsicLibFunc(UseMemIntrinsicLibFunc_) {}
58
60
59
61
static bool shouldExpandMemIntrinsicWithSize (Value *Size ,
@@ -195,9 +197,15 @@ bool PreISelIntrinsicLowering::shouldExpandMemIntrinsicWithSize(
195
197
return SizeVal > Threshold || Threshold == 0 ;
196
198
}
197
199
200
+ static bool canEmitLibcall (const TargetLowering &TLI, RTLIB::Libcall LC) {
201
+ // TODO: Should this consider the address space of the memcpy?
202
+ return TLI.getLibcallName (LC) != nullptr ;
203
+ }
204
+
198
205
// TODO: Handle atomic memcpy and memcpy.inline
199
206
// TODO: Pass ScalarEvolution
200
207
bool PreISelIntrinsicLowering::expandMemIntrinsicUses (Function &F) const {
208
+ const TargetLowering *TLI = TM.getSubtargetImpl (F)->getTargetLowering ();
201
209
Intrinsic::ID ID = F.getIntrinsicID ();
202
210
bool Changed = false ;
203
211
@@ -210,10 +218,10 @@ bool PreISelIntrinsicLowering::expandMemIntrinsicUses(Function &F) const {
210
218
Function *ParentFunc = Memcpy->getFunction ();
211
219
const TargetTransformInfo &TTI = LookupTTI (*ParentFunc);
212
220
if (shouldExpandMemIntrinsicWithSize (Memcpy->getLength (), TTI)) {
213
- if (UseMemIntrinsicLibFunc &&
214
- LookupLibInfo (*ParentFunc).has (LibFunc_memcpy))
221
+ if (UseMemIntrinsicLibFunc && canEmitLibcall (*TLI, RTLIB::MEMCPY))
215
222
break ;
216
223
224
+ // TODO: For optsize, emit the loop into a separate function
217
225
expandMemCpyAsLoop (Memcpy, TTI);
218
226
Changed = true ;
219
227
Memcpy->eraseFromParent ();
@@ -226,8 +234,7 @@ bool PreISelIntrinsicLowering::expandMemIntrinsicUses(Function &F) const {
226
234
Function *ParentFunc = Memmove->getFunction ();
227
235
const TargetTransformInfo &TTI = LookupTTI (*ParentFunc);
228
236
if (shouldExpandMemIntrinsicWithSize (Memmove->getLength (), TTI)) {
229
- if (UseMemIntrinsicLibFunc &&
230
- LookupLibInfo (*ParentFunc).has (LibFunc_memmove))
237
+ if (UseMemIntrinsicLibFunc && canEmitLibcall (*TLI, RTLIB::MEMMOVE))
231
238
break ;
232
239
233
240
if (expandMemMoveAsLoop (Memmove, TTI)) {
@@ -243,8 +250,7 @@ bool PreISelIntrinsicLowering::expandMemIntrinsicUses(Function &F) const {
243
250
Function *ParentFunc = Memset->getFunction ();
244
251
const TargetTransformInfo &TTI = LookupTTI (*ParentFunc);
245
252
if (shouldExpandMemIntrinsicWithSize (Memset->getLength (), TTI)) {
246
- if (UseMemIntrinsicLibFunc &&
247
- LookupLibInfo (*Memset->getFunction ()).has (LibFunc_memset))
253
+ if (UseMemIntrinsicLibFunc && canEmitLibcall (*TLI, RTLIB::MEMSET))
248
254
break ;
249
255
250
256
expandMemSetAsLoop (Memset);
@@ -365,20 +371,17 @@ class PreISelIntrinsicLoweringLegacyPass : public ModulePass {
365
371
PreISelIntrinsicLoweringLegacyPass () : ModulePass(ID) {}
366
372
367
373
void getAnalysisUsage (AnalysisUsage &AU) const override {
368
- AU.addRequired <TargetLibraryInfoWrapperPass>();
369
374
AU.addRequired <TargetTransformInfoWrapperPass>();
375
+ AU.addRequired <TargetPassConfig>();
370
376
}
371
377
372
378
bool runOnModule (Module &M) override {
373
379
auto LookupTTI = [this ](Function &F) -> TargetTransformInfo & {
374
380
return this ->getAnalysis <TargetTransformInfoWrapperPass>().getTTI (F);
375
381
};
376
382
377
- auto LookupTLI = [this ](Function &F) -> TargetLibraryInfo & {
378
- return this ->getAnalysis <TargetLibraryInfoWrapperPass>().getTLI (F);
379
- };
380
-
381
- PreISelIntrinsicLowering Lowering (LookupTTI, LookupTLI);
383
+ const auto &TM = getAnalysis<TargetPassConfig>().getTM <TargetMachine>();
384
+ PreISelIntrinsicLowering Lowering (TM, LookupTTI);
382
385
return Lowering.lowerIntrinsics (M);
383
386
}
384
387
};
@@ -387,27 +390,28 @@ class PreISelIntrinsicLoweringLegacyPass : public ModulePass {
387
390
388
391
char PreISelIntrinsicLoweringLegacyPass::ID;
389
392
390
- INITIALIZE_PASS (PreISelIntrinsicLoweringLegacyPass,
391
- " pre-isel-intrinsic-lowering" , " Pre-ISel Intrinsic Lowering" ,
392
- false , false )
393
+ INITIALIZE_PASS_BEGIN (PreISelIntrinsicLoweringLegacyPass,
394
+ " pre-isel-intrinsic-lowering" ,
395
+ " Pre-ISel Intrinsic Lowering" , false , false )
396
+ INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
397
+ INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
398
+ INITIALIZE_PASS_END(PreISelIntrinsicLoweringLegacyPass,
399
+ " pre-isel-intrinsic-lowering" ,
400
+ " Pre-ISel Intrinsic Lowering" , false , false )
393
401
394
402
ModulePass *llvm::createPreISelIntrinsicLoweringPass() {
395
- return new PreISelIntrinsicLoweringLegacyPass;
403
+ return new PreISelIntrinsicLoweringLegacyPass () ;
396
404
}
397
405
398
406
PreservedAnalyses PreISelIntrinsicLoweringPass::run (Module &M,
399
407
ModuleAnalysisManager &AM) {
400
408
auto &FAM = AM.getResult <FunctionAnalysisManagerModuleProxy>(M).getManager ();
401
409
402
- auto LookupTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
403
- return FAM.getResult <TargetLibraryAnalysis>(F);
404
- };
405
-
406
410
auto LookupTTI = [&FAM](Function &F) -> TargetTransformInfo & {
407
411
return FAM.getResult <TargetIRAnalysis>(F);
408
412
};
409
413
410
- PreISelIntrinsicLowering Lowering (LookupTTI, LookupTLI );
414
+ PreISelIntrinsicLowering Lowering (TM, LookupTTI );
411
415
if (!Lowering.lowerIntrinsics (M))
412
416
return PreservedAnalyses::all ();
413
417
else
0 commit comments