LowerExpectIntrinsic.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //===- LowerExpectIntrinsic.cpp - Lower expect intrinsic ------------------===//
  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 pass lowers the 'expect' intrinsic to LLVM metadata.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/IR/BasicBlock.h"
  17. #include "llvm/IR/Constants.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include "llvm/IR/Intrinsics.h"
  21. #include "llvm/IR/LLVMContext.h"
  22. #include "llvm/IR/MDBuilder.h"
  23. #include "llvm/IR/Metadata.h"
  24. #include "llvm/Pass.h"
  25. #include "llvm/Support/CommandLine.h"
  26. #include "llvm/Support/Debug.h"
  27. #include "llvm/Transforms/Scalar.h"
  28. using namespace llvm;
  29. #define DEBUG_TYPE "lower-expect-intrinsic"
  30. STATISTIC(ExpectIntrinsicsHandled,
  31. "Number of 'expect' intrinsic instructions handled");
  32. #if 0 // HLSL Change Starts - option pending
  33. static cl::opt<uint32_t>
  34. LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(64),
  35. cl::desc("Weight of the branch likely to be taken (default = 64)"));
  36. static cl::opt<uint32_t>
  37. UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4),
  38. cl::desc("Weight of the branch unlikely to be taken (default = 4)"));
  39. #else
  40. static const uint32_t LikelyBranchWeight = 64;
  41. static const uint32_t UnlikelyBranchWeight = 4;
  42. #endif
  43. static bool handleSwitchExpect(SwitchInst &SI) {
  44. CallInst *CI = dyn_cast<CallInst>(SI.getCondition());
  45. if (!CI)
  46. return false;
  47. Function *Fn = CI->getCalledFunction();
  48. if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
  49. return false;
  50. Value *ArgValue = CI->getArgOperand(0);
  51. ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  52. if (!ExpectedValue)
  53. return false;
  54. SwitchInst::CaseIt Case = SI.findCaseValue(ExpectedValue);
  55. unsigned n = SI.getNumCases(); // +1 for default case.
  56. #if 0 // HLSL Change - help the compiler pick the right constructor overload
  57. SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeight);
  58. #else
  59. SmallVector<uint32_t, 16> Weights;
  60. Weights.assign(n + 1, UnlikelyBranchWeight);
  61. #endif
  62. if (Case == SI.case_default())
  63. Weights[0] = LikelyBranchWeight;
  64. else
  65. Weights[Case.getCaseIndex() + 1] = LikelyBranchWeight;
  66. SI.setMetadata(LLVMContext::MD_prof,
  67. MDBuilder(CI->getContext()).createBranchWeights(Weights));
  68. SI.setCondition(ArgValue);
  69. return true;
  70. }
  71. static bool handleBranchExpect(BranchInst &BI) {
  72. if (BI.isUnconditional())
  73. return false;
  74. // Handle non-optimized IR code like:
  75. // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
  76. // %tobool = icmp ne i64 %expval, 0
  77. // br i1 %tobool, label %if.then, label %if.end
  78. //
  79. // Or the following simpler case:
  80. // %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1)
  81. // br i1 %expval, label %if.then, label %if.end
  82. CallInst *CI;
  83. ICmpInst *CmpI = dyn_cast<ICmpInst>(BI.getCondition());
  84. if (!CmpI) {
  85. CI = dyn_cast<CallInst>(BI.getCondition());
  86. } else {
  87. if (CmpI->getPredicate() != CmpInst::ICMP_NE)
  88. return false;
  89. CI = dyn_cast<CallInst>(CmpI->getOperand(0));
  90. }
  91. if (!CI)
  92. return false;
  93. Function *Fn = CI->getCalledFunction();
  94. if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
  95. return false;
  96. Value *ArgValue = CI->getArgOperand(0);
  97. ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
  98. if (!ExpectedValue)
  99. return false;
  100. MDBuilder MDB(CI->getContext());
  101. MDNode *Node;
  102. // If expect value is equal to 1 it means that we are more likely to take
  103. // branch 0, in other case more likely is branch 1.
  104. if (ExpectedValue->isOne())
  105. Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
  106. else
  107. Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
  108. BI.setMetadata(LLVMContext::MD_prof, Node);
  109. if (CmpI)
  110. CmpI->setOperand(0, ArgValue);
  111. else
  112. BI.setCondition(ArgValue);
  113. return true;
  114. }
  115. static bool lowerExpectIntrinsic(Function &F) {
  116. bool Changed = false;
  117. for (BasicBlock &BB : F) {
  118. // Create "block_weights" metadata.
  119. if (BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
  120. if (handleBranchExpect(*BI))
  121. ExpectIntrinsicsHandled++;
  122. } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
  123. if (handleSwitchExpect(*SI))
  124. ExpectIntrinsicsHandled++;
  125. }
  126. // remove llvm.expect intrinsics.
  127. for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) {
  128. CallInst *CI = dyn_cast<CallInst>(BI++);
  129. if (!CI)
  130. continue;
  131. Function *Fn = CI->getCalledFunction();
  132. if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
  133. Value *Exp = CI->getArgOperand(0);
  134. CI->replaceAllUsesWith(Exp);
  135. CI->eraseFromParent();
  136. Changed = true;
  137. }
  138. }
  139. }
  140. return Changed;
  141. }
  142. PreservedAnalyses LowerExpectIntrinsicPass::run(Function &F) {
  143. if (lowerExpectIntrinsic(F))
  144. return PreservedAnalyses::none();
  145. return PreservedAnalyses::all();
  146. }
  147. namespace {
  148. /// \brief Legacy pass for lowering expect intrinsics out of the IR.
  149. ///
  150. /// When this pass is run over a function it uses expect intrinsics which feed
  151. /// branches and switches to provide branch weight metadata for those
  152. /// terminators. It then removes the expect intrinsics from the IR so the rest
  153. /// of the optimizer can ignore them.
  154. class LowerExpectIntrinsic : public FunctionPass {
  155. public:
  156. static char ID;
  157. LowerExpectIntrinsic() : FunctionPass(ID) {
  158. initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
  159. }
  160. bool runOnFunction(Function &F) override { return lowerExpectIntrinsic(F); }
  161. };
  162. }
  163. char LowerExpectIntrinsic::ID = 0;
  164. INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect",
  165. "Lower 'expect' Intrinsics", false, false)
  166. FunctionPass *llvm::createLowerExpectIntrinsicPass() {
  167. return new LowerExpectIntrinsic();
  168. }