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