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