Rong Xu | 48596b6 | 2017-04-04 16:42:20 +0000 | [diff] [blame] | 1 | //===-- IndirectCallPromotion.cpp - Optimizations based on value profiling ===// |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the transformation that promotes indirect calls to |
| 11 | // conditional direct calls when the indirect-call value profile metadata is |
| 12 | // available. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ArrayRef.h" |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/ADT/Twine.h" |
Rong Xu | 48596b6 | 2017-04-04 16:42:20 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
Rong Xu | 2bf4c59 | 2017-04-06 20:56:00 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/GlobalsModRef.h" |
Teresa Johnson | 1e44b5d | 2016-07-12 21:13:44 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/IndirectCallPromotionAnalysis.h" |
| 23 | #include "llvm/Analysis/IndirectCallSiteVisitor.h" |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/OptimizationDiagnosticInfo.h" |
Dehao Chen | 34cfcb2 | 2017-08-08 20:57:33 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 26 | #include "llvm/IR/BasicBlock.h" |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 27 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DerivedTypes.h" |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DiagnosticInfo.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Function.h" |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 31 | #include "llvm/IR/IRBuilder.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 32 | #include "llvm/IR/InstrTypes.h" |
| 33 | #include "llvm/IR/Instruction.h" |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Instructions.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 35 | #include "llvm/IR/LLVMContext.h" |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 36 | #include "llvm/IR/MDBuilder.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 37 | #include "llvm/IR/PassManager.h" |
| 38 | #include "llvm/IR/Type.h" |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 39 | #include "llvm/Pass.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 40 | #include "llvm/PassRegistry.h" |
| 41 | #include "llvm/PassSupport.h" |
| 42 | #include "llvm/ProfileData/InstrProf.h" |
| 43 | #include "llvm/Support/Casting.h" |
| 44 | #include "llvm/Support/CommandLine.h" |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 46 | #include "llvm/Support/ErrorHandling.h" |
Rong Xu | 48596b6 | 2017-04-04 16:42:20 +0000 | [diff] [blame] | 47 | #include "llvm/Support/MathExtras.h" |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 48 | #include "llvm/Transforms/Instrumentation.h" |
Xinliang David Li | f3c7a35 | 2016-05-16 16:31:07 +0000 | [diff] [blame] | 49 | #include "llvm/Transforms/PGOInstrumentation.h" |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 50 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 51 | #include <cassert> |
| 52 | #include <cstdint> |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 53 | #include <vector> |
| 54 | |
| 55 | using namespace llvm; |
| 56 | |
Xinliang David Li | 0b29330 | 2016-06-02 01:52:05 +0000 | [diff] [blame] | 57 | #define DEBUG_TYPE "pgo-icall-prom" |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 58 | |
| 59 | STATISTIC(NumOfPGOICallPromotion, "Number of indirect call promotions."); |
| 60 | STATISTIC(NumOfPGOICallsites, "Number of indirect call candidate sites."); |
| 61 | |
| 62 | // Command line option to disable indirect-call promotion with the default as |
| 63 | // false. This is for debug purpose. |
| 64 | static cl::opt<bool> DisableICP("disable-icp", cl::init(false), cl::Hidden, |
| 65 | cl::desc("Disable indirect call promotion")); |
| 66 | |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 67 | // Set the cutoff value for the promotion. If the value is other than 0, we |
| 68 | // stop the transformation once the total number of promotions equals the cutoff |
| 69 | // value. |
| 70 | // For debug use only. |
| 71 | static cl::opt<unsigned> |
| 72 | ICPCutOff("icp-cutoff", cl::init(0), cl::Hidden, cl::ZeroOrMore, |
Craig Topper | a49e768 | 2017-05-05 22:31:11 +0000 | [diff] [blame] | 73 | cl::desc("Max number of promotions for this compilation")); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 74 | |
| 75 | // If ICPCSSkip is non zero, the first ICPCSSkip callsites will be skipped. |
| 76 | // For debug use only. |
| 77 | static cl::opt<unsigned> |
| 78 | ICPCSSkip("icp-csskip", cl::init(0), cl::Hidden, cl::ZeroOrMore, |
Craig Topper | a49e768 | 2017-05-05 22:31:11 +0000 | [diff] [blame] | 79 | cl::desc("Skip Callsite up to this number for this compilation")); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 80 | |
| 81 | // Set if the pass is called in LTO optimization. The difference for LTO mode |
| 82 | // is the pass won't prefix the source module name to the internal linkage |
| 83 | // symbols. |
| 84 | static cl::opt<bool> ICPLTOMode("icp-lto", cl::init(false), cl::Hidden, |
| 85 | cl::desc("Run indirect-call promotion in LTO " |
| 86 | "mode")); |
Teresa Johnson | 1e44b5d | 2016-07-12 21:13:44 +0000 | [diff] [blame] | 87 | |
Dehao Chen | cc75d24 | 2017-02-23 22:15:18 +0000 | [diff] [blame] | 88 | // Set if the pass is called in SamplePGO mode. The difference for SamplePGO |
| 89 | // mode is it will add prof metadatato the created direct call. |
| 90 | static cl::opt<bool> |
| 91 | ICPSamplePGOMode("icp-samplepgo", cl::init(false), cl::Hidden, |
| 92 | cl::desc("Run indirect-call promotion in SamplePGO mode")); |
| 93 | |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 94 | // If the option is set to true, only call instructions will be considered for |
| 95 | // transformation -- invoke instructions will be ignored. |
| 96 | static cl::opt<bool> |
| 97 | ICPCallOnly("icp-call-only", cl::init(false), cl::Hidden, |
| 98 | cl::desc("Run indirect-call promotion for call instructions " |
| 99 | "only")); |
| 100 | |
| 101 | // If the option is set to true, only invoke instructions will be considered for |
| 102 | // transformation -- call instructions will be ignored. |
| 103 | static cl::opt<bool> ICPInvokeOnly("icp-invoke-only", cl::init(false), |
| 104 | cl::Hidden, |
| 105 | cl::desc("Run indirect-call promotion for " |
| 106 | "invoke instruction only")); |
| 107 | |
| 108 | // Dump the function level IR if the transformation happened in this |
| 109 | // function. For debug use only. |
| 110 | static cl::opt<bool> |
| 111 | ICPDUMPAFTER("icp-dumpafter", cl::init(false), cl::Hidden, |
| 112 | cl::desc("Dump IR after transformation happens")); |
| 113 | |
| 114 | namespace { |
Xinliang David Li | 7261618 | 2016-05-15 01:04:24 +0000 | [diff] [blame] | 115 | class PGOIndirectCallPromotionLegacyPass : public ModulePass { |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 116 | public: |
| 117 | static char ID; |
| 118 | |
Dehao Chen | cc75d24 | 2017-02-23 22:15:18 +0000 | [diff] [blame] | 119 | PGOIndirectCallPromotionLegacyPass(bool InLTO = false, bool SamplePGO = false) |
| 120 | : ModulePass(ID), InLTO(InLTO), SamplePGO(SamplePGO) { |
Xinliang David Li | 7261618 | 2016-05-15 01:04:24 +0000 | [diff] [blame] | 121 | initializePGOIndirectCallPromotionLegacyPassPass( |
| 122 | *PassRegistry::getPassRegistry()); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Dehao Chen | 34cfcb2 | 2017-08-08 20:57:33 +0000 | [diff] [blame] | 125 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 126 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
| 127 | } |
| 128 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 129 | StringRef getPassName() const override { return "PGOIndirectCallPromotion"; } |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 130 | |
| 131 | private: |
| 132 | bool runOnModule(Module &M) override; |
| 133 | |
| 134 | // If this pass is called in LTO. We need to special handling the PGOFuncName |
| 135 | // for the static variables due to LTO's internalization. |
| 136 | bool InLTO; |
Dehao Chen | cc75d24 | 2017-02-23 22:15:18 +0000 | [diff] [blame] | 137 | |
| 138 | // If this pass is called in SamplePGO. We need to add the prof metadata to |
| 139 | // the promoted direct call. |
| 140 | bool SamplePGO; |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 141 | }; |
| 142 | } // end anonymous namespace |
| 143 | |
Xinliang David Li | 7261618 | 2016-05-15 01:04:24 +0000 | [diff] [blame] | 144 | char PGOIndirectCallPromotionLegacyPass::ID = 0; |
Dehao Chen | 45847d3 | 2017-08-14 23:25:21 +0000 | [diff] [blame^] | 145 | INITIALIZE_PASS_BEGIN(PGOIndirectCallPromotionLegacyPass, "pgo-icall-prom", |
| 146 | "Use PGO instrumentation profile to promote indirect " |
| 147 | "calls to direct calls.", |
| 148 | false, false) |
| 149 | INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) |
| 150 | INITIALIZE_PASS_END(PGOIndirectCallPromotionLegacyPass, "pgo-icall-prom", |
| 151 | "Use PGO instrumentation profile to promote indirect " |
| 152 | "calls to direct calls.", |
| 153 | false, false) |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 154 | |
Dehao Chen | cc75d24 | 2017-02-23 22:15:18 +0000 | [diff] [blame] | 155 | ModulePass *llvm::createPGOIndirectCallPromotionLegacyPass(bool InLTO, |
| 156 | bool SamplePGO) { |
| 157 | return new PGOIndirectCallPromotionLegacyPass(InLTO, SamplePGO); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Benjamin Kramer | a65b610 | 2016-05-15 15:18:11 +0000 | [diff] [blame] | 160 | namespace { |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 161 | // The class for main data structure to promote indirect calls to conditional |
| 162 | // direct calls. |
| 163 | class ICallPromotionFunc { |
| 164 | private: |
| 165 | Function &F; |
| 166 | Module *M; |
| 167 | |
| 168 | // Symtab that maps indirect call profile values to function names and |
| 169 | // defines. |
| 170 | InstrProfSymtab *Symtab; |
| 171 | |
Dehao Chen | cc75d24 | 2017-02-23 22:15:18 +0000 | [diff] [blame] | 172 | bool SamplePGO; |
| 173 | |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 174 | OptimizationRemarkEmitter &ORE; |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 175 | |
| 176 | // A struct that records the direct target and it's call count. |
| 177 | struct PromotionCandidate { |
| 178 | Function *TargetFunction; |
| 179 | uint64_t Count; |
| 180 | PromotionCandidate(Function *F, uint64_t C) : TargetFunction(F), Count(C) {} |
| 181 | }; |
| 182 | |
| 183 | // Check if the indirect-call call site should be promoted. Return the number |
Teresa Johnson | 1e44b5d | 2016-07-12 21:13:44 +0000 | [diff] [blame] | 184 | // of promotions. Inst is the candidate indirect call, ValueDataRef |
| 185 | // contains the array of value profile data for profiled targets, |
| 186 | // TotalCount is the total profiled count of call executions, and |
| 187 | // NumCandidates is the number of candidate entries in ValueDataRef. |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 188 | std::vector<PromotionCandidate> getPromotionCandidatesForCallSite( |
| 189 | Instruction *Inst, const ArrayRef<InstrProfValueData> &ValueDataRef, |
Teresa Johnson | 1e44b5d | 2016-07-12 21:13:44 +0000 | [diff] [blame] | 190 | uint64_t TotalCount, uint32_t NumCandidates); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 191 | |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 192 | // Promote a list of targets for one indirect-call callsite. Return |
| 193 | // the number of promotions. |
| 194 | uint32_t tryToPromote(Instruction *Inst, |
| 195 | const std::vector<PromotionCandidate> &Candidates, |
| 196 | uint64_t &TotalCount); |
| 197 | |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 198 | // Noncopyable |
| 199 | ICallPromotionFunc(const ICallPromotionFunc &other) = delete; |
| 200 | ICallPromotionFunc &operator=(const ICallPromotionFunc &other) = delete; |
| 201 | |
| 202 | public: |
Dehao Chen | cc75d24 | 2017-02-23 22:15:18 +0000 | [diff] [blame] | 203 | ICallPromotionFunc(Function &Func, Module *Modu, InstrProfSymtab *Symtab, |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 204 | bool SamplePGO, OptimizationRemarkEmitter &ORE) |
| 205 | : F(Func), M(Modu), Symtab(Symtab), SamplePGO(SamplePGO), ORE(ORE) {} |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 206 | |
Dehao Chen | 34cfcb2 | 2017-08-08 20:57:33 +0000 | [diff] [blame] | 207 | bool processFunction(ProfileSummaryInfo *PSI); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 208 | }; |
Benjamin Kramer | a65b610 | 2016-05-15 15:18:11 +0000 | [diff] [blame] | 209 | } // end anonymous namespace |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 210 | |
Dehao Chen | 6775f5d | 2017-01-30 22:46:37 +0000 | [diff] [blame] | 211 | bool llvm::isLegalToPromote(Instruction *Inst, Function *F, |
| 212 | const char **Reason) { |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 213 | // Check the return type. |
| 214 | Type *CallRetType = Inst->getType(); |
| 215 | if (!CallRetType->isVoidTy()) { |
Dehao Chen | 6775f5d | 2017-01-30 22:46:37 +0000 | [diff] [blame] | 216 | Type *FuncRetType = F->getReturnType(); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 217 | if (FuncRetType != CallRetType && |
Dehao Chen | 6775f5d | 2017-01-30 22:46:37 +0000 | [diff] [blame] | 218 | !CastInst::isBitCastable(FuncRetType, CallRetType)) { |
| 219 | if (Reason) |
| 220 | *Reason = "Return type mismatch"; |
| 221 | return false; |
| 222 | } |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // Check if the arguments are compatible with the parameters |
Dehao Chen | 6775f5d | 2017-01-30 22:46:37 +0000 | [diff] [blame] | 226 | FunctionType *DirectCalleeType = F->getFunctionType(); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 227 | unsigned ParamNum = DirectCalleeType->getFunctionNumParams(); |
| 228 | CallSite CS(Inst); |
| 229 | unsigned ArgNum = CS.arg_size(); |
| 230 | |
Dehao Chen | 6775f5d | 2017-01-30 22:46:37 +0000 | [diff] [blame] | 231 | if (ParamNum != ArgNum && !DirectCalleeType->isVarArg()) { |
| 232 | if (Reason) |
| 233 | *Reason = "The number of arguments mismatch"; |
| 234 | return false; |
| 235 | } |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 236 | |
| 237 | for (unsigned I = 0; I < ParamNum; ++I) { |
| 238 | Type *PTy = DirectCalleeType->getFunctionParamType(I); |
| 239 | Type *ATy = CS.getArgument(I)->getType(); |
| 240 | if (PTy == ATy) |
| 241 | continue; |
Dehao Chen | 6775f5d | 2017-01-30 22:46:37 +0000 | [diff] [blame] | 242 | if (!CastInst::castIsValid(Instruction::BitCast, CS.getArgument(I), PTy)) { |
| 243 | if (Reason) |
Benjamin Kramer | 365c9bd | 2017-01-30 23:11:29 +0000 | [diff] [blame] | 244 | *Reason = "Argument type mismatch"; |
Dehao Chen | 6775f5d | 2017-01-30 22:46:37 +0000 | [diff] [blame] | 245 | return false; |
| 246 | } |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | DEBUG(dbgs() << " #" << NumOfPGOICallPromotion << " Promote the icall to " |
Dehao Chen | 6775f5d | 2017-01-30 22:46:37 +0000 | [diff] [blame] | 250 | << F->getName() << "\n"); |
| 251 | return true; |
| 252 | } |
| 253 | |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 254 | // Indirect-call promotion heuristic. The direct targets are sorted based on |
| 255 | // the count. Stop at the first target that is not promoted. |
| 256 | std::vector<ICallPromotionFunc::PromotionCandidate> |
| 257 | ICallPromotionFunc::getPromotionCandidatesForCallSite( |
| 258 | Instruction *Inst, const ArrayRef<InstrProfValueData> &ValueDataRef, |
Teresa Johnson | 1e44b5d | 2016-07-12 21:13:44 +0000 | [diff] [blame] | 259 | uint64_t TotalCount, uint32_t NumCandidates) { |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 260 | std::vector<PromotionCandidate> Ret; |
| 261 | |
| 262 | DEBUG(dbgs() << " \nWork on callsite #" << NumOfPGOICallsites << *Inst |
Teresa Johnson | 8950ad1 | 2016-07-12 21:29:05 +0000 | [diff] [blame] | 263 | << " Num_targets: " << ValueDataRef.size() |
Teresa Johnson | 1e44b5d | 2016-07-12 21:13:44 +0000 | [diff] [blame] | 264 | << " Num_candidates: " << NumCandidates << "\n"); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 265 | NumOfPGOICallsites++; |
| 266 | if (ICPCSSkip != 0 && NumOfPGOICallsites <= ICPCSSkip) { |
| 267 | DEBUG(dbgs() << " Skip: User options.\n"); |
| 268 | return Ret; |
| 269 | } |
| 270 | |
Teresa Johnson | 1e44b5d | 2016-07-12 21:13:44 +0000 | [diff] [blame] | 271 | for (uint32_t I = 0; I < NumCandidates; I++) { |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 272 | uint64_t Count = ValueDataRef[I].Count; |
| 273 | assert(Count <= TotalCount); |
| 274 | uint64_t Target = ValueDataRef[I].Value; |
| 275 | DEBUG(dbgs() << " Candidate " << I << " Count=" << Count |
| 276 | << " Target_func: " << Target << "\n"); |
| 277 | |
Teresa Johnson | ce7de9b | 2016-07-17 14:46:58 +0000 | [diff] [blame] | 278 | if (ICPInvokeOnly && dyn_cast<CallInst>(Inst)) { |
| 279 | DEBUG(dbgs() << " Not promote: User options.\n"); |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 280 | ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst) |
| 281 | << " Not promote: User options"); |
Teresa Johnson | ce7de9b | 2016-07-17 14:46:58 +0000 | [diff] [blame] | 282 | break; |
| 283 | } |
| 284 | if (ICPCallOnly && dyn_cast<InvokeInst>(Inst)) { |
| 285 | DEBUG(dbgs() << " Not promote: User option.\n"); |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 286 | ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst) |
| 287 | << " Not promote: User options"); |
Teresa Johnson | ce7de9b | 2016-07-17 14:46:58 +0000 | [diff] [blame] | 288 | break; |
| 289 | } |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 290 | if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) { |
| 291 | DEBUG(dbgs() << " Not promote: Cutoff reached.\n"); |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 292 | ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "CutOffReached", Inst) |
| 293 | << " Not promote: Cutoff reached"); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 294 | break; |
| 295 | } |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 296 | |
| 297 | Function *TargetFunction = Symtab->getFunction(Target); |
| 298 | if (TargetFunction == nullptr) { |
| 299 | DEBUG(dbgs() << " Not promote: Cannot find the target\n"); |
| 300 | ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UnableToFindTarget", Inst) |
| 301 | << "Cannot promote indirect call: target not found"); |
| 302 | break; |
| 303 | } |
| 304 | |
Dehao Chen | 6775f5d | 2017-01-30 22:46:37 +0000 | [diff] [blame] | 305 | const char *Reason = nullptr; |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 306 | if (!isLegalToPromote(Inst, TargetFunction, &Reason)) { |
| 307 | using namespace ore; |
| 308 | ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UnableToPromote", Inst) |
| 309 | << "Cannot promote indirect call to " |
| 310 | << NV("TargetFunction", TargetFunction) << " with count of " |
| 311 | << NV("Count", Count) << ": " << Reason); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 312 | break; |
| 313 | } |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 314 | |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 315 | Ret.push_back(PromotionCandidate(TargetFunction, Count)); |
| 316 | TotalCount -= Count; |
| 317 | } |
| 318 | return Ret; |
| 319 | } |
| 320 | |
| 321 | // Create a diamond structure for If_Then_Else. Also update the profile |
| 322 | // count. Do the fix-up for the invoke instruction. |
| 323 | static void createIfThenElse(Instruction *Inst, Function *DirectCallee, |
| 324 | uint64_t Count, uint64_t TotalCount, |
| 325 | BasicBlock **DirectCallBB, |
| 326 | BasicBlock **IndirectCallBB, |
| 327 | BasicBlock **MergeBB) { |
| 328 | CallSite CS(Inst); |
| 329 | Value *OrigCallee = CS.getCalledValue(); |
| 330 | |
| 331 | IRBuilder<> BBBuilder(Inst); |
| 332 | LLVMContext &Ctx = Inst->getContext(); |
| 333 | Value *BCI1 = |
| 334 | BBBuilder.CreateBitCast(OrigCallee, Type::getInt8PtrTy(Ctx), ""); |
| 335 | Value *BCI2 = |
| 336 | BBBuilder.CreateBitCast(DirectCallee, Type::getInt8PtrTy(Ctx), ""); |
| 337 | Value *PtrCmp = BBBuilder.CreateICmpEQ(BCI1, BCI2, ""); |
| 338 | |
| 339 | uint64_t ElseCount = TotalCount - Count; |
| 340 | uint64_t MaxCount = (Count >= ElseCount ? Count : ElseCount); |
| 341 | uint64_t Scale = calculateCountScale(MaxCount); |
| 342 | MDBuilder MDB(Inst->getContext()); |
| 343 | MDNode *BranchWeights = MDB.createBranchWeights( |
| 344 | scaleBranchCount(Count, Scale), scaleBranchCount(ElseCount, Scale)); |
| 345 | TerminatorInst *ThenTerm, *ElseTerm; |
| 346 | SplitBlockAndInsertIfThenElse(PtrCmp, Inst, &ThenTerm, &ElseTerm, |
| 347 | BranchWeights); |
| 348 | *DirectCallBB = ThenTerm->getParent(); |
| 349 | (*DirectCallBB)->setName("if.true.direct_targ"); |
| 350 | *IndirectCallBB = ElseTerm->getParent(); |
| 351 | (*IndirectCallBB)->setName("if.false.orig_indirect"); |
| 352 | *MergeBB = Inst->getParent(); |
| 353 | (*MergeBB)->setName("if.end.icp"); |
| 354 | |
| 355 | // Special handing of Invoke instructions. |
| 356 | InvokeInst *II = dyn_cast<InvokeInst>(Inst); |
| 357 | if (!II) |
| 358 | return; |
| 359 | |
| 360 | // We don't need branch instructions for invoke. |
| 361 | ThenTerm->eraseFromParent(); |
| 362 | ElseTerm->eraseFromParent(); |
| 363 | |
| 364 | // Add jump from Merge BB to the NormalDest. This is needed for the newly |
| 365 | // created direct invoke stmt -- as its NormalDst will be fixed up to MergeBB. |
| 366 | BranchInst::Create(II->getNormalDest(), *MergeBB); |
| 367 | } |
| 368 | |
| 369 | // Find the PHI in BB that have the CallResult as the operand. |
| 370 | static bool getCallRetPHINode(BasicBlock *BB, Instruction *Inst) { |
| 371 | BasicBlock *From = Inst->getParent(); |
| 372 | for (auto &I : *BB) { |
| 373 | PHINode *PHI = dyn_cast<PHINode>(&I); |
| 374 | if (!PHI) |
| 375 | continue; |
| 376 | int IX = PHI->getBasicBlockIndex(From); |
| 377 | if (IX == -1) |
| 378 | continue; |
| 379 | Value *V = PHI->getIncomingValue(IX); |
| 380 | if (dyn_cast<Instruction>(V) == Inst) |
| 381 | return true; |
| 382 | } |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | // This method fixes up PHI nodes in BB where BB is the UnwindDest of an |
| 387 | // invoke instruction. In BB, there may be PHIs with incoming block being |
| 388 | // OrigBB (the MergeBB after if-then-else splitting). After moving the invoke |
| 389 | // instructions to its own BB, OrigBB is no longer the predecessor block of BB. |
| 390 | // Instead two new predecessors are added: IndirectCallBB and DirectCallBB, |
| 391 | // so the PHI node's incoming BBs need to be fixed up accordingly. |
| 392 | static void fixupPHINodeForUnwind(Instruction *Inst, BasicBlock *BB, |
| 393 | BasicBlock *OrigBB, |
| 394 | BasicBlock *IndirectCallBB, |
| 395 | BasicBlock *DirectCallBB) { |
| 396 | for (auto &I : *BB) { |
| 397 | PHINode *PHI = dyn_cast<PHINode>(&I); |
| 398 | if (!PHI) |
| 399 | continue; |
| 400 | int IX = PHI->getBasicBlockIndex(OrigBB); |
| 401 | if (IX == -1) |
| 402 | continue; |
| 403 | Value *V = PHI->getIncomingValue(IX); |
| 404 | PHI->addIncoming(V, IndirectCallBB); |
| 405 | PHI->setIncomingBlock(IX, DirectCallBB); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | // This method fixes up PHI nodes in BB where BB is the NormalDest of an |
| 410 | // invoke instruction. In BB, there may be PHIs with incoming block being |
| 411 | // OrigBB (the MergeBB after if-then-else splitting). After moving the invoke |
| 412 | // instructions to its own BB, a new incoming edge will be added to the original |
| 413 | // NormalDstBB from the IndirectCallBB. |
| 414 | static void fixupPHINodeForNormalDest(Instruction *Inst, BasicBlock *BB, |
| 415 | BasicBlock *OrigBB, |
| 416 | BasicBlock *IndirectCallBB, |
| 417 | Instruction *NewInst) { |
| 418 | for (auto &I : *BB) { |
| 419 | PHINode *PHI = dyn_cast<PHINode>(&I); |
| 420 | if (!PHI) |
| 421 | continue; |
| 422 | int IX = PHI->getBasicBlockIndex(OrigBB); |
| 423 | if (IX == -1) |
| 424 | continue; |
| 425 | Value *V = PHI->getIncomingValue(IX); |
| 426 | if (dyn_cast<Instruction>(V) == Inst) { |
| 427 | PHI->setIncomingBlock(IX, IndirectCallBB); |
| 428 | PHI->addIncoming(NewInst, OrigBB); |
| 429 | continue; |
| 430 | } |
| 431 | PHI->addIncoming(V, IndirectCallBB); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // Add a bitcast instruction to the direct-call return value if needed. |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 436 | static Instruction *insertCallRetCast(const Instruction *Inst, |
| 437 | Instruction *DirectCallInst, |
| 438 | Function *DirectCallee) { |
| 439 | if (Inst->getType()->isVoidTy()) |
| 440 | return DirectCallInst; |
| 441 | |
| 442 | Type *CallRetType = Inst->getType(); |
| 443 | Type *FuncRetType = DirectCallee->getReturnType(); |
| 444 | if (FuncRetType == CallRetType) |
| 445 | return DirectCallInst; |
| 446 | |
| 447 | BasicBlock *InsertionBB; |
| 448 | if (CallInst *CI = dyn_cast<CallInst>(DirectCallInst)) |
| 449 | InsertionBB = CI->getParent(); |
| 450 | else |
| 451 | InsertionBB = (dyn_cast<InvokeInst>(DirectCallInst))->getNormalDest(); |
| 452 | |
| 453 | return (new BitCastInst(DirectCallInst, CallRetType, "", |
| 454 | InsertionBB->getTerminator())); |
| 455 | } |
| 456 | |
| 457 | // Create a DirectCall instruction in the DirectCallBB. |
| 458 | // Parameter Inst is the indirect-call (invoke) instruction. |
| 459 | // DirectCallee is the decl of the direct-call (invoke) target. |
| 460 | // DirecallBB is the BB that the direct-call (invoke) instruction is inserted. |
| 461 | // MergeBB is the bottom BB of the if-then-else-diamond after the |
| 462 | // transformation. For invoke instruction, the edges from DirectCallBB and |
| 463 | // IndirectCallBB to MergeBB are removed before this call (during |
| 464 | // createIfThenElse). |
| 465 | static Instruction *createDirectCallInst(const Instruction *Inst, |
| 466 | Function *DirectCallee, |
| 467 | BasicBlock *DirectCallBB, |
| 468 | BasicBlock *MergeBB) { |
| 469 | Instruction *NewInst = Inst->clone(); |
| 470 | if (CallInst *CI = dyn_cast<CallInst>(NewInst)) { |
| 471 | CI->setCalledFunction(DirectCallee); |
| 472 | CI->mutateFunctionType(DirectCallee->getFunctionType()); |
| 473 | } else { |
| 474 | // Must be an invoke instruction. Direct invoke's normal destination is |
| 475 | // fixed up to MergeBB. MergeBB is the place where return cast is inserted. |
| 476 | // Also since IndirectCallBB does not have an edge to MergeBB, there is no |
| 477 | // need to insert new PHIs into MergeBB. |
| 478 | InvokeInst *II = dyn_cast<InvokeInst>(NewInst); |
| 479 | assert(II); |
| 480 | II->setCalledFunction(DirectCallee); |
| 481 | II->mutateFunctionType(DirectCallee->getFunctionType()); |
| 482 | II->setNormalDest(MergeBB); |
| 483 | } |
| 484 | |
| 485 | DirectCallBB->getInstList().insert(DirectCallBB->getFirstInsertionPt(), |
| 486 | NewInst); |
| 487 | |
| 488 | // Clear the value profile data. |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 489 | NewInst->setMetadata(LLVMContext::MD_prof, nullptr); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 490 | CallSite NewCS(NewInst); |
| 491 | FunctionType *DirectCalleeType = DirectCallee->getFunctionType(); |
| 492 | unsigned ParamNum = DirectCalleeType->getFunctionNumParams(); |
| 493 | for (unsigned I = 0; I < ParamNum; ++I) { |
| 494 | Type *ATy = NewCS.getArgument(I)->getType(); |
| 495 | Type *PTy = DirectCalleeType->getParamType(I); |
| 496 | if (ATy != PTy) { |
| 497 | BitCastInst *BI = new BitCastInst(NewCS.getArgument(I), PTy, "", NewInst); |
| 498 | NewCS.setArgument(I, BI); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | return insertCallRetCast(Inst, NewInst, DirectCallee); |
| 503 | } |
| 504 | |
| 505 | // Create a PHI to unify the return values of calls. |
| 506 | static void insertCallRetPHI(Instruction *Inst, Instruction *CallResult, |
| 507 | Function *DirectCallee) { |
| 508 | if (Inst->getType()->isVoidTy()) |
| 509 | return; |
| 510 | |
| 511 | BasicBlock *RetValBB = CallResult->getParent(); |
| 512 | |
| 513 | BasicBlock *PHIBB; |
| 514 | if (InvokeInst *II = dyn_cast<InvokeInst>(CallResult)) |
| 515 | RetValBB = II->getNormalDest(); |
| 516 | |
| 517 | PHIBB = RetValBB->getSingleSuccessor(); |
| 518 | if (getCallRetPHINode(PHIBB, Inst)) |
| 519 | return; |
| 520 | |
| 521 | PHINode *CallRetPHI = PHINode::Create(Inst->getType(), 0); |
| 522 | PHIBB->getInstList().push_front(CallRetPHI); |
| 523 | Inst->replaceAllUsesWith(CallRetPHI); |
| 524 | CallRetPHI->addIncoming(Inst, Inst->getParent()); |
| 525 | CallRetPHI->addIncoming(CallResult, RetValBB); |
| 526 | } |
| 527 | |
| 528 | // This function does the actual indirect-call promotion transformation: |
| 529 | // For an indirect-call like: |
| 530 | // Ret = (*Foo)(Args); |
| 531 | // It transforms to: |
| 532 | // if (Foo == DirectCallee) |
| 533 | // Ret1 = DirectCallee(Args); |
| 534 | // else |
| 535 | // Ret2 = (*Foo)(Args); |
| 536 | // Ret = phi(Ret1, Ret2); |
| 537 | // It adds type casts for the args do not match the parameters and the return |
| 538 | // value. Branch weights metadata also updated. |
Dehao Chen | cc75d24 | 2017-02-23 22:15:18 +0000 | [diff] [blame] | 539 | // If \p AttachProfToDirectCall is true, a prof metadata is attached to the |
| 540 | // new direct call to contain \p Count. This is used by SamplePGO inliner to |
| 541 | // check callsite hotness. |
Dehao Chen | 14bf029 | 2017-01-23 23:18:24 +0000 | [diff] [blame] | 542 | // Returns the promoted direct call instruction. |
| 543 | Instruction *llvm::promoteIndirectCall(Instruction *Inst, |
| 544 | Function *DirectCallee, uint64_t Count, |
Dehao Chen | cc75d24 | 2017-02-23 22:15:18 +0000 | [diff] [blame] | 545 | uint64_t TotalCount, |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 546 | bool AttachProfToDirectCall, |
| 547 | OptimizationRemarkEmitter *ORE) { |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 548 | assert(DirectCallee != nullptr); |
| 549 | BasicBlock *BB = Inst->getParent(); |
| 550 | // Just to suppress the non-debug build warning. |
| 551 | (void)BB; |
| 552 | DEBUG(dbgs() << "\n\n== Basic Block Before ==\n"); |
| 553 | DEBUG(dbgs() << *BB << "\n"); |
| 554 | |
| 555 | BasicBlock *DirectCallBB, *IndirectCallBB, *MergeBB; |
| 556 | createIfThenElse(Inst, DirectCallee, Count, TotalCount, &DirectCallBB, |
| 557 | &IndirectCallBB, &MergeBB); |
| 558 | |
| 559 | Instruction *NewInst = |
| 560 | createDirectCallInst(Inst, DirectCallee, DirectCallBB, MergeBB); |
| 561 | |
Dehao Chen | cc75d24 | 2017-02-23 22:15:18 +0000 | [diff] [blame] | 562 | if (AttachProfToDirectCall) { |
| 563 | SmallVector<uint32_t, 1> Weights; |
| 564 | Weights.push_back(Count); |
| 565 | MDBuilder MDB(NewInst->getContext()); |
| 566 | dyn_cast<Instruction>(NewInst->stripPointerCasts()) |
| 567 | ->setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights)); |
| 568 | } |
| 569 | |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 570 | // Move Inst from MergeBB to IndirectCallBB. |
| 571 | Inst->removeFromParent(); |
| 572 | IndirectCallBB->getInstList().insert(IndirectCallBB->getFirstInsertionPt(), |
| 573 | Inst); |
| 574 | |
| 575 | if (InvokeInst *II = dyn_cast<InvokeInst>(Inst)) { |
| 576 | // At this point, the original indirect invoke instruction has the original |
| 577 | // UnwindDest and NormalDest. For the direct invoke instruction, the |
| 578 | // NormalDest points to MergeBB, and MergeBB jumps to the original |
| 579 | // NormalDest. MergeBB might have a new bitcast instruction for the return |
| 580 | // value. The PHIs are with the original NormalDest. Since we now have two |
| 581 | // incoming edges to NormalDest and UnwindDest, we have to do some fixups. |
| 582 | // |
| 583 | // UnwindDest will not use the return value. So pass nullptr here. |
| 584 | fixupPHINodeForUnwind(Inst, II->getUnwindDest(), MergeBB, IndirectCallBB, |
| 585 | DirectCallBB); |
| 586 | // We don't need to update the operand from NormalDest for DirectCallBB. |
| 587 | // Pass nullptr here. |
| 588 | fixupPHINodeForNormalDest(Inst, II->getNormalDest(), MergeBB, |
| 589 | IndirectCallBB, NewInst); |
| 590 | } |
| 591 | |
| 592 | insertCallRetPHI(Inst, NewInst, DirectCallee); |
| 593 | |
| 594 | DEBUG(dbgs() << "\n== Basic Blocks After ==\n"); |
| 595 | DEBUG(dbgs() << *BB << *DirectCallBB << *IndirectCallBB << *MergeBB << "\n"); |
| 596 | |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 597 | using namespace ore; |
| 598 | if (ORE) |
| 599 | ORE->emit(OptimizationRemark(DEBUG_TYPE, "Promoted", Inst) |
| 600 | << "Promote indirect call to " << NV("DirectCallee", DirectCallee) |
| 601 | << " with count " << NV("Count", Count) << " out of " |
| 602 | << NV("TotalCount", TotalCount)); |
Dehao Chen | 14bf029 | 2017-01-23 23:18:24 +0000 | [diff] [blame] | 603 | return NewInst; |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | // Promote indirect-call to conditional direct-call for one callsite. |
| 607 | uint32_t ICallPromotionFunc::tryToPromote( |
| 608 | Instruction *Inst, const std::vector<PromotionCandidate> &Candidates, |
| 609 | uint64_t &TotalCount) { |
| 610 | uint32_t NumPromoted = 0; |
| 611 | |
| 612 | for (auto &C : Candidates) { |
| 613 | uint64_t Count = C.Count; |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 614 | promoteIndirectCall(Inst, C.TargetFunction, Count, TotalCount, SamplePGO, |
| 615 | &ORE); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 616 | assert(TotalCount >= Count); |
| 617 | TotalCount -= Count; |
| 618 | NumOfPGOICallPromotion++; |
| 619 | NumPromoted++; |
| 620 | } |
| 621 | return NumPromoted; |
| 622 | } |
| 623 | |
| 624 | // Traverse all the indirect-call callsite and get the value profile |
| 625 | // annotation to perform indirect-call promotion. |
Dehao Chen | 34cfcb2 | 2017-08-08 20:57:33 +0000 | [diff] [blame] | 626 | bool ICallPromotionFunc::processFunction(ProfileSummaryInfo *PSI) { |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 627 | bool Changed = false; |
Teresa Johnson | 1e44b5d | 2016-07-12 21:13:44 +0000 | [diff] [blame] | 628 | ICallPromotionAnalysis ICallAnalysis; |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 629 | for (auto &I : findIndirectCallSites(F)) { |
Teresa Johnson | 1e44b5d | 2016-07-12 21:13:44 +0000 | [diff] [blame] | 630 | uint32_t NumVals, NumCandidates; |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 631 | uint64_t TotalCount; |
Teresa Johnson | 1e44b5d | 2016-07-12 21:13:44 +0000 | [diff] [blame] | 632 | auto ICallProfDataRef = ICallAnalysis.getPromotionCandidatesForInstruction( |
| 633 | I, NumVals, TotalCount, NumCandidates); |
Dehao Chen | 34cfcb2 | 2017-08-08 20:57:33 +0000 | [diff] [blame] | 634 | if (!NumCandidates || |
| 635 | (PSI && PSI->hasProfileSummary() && !PSI->isHotCount(TotalCount))) |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 636 | continue; |
Teresa Johnson | 1e44b5d | 2016-07-12 21:13:44 +0000 | [diff] [blame] | 637 | auto PromotionCandidates = getPromotionCandidatesForCallSite( |
| 638 | I, ICallProfDataRef, TotalCount, NumCandidates); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 639 | uint32_t NumPromoted = tryToPromote(I, PromotionCandidates, TotalCount); |
| 640 | if (NumPromoted == 0) |
| 641 | continue; |
| 642 | |
| 643 | Changed = true; |
| 644 | // Adjust the MD.prof metadata. First delete the old one. |
Eugene Zelenko | cdc7161 | 2016-08-11 17:20:18 +0000 | [diff] [blame] | 645 | I->setMetadata(LLVMContext::MD_prof, nullptr); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 646 | // If all promoted, we don't need the MD.prof metadata. |
| 647 | if (TotalCount == 0 || NumPromoted == NumVals) |
| 648 | continue; |
| 649 | // Otherwise we need update with the un-promoted records back. |
Teresa Johnson | 1e44b5d | 2016-07-12 21:13:44 +0000 | [diff] [blame] | 650 | annotateValueSite(*M, *I, ICallProfDataRef.slice(NumPromoted), TotalCount, |
| 651 | IPVK_IndirectCallTarget, NumCandidates); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 652 | } |
| 653 | return Changed; |
| 654 | } |
| 655 | |
| 656 | // A wrapper function that does the actual work. |
Dehao Chen | 34cfcb2 | 2017-08-08 20:57:33 +0000 | [diff] [blame] | 657 | static bool promoteIndirectCalls(Module &M, ProfileSummaryInfo *PSI, |
| 658 | bool InLTO, bool SamplePGO, |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 659 | ModuleAnalysisManager *AM = nullptr) { |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 660 | if (DisableICP) |
| 661 | return false; |
| 662 | InstrProfSymtab Symtab; |
Vedant Kumar | b5794ca | 2017-06-20 01:38:56 +0000 | [diff] [blame] | 663 | if (Error E = Symtab.create(M, InLTO)) { |
| 664 | std::string SymtabFailure = toString(std::move(E)); |
| 665 | DEBUG(dbgs() << "Failed to create symtab: " << SymtabFailure << "\n"); |
| 666 | (void)SymtabFailure; |
| 667 | return false; |
| 668 | } |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 669 | bool Changed = false; |
| 670 | for (auto &F : M) { |
| 671 | if (F.isDeclaration()) |
| 672 | continue; |
| 673 | if (F.hasFnAttribute(Attribute::OptimizeNone)) |
| 674 | continue; |
Adam Nemet | 0d8b5d6 | 2017-07-27 16:54:15 +0000 | [diff] [blame] | 675 | |
| 676 | std::unique_ptr<OptimizationRemarkEmitter> OwnedORE; |
| 677 | OptimizationRemarkEmitter *ORE; |
| 678 | if (AM) { |
| 679 | auto &FAM = |
| 680 | AM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
| 681 | ORE = &FAM.getResult<OptimizationRemarkEmitterAnalysis>(F); |
| 682 | } else { |
| 683 | OwnedORE = make_unique<OptimizationRemarkEmitter>(&F); |
| 684 | ORE = OwnedORE.get(); |
| 685 | } |
| 686 | |
| 687 | ICallPromotionFunc ICallPromotion(F, &M, &Symtab, SamplePGO, *ORE); |
Dehao Chen | 34cfcb2 | 2017-08-08 20:57:33 +0000 | [diff] [blame] | 688 | bool FuncChanged = ICallPromotion.processFunction(PSI); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 689 | if (ICPDUMPAFTER && FuncChanged) { |
| 690 | DEBUG(dbgs() << "\n== IR Dump After =="; F.print(dbgs())); |
| 691 | DEBUG(dbgs() << "\n"); |
| 692 | } |
| 693 | Changed |= FuncChanged; |
| 694 | if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) { |
| 695 | DEBUG(dbgs() << " Stop: Cutoff reached.\n"); |
| 696 | break; |
| 697 | } |
| 698 | } |
| 699 | return Changed; |
| 700 | } |
| 701 | |
Xinliang David Li | 7261618 | 2016-05-15 01:04:24 +0000 | [diff] [blame] | 702 | bool PGOIndirectCallPromotionLegacyPass::runOnModule(Module &M) { |
Dehao Chen | 34cfcb2 | 2017-08-08 20:57:33 +0000 | [diff] [blame] | 703 | ProfileSummaryInfo *PSI = |
| 704 | getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); |
| 705 | |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 706 | // Command-line option has the priority for InLTO. |
Dehao Chen | 34cfcb2 | 2017-08-08 20:57:33 +0000 | [diff] [blame] | 707 | return promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode, |
Dehao Chen | cc75d24 | 2017-02-23 22:15:18 +0000 | [diff] [blame] | 708 | SamplePGO | ICPSamplePGOMode); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 709 | } |
Xinliang David Li | f3c7a35 | 2016-05-16 16:31:07 +0000 | [diff] [blame] | 710 | |
Dehao Chen | cc75d24 | 2017-02-23 22:15:18 +0000 | [diff] [blame] | 711 | PreservedAnalyses PGOIndirectCallPromotion::run(Module &M, |
| 712 | ModuleAnalysisManager &AM) { |
Dehao Chen | 34cfcb2 | 2017-08-08 20:57:33 +0000 | [diff] [blame] | 713 | ProfileSummaryInfo *PSI = &AM.getResult<ProfileSummaryAnalysis>(M); |
| 714 | |
| 715 | if (!promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode, |
| 716 | SamplePGO | ICPSamplePGOMode, &AM)) |
Xinliang David Li | f3c7a35 | 2016-05-16 16:31:07 +0000 | [diff] [blame] | 717 | return PreservedAnalyses::all(); |
| 718 | |
| 719 | return PreservedAnalyses::none(); |
| 720 | } |