Index: include/llvm/Transforms/InstrProfiling.h =================================================================== --- include/llvm/Transforms/InstrProfiling.h +++ include/llvm/Transforms/InstrProfiling.h @@ -21,6 +21,8 @@ namespace llvm { +class TargetLibraryInfo; + /// Instrumenation based profiling lowering pass. This pass lowers /// the profile instrumented code generated by FE or the IR based /// instrumentation pass. @@ -30,7 +32,7 @@ InstrProfiling(const InstrProfOptions &Options) : Options(Options) {} PreservedAnalyses run(Module &M, AnalysisManager &AM); - bool run(Module &M); + bool run(Module &M, const TargetLibraryInfo &TLI); private: InstrProfOptions Options; @@ -67,7 +69,8 @@ void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins); /// Replace instrprof_value_profile with a call to runtime library. - void lowerValueProfileInst(InstrProfValueProfileInst *Ins); + void lowerValueProfileInst(InstrProfValueProfileInst *Ins, + const TargetLibraryInfo &TLI); /// Replace instrprof_increment with an increment of the appropriate value. void lowerIncrement(InstrProfIncrementInst *Inc); Index: lib/Transforms/Instrumentation/InstrProfiling.cpp =================================================================== --- lib/Transforms/Instrumentation/InstrProfiling.cpp +++ lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -15,6 +15,7 @@ #include "llvm/Transforms/InstrProfiling.h" #include "llvm/ADT/Triple.h" +#include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/Module.h" @@ -57,26 +58,34 @@ return "Frontend instrumentation-based coverage lowering"; } - bool runOnModule(Module &M) override { return InstrProf.run(M); } + bool runOnModule(Module &M) override { + return InstrProf.run(M, getAnalysis().getTLI()); + } void getAnalysisUsage(AnalysisUsage &AU) const override { AU.setPreservesCFG(); + AU.addRequired(); } }; } // anonymous namespace PreservedAnalyses InstrProfiling::run(Module &M, AnalysisManager &AM) { - if (!run(M)) + auto &TLI = AM.getResult(M); + if (!run(M, TLI)) return PreservedAnalyses::all(); return PreservedAnalyses::none(); } char InstrProfilingLegacyPass::ID = 0; -INITIALIZE_PASS(InstrProfilingLegacyPass, "instrprof", - "Frontend instrumentation-based coverage lowering.", false, - false) +INITIALIZE_PASS_BEGIN( + InstrProfilingLegacyPass, "instrprof", + "Frontend instrumentation-based coverage lowering.", false, false) +INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) +INITIALIZE_PASS_END( + InstrProfilingLegacyPass, "instrprof", + "Frontend instrumentation-based coverage lowering.", false, false) ModulePass * llvm::createInstrProfilingLegacyPass(const InstrProfOptions &Options) { @@ -107,7 +116,7 @@ return getInstrProfCoverageSectionName(isMachO()); } -bool InstrProfiling::run(Module &M) { +bool InstrProfiling::run(Module &M, const TargetLibraryInfo &TLI) { bool MadeChange = false; this->M = &M; @@ -142,7 +151,7 @@ lowerIncrement(Inc); MadeChange = true; } else if (auto *Ind = dyn_cast(Instr)) { - lowerValueProfileInst(Ind); + lowerValueProfileInst(Ind, TLI); MadeChange = true; } } @@ -165,7 +174,8 @@ return true; } -static Constant *getOrInsertValueProfilingCall(Module &M) { +static Constant *getOrInsertValueProfilingCall(Module &M, + const TargetLibraryInfo &TLI) { LLVMContext &Ctx = M.getContext(); auto *ReturnTy = Type::getVoidTy(M.getContext()); Type *ParamTypes[] = { @@ -174,8 +184,12 @@ }; auto *ValueProfilingCallTy = FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false); - return M.getOrInsertFunction(getInstrProfValueProfFuncName(), - ValueProfilingCallTy); + Constant *Res = M.getOrInsertFunction(getInstrProfValueProfFuncName(), + ValueProfilingCallTy); + Function *FunRes = dyn_cast(Res); + if (TLI.shouldExtI32Param() && FunRes) + FunRes->addAttribute(3, Attribute::ZExt); + return Res; } void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) { @@ -192,7 +206,8 @@ It->second.NumValueSites[ValueKind] = Index + 1; } -void InstrProfiling::lowerValueProfileInst(InstrProfValueProfileInst *Ind) { +void InstrProfiling::lowerValueProfileInst(InstrProfValueProfileInst *Ind, + const TargetLibraryInfo &TLI) { GlobalVariable *Name = Ind->getName(); auto It = ProfileDataMap.find(Name); @@ -209,8 +224,11 @@ Value *Args[3] = {Ind->getTargetValue(), Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()), Builder.getInt32(Index)}; - Ind->replaceAllUsesWith( - Builder.CreateCall(getOrInsertValueProfilingCall(*M), Args)); + CallInst *Call = Builder.CreateCall(getOrInsertValueProfilingCall(*M, TLI), + Args); + if (TLI.shouldExtI32Param()) + Call->addAttribute(3, Attribute::ZExt); + Ind->replaceAllUsesWith(Call); Ind->eraseFromParent(); } Index: test/Instrumentation/InstrProfiling/icall.ll =================================================================== --- test/Instrumentation/InstrProfiling/icall.ll +++ test/Instrumentation/InstrProfiling/icall.ll @@ -2,6 +2,7 @@ ; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -vp-static-alloc=true -instrprof -S | FileCheck %s --check-prefix=STATIC ; RUN: opt < %s -mtriple=x86_64-unknown-linux -instrprof -vp-static-alloc=true -S | FileCheck %s --check-prefix=STATIC +; RUN: opt < %s -mtriple=s390x-unknown-linux -instrprof -vp-static-alloc=true -S | FileCheck %s --check-prefix=STATIC-EXT ; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -vp-static-alloc=false -instrprof -S | FileCheck %s --check-prefix=DYN ; RUN: opt < %s -mtriple=x86_64-unknown-linux -instrprof -vp-static-alloc=false -S | FileCheck %s --check-prefix=DYN @@ -29,3 +30,9 @@ ; DYN-NOT: @__profvp_foo ; DYN-NOT: @__llvm_prf_vnodes + +; STATIC: call void @__llvm_profile_instrument_target(i64 %3, i8* bitcast ({ i64, i64, i64*, i8*, i8*, i32, [1 x i16] }* @__profd_foo to i8*), i32 0) +; STATIC-EXT: call void @__llvm_profile_instrument_target(i64 %3, i8* bitcast ({ i64, i64, i64*, i8*, i8*, i32, [1 x i16] }* @__profd_foo to i8*), i32 zeroext 0) + +; STATIC: declare void @__llvm_profile_instrument_target(i64, i8*, i32) +; STATIC-EXT: declare void @__llvm_profile_instrument_target(i64, i8*, i32 zeroext)