193a013c760631e696e6ae3042551c16d4636ade6Michael Gottesman//=- MachineBranchProbabilityInfo.h - Branch Probability Analysis -*- C++ -*-=// 27cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak// 37cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak// The LLVM Compiler Infrastructure 47cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak// 57cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak// This file is distributed under the University of Illinois Open Source 67cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak// License. See LICENSE.TXT for details. 77cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak// 87cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak//===----------------------------------------------------------------------===// 97cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak// 107cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak// This pass is used to evaluate branch probabilties on machine basic blocks. 117cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak// 127cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak//===----------------------------------------------------------------------===// 137cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 147cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak#ifndef LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H 157cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak#define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H 167cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 17990ca5517fd6666d4049b6b8281d9df99da11637Jakob Stoklund Olesen#include "llvm/CodeGen/MachineBasicBlock.h" 18255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/Pass.h" 197cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak#include "llvm/Support/BranchProbability.h" 207cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak#include <climits> 217cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 227cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszaknamespace llvm { 237cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 247cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszakclass MachineBranchProbabilityInfo : public ImmutablePass { 252d24e2a396a1d211baaeedf32148a3b657240170David Blaikie virtual void anchor(); 267cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 277cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // Default weight value. Used when we don't have information about the edge. 287cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of 297cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // the successors have a weight yet. But it doesn't make sense when providing 307cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // weight to an edge that may have siblings with non-zero weights. This can 317cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // be handled various ways, but it's probably fine for an edge with unknown 327cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // weight to just "inherit" the non-zero weight of an adjacent successor. 337cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak static const uint32_t DEFAULT_WEIGHT = 16; 347cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 357cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszakpublic: 367cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak static char ID; 377cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 387cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak MachineBranchProbabilityInfo() : ImmutablePass(ID) { 397cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak PassRegistry &Registry = *PassRegistry::getPassRegistry(); 407cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak initializeMachineBranchProbabilityInfoPass(Registry); 417cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak } 427cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 4336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void getAnalysisUsage(AnalysisUsage &AU) const override { 447cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak AU.setPreservesAll(); 457cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak } 467cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 477cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // Return edge weight. If we don't have any informations about it - return 487cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // DEFAULT_WEIGHT. 4925101bb2a799a36be9f077ee2fc2dcf0df2b6efbJakub Staszak uint32_t getEdgeWeight(const MachineBasicBlock *Src, 5025101bb2a799a36be9f077ee2fc2dcf0df2b6efbJakub Staszak const MachineBasicBlock *Dst) const; 517cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 52990ca5517fd6666d4049b6b8281d9df99da11637Jakob Stoklund Olesen // Same thing, but using a const_succ_iterator from Src. This is faster when 53990ca5517fd6666d4049b6b8281d9df99da11637Jakob Stoklund Olesen // the iterator is already available. 54990ca5517fd6666d4049b6b8281d9df99da11637Jakob Stoklund Olesen uint32_t getEdgeWeight(const MachineBasicBlock *Src, 55990ca5517fd6666d4049b6b8281d9df99da11637Jakob Stoklund Olesen MachineBasicBlock::const_succ_iterator Dst) const; 56990ca5517fd6666d4049b6b8281d9df99da11637Jakob Stoklund Olesen 57340d596509129de8c3fa9dbe4184a2b148b78757Chandler Carruth // Get sum of the block successors' weights, potentially scaling them to fit 58340d596509129de8c3fa9dbe4184a2b148b78757Chandler Carruth // within 32-bits. If scaling is required, sets Scale based on the necessary 59340d596509129de8c3fa9dbe4184a2b148b78757Chandler Carruth // adjustment. Any edge weights used with the sum should be divided by Scale. 6025101bb2a799a36be9f077ee2fc2dcf0df2b6efbJakub Staszak uint32_t getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const; 61340d596509129de8c3fa9dbe4184a2b148b78757Chandler Carruth 627cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // A 'Hot' edge is an edge which probability is >= 80%. 6336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool isEdgeHot(const MachineBasicBlock *Src, 6436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const MachineBasicBlock *Dst) const; 657cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 667cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // Return a hot successor for the block BB or null if there isn't one. 67c4e15628255b24cb17138404abe3d94bde811e25Chandler Carruth // NB: This routine's complexity is linear on the number of successors. 687cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak MachineBasicBlock *getHotSucc(MachineBasicBlock *MBB) const; 697cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 707cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // Return a probability as a fraction between 0 (0% probability) and 717cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // 1 (100% probability), however the value is never equal to 0, and can be 1 7294c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru // only iff SRC block has only one successor. 73883f2fab47ca2f7f6efebb10a5e859f5804bed07Chandler Carruth // NB: This routine's complexity is linear on the number of successors of 74883f2fab47ca2f7f6efebb10a5e859f5804bed07Chandler Carruth // Src. Querying sequentially for each successor's probability is a quadratic 75883f2fab47ca2f7f6efebb10a5e859f5804bed07Chandler Carruth // query pattern. 7636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines BranchProbability getEdgeProbability(const MachineBasicBlock *Src, 7736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const MachineBasicBlock *Dst) const; 787cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 797cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // Print value between 0 (0% probability) and 1 (100% probability), 8094c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru // however the value is never equal to 0, and can be 1 only iff SRC block 817cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak // has only one successor. 8236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines raw_ostream &printEdgeProbability(raw_ostream &OS, 8336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const MachineBasicBlock *Src, 8436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const MachineBasicBlock *Dst) const; 857cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak}; 867cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 877cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak} 887cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 897cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak 907cc2b07437a1243c33324549a1904fefc5f1845eJakub Staszak#endif 91