brintos

brintos / llvm-project-archived public Read only

0
0
Text · 13.6 KiB · 893f99a Raw
372 lines · cpp
1//===---- llvm/MDBuilder.cpp - Builder for LLVM metadata ------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file defines the MDBuilder class, which is used as a convenient way to10// create LLVM metadata with a consistent and simplified interface.11//12//===----------------------------------------------------------------------===//13 14#include "llvm/IR/MDBuilder.h"15#include "llvm/IR/Constants.h"16#include "llvm/IR/Function.h"17#include "llvm/IR/Metadata.h"18#include "llvm/IR/ProfDataUtils.h"19using namespace llvm;20 21MDString *MDBuilder::createString(StringRef Str) {22  return MDString::get(Context, Str);23}24 25ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {26  return ConstantAsMetadata::get(C);27}28 29MDNode *MDBuilder::createFPMath(float Accuracy) {30  if (Accuracy == 0.0)31    return nullptr;32  assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");33  auto *Op =34      createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));35  return MDNode::get(Context, Op);36}37 38MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,39                                       uint32_t FalseWeight, bool IsExpected) {40  return createBranchWeights({TrueWeight, FalseWeight}, IsExpected);41}42 43MDNode *MDBuilder::createLikelyBranchWeights() {44  // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp45  return createBranchWeights((1U << 20) - 1, 1);46}47 48MDNode *MDBuilder::createUnlikelyBranchWeights() {49  // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp50  return createBranchWeights(1, (1U << 20) - 1);51}52 53MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights,54                                       bool IsExpected) {55  assert(Weights.size() >= 1 && "Need at least one branch weights!");56 57  unsigned int Offset = IsExpected ? 2 : 1;58  SmallVector<Metadata *, 4> Vals(Weights.size() + Offset);59  Vals[0] = createString(MDProfLabels::BranchWeights);60  if (IsExpected)61    Vals[1] = createString(MDProfLabels::ExpectedBranchWeights);62 63  Type *Int32Ty = Type::getInt32Ty(Context);64  for (unsigned i = 0, e = Weights.size(); i != e; ++i)65    Vals[i + Offset] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));66 67  return MDNode::get(Context, Vals);68}69 70MDNode *MDBuilder::createUnpredictable() { return MDNode::get(Context, {}); }71 72MDNode *MDBuilder::createFunctionEntryCount(73    uint64_t Count, bool Synthetic,74    const DenseSet<GlobalValue::GUID> *Imports) {75  Type *Int64Ty = Type::getInt64Ty(Context);76  SmallVector<Metadata *, 8> Ops;77  if (Synthetic)78    Ops.push_back(createString(MDProfLabels::SyntheticFunctionEntryCount));79  else80    Ops.push_back(createString(MDProfLabels::FunctionEntryCount));81  Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));82  if (Imports) {83    SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());84    llvm::sort(OrderID);85    for (auto ID : OrderID)86      Ops.push_back(createConstant(ConstantInt::get(Int64Ty, ID)));87  }88  return MDNode::get(Context, Ops);89}90 91MDNode *MDBuilder::createGlobalObjectSectionPrefix(StringRef Prefix) {92  return MDNode::get(Context,93                     {createString("section_prefix"), createString(Prefix)});94}95 96MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {97  assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");98 99  Type *Ty = IntegerType::get(Context, Lo.getBitWidth());100  return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi));101}102 103MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) {104  // If the range is everything then it is useless.105  if (Hi == Lo)106    return nullptr;107 108  // Return the range [Lo, Hi).109  return MDNode::get(Context, {createConstant(Lo), createConstant(Hi)});110}111 112MDNode *MDBuilder::createCallees(ArrayRef<Function *> Callees) {113  SmallVector<Metadata *, 4> Ops;114  for (Function *F : Callees)115    Ops.push_back(createConstant(F));116  return MDNode::get(Context, Ops);117}118 119MDNode *MDBuilder::createCallbackEncoding(unsigned CalleeArgNo,120                                          ArrayRef<int> Arguments,121                                          bool VarArgArePassed) {122  SmallVector<Metadata *, 4> Ops;123 124  Type *Int64 = Type::getInt64Ty(Context);125  Ops.push_back(createConstant(ConstantInt::get(Int64, CalleeArgNo)));126 127  for (int ArgNo : Arguments)128    Ops.push_back(createConstant(ConstantInt::get(Int64, ArgNo, true)));129 130  Type *Int1 = Type::getInt1Ty(Context);131  Ops.push_back(createConstant(ConstantInt::get(Int1, VarArgArePassed)));132 133  return MDNode::get(Context, Ops);134}135 136MDNode *MDBuilder::mergeCallbackEncodings(MDNode *ExistingCallbacks,137                                          MDNode *NewCB) {138  if (!ExistingCallbacks)139    return MDNode::get(Context, {NewCB});140 141  auto *NewCBCalleeIdxAsCM = cast<ConstantAsMetadata>(NewCB->getOperand(0));142  uint64_t NewCBCalleeIdx =143      cast<ConstantInt>(NewCBCalleeIdxAsCM->getValue())->getZExtValue();144  (void)NewCBCalleeIdx;145 146  SmallVector<Metadata *, 4> Ops;147  unsigned NumExistingOps = ExistingCallbacks->getNumOperands();148  Ops.resize(NumExistingOps + 1);149 150  for (unsigned u = 0; u < NumExistingOps; u++) {151    Ops[u] = ExistingCallbacks->getOperand(u);152 153    auto *OldCBCalleeIdxAsCM =154        cast<ConstantAsMetadata>(cast<MDNode>(Ops[u])->getOperand(0));155    uint64_t OldCBCalleeIdx =156        cast<ConstantInt>(OldCBCalleeIdxAsCM->getValue())->getZExtValue();157    (void)OldCBCalleeIdx;158    assert(NewCBCalleeIdx != OldCBCalleeIdx &&159           "Cannot map a callback callee index twice!");160  }161 162  Ops[NumExistingOps] = NewCB;163  return MDNode::get(Context, Ops);164}165 166MDNode *MDBuilder::createRTTIPointerPrologue(Constant *PrologueSig,167                                             Constant *RTTI) {168  SmallVector<Metadata *, 4> Ops;169  Ops.push_back(createConstant(PrologueSig));170  Ops.push_back(createConstant(RTTI));171  return MDNode::get(Context, Ops);172}173 174MDNode *MDBuilder::createPCSections(ArrayRef<PCSection> Sections) {175  SmallVector<Metadata *, 2> Ops;176 177  for (const auto &Entry : Sections) {178    const StringRef &Sec = Entry.first;179    Ops.push_back(createString(Sec));180 181    // If auxiliary data for this section exists, append it.182    const SmallVector<Constant *> &AuxConsts = Entry.second;183    if (!AuxConsts.empty()) {184      SmallVector<Metadata *, 1> AuxMDs;185      AuxMDs.reserve(AuxConsts.size());186      for (Constant *C : AuxConsts)187        AuxMDs.push_back(createConstant(C));188      Ops.push_back(MDNode::get(Context, AuxMDs));189    }190  }191 192  return MDNode::get(Context, Ops);193}194 195MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {196  SmallVector<Metadata *, 3> Args(1, nullptr);197  if (Extra)198    Args.push_back(Extra);199  if (!Name.empty())200    Args.push_back(createString(Name));201  MDNode *Root = MDNode::getDistinct(Context, Args);202 203  // At this point we have204  //   !0 = distinct !{null} <- root205  // Replace the reserved operand with the root node itself.206  Root->replaceOperandWith(0, Root);207 208  // We now have209  //   !0 = distinct !{!0} <- root210  return Root;211}212 213MDNode *MDBuilder::createTBAARoot(StringRef Name) {214  return MDNode::get(Context, createString(Name));215}216 217/// Return metadata for a non-root TBAA node with the given name,218/// parent in the TBAA tree, and value for 'pointsToConstantMemory'.219MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,220                                  bool isConstant) {221  if (isConstant) {222    Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);223    return MDNode::get(Context,224                       {createString(Name), Parent, createConstant(Flags)});225  }226  return MDNode::get(Context, {createString(Name), Parent});227}228 229MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {230  return MDNode::get(Context, createString(Name));231}232 233MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {234  return MDNode::get(Context, {createString(Name), Domain});235}236 237/// Return metadata for a tbaa.struct node with the given238/// struct field descriptions.239MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {240  SmallVector<Metadata *, 4> Vals(Fields.size() * 3);241  Type *Int64 = Type::getInt64Ty(Context);242  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {243    Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));244    Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));245    Vals[i * 3 + 2] = Fields[i].Type;246  }247  return MDNode::get(Context, Vals);248}249 250/// Return metadata for a TBAA struct node in the type DAG251/// with the given name, a list of pairs (offset, field type in the type DAG).252MDNode *MDBuilder::createTBAAStructTypeNode(253    StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {254  SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);255  Type *Int64 = Type::getInt64Ty(Context);256  Ops[0] = createString(Name);257  for (unsigned i = 0, e = Fields.size(); i != e; ++i) {258    Ops[i * 2 + 1] = Fields[i].first;259    Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));260  }261  return MDNode::get(Context, Ops);262}263 264/// Return metadata for a TBAA scalar type node with the265/// given name, an offset and a parent in the TBAA type DAG.266MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,267                                            uint64_t Offset) {268  ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);269  return MDNode::get(Context,270                     {createString(Name), Parent, createConstant(Off)});271}272 273/// Return metadata for a TBAA tag node with the given274/// base type, access type and offset relative to the base type.275MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,276                                           uint64_t Offset, bool IsConstant) {277  IntegerType *Int64 = Type::getInt64Ty(Context);278  ConstantInt *Off = ConstantInt::get(Int64, Offset);279  if (IsConstant) {280    return MDNode::get(Context, {BaseType, AccessType, createConstant(Off),281                                 createConstant(ConstantInt::get(Int64, 1))});282  }283  return MDNode::get(Context, {BaseType, AccessType, createConstant(Off)});284}285 286MDNode *MDBuilder::createTBAATypeNode(MDNode *Parent, uint64_t Size,287                                      Metadata *Id,288                                      ArrayRef<TBAAStructField> Fields) {289  SmallVector<Metadata *, 4> Ops(3 + Fields.size() * 3);290  Type *Int64 = Type::getInt64Ty(Context);291  Ops[0] = Parent;292  Ops[1] = createConstant(ConstantInt::get(Int64, Size));293  Ops[2] = Id;294  for (unsigned I = 0, E = Fields.size(); I != E; ++I) {295    Ops[I * 3 + 3] = Fields[I].Type;296    Ops[I * 3 + 4] = createConstant(ConstantInt::get(Int64, Fields[I].Offset));297    Ops[I * 3 + 5] = createConstant(ConstantInt::get(Int64, Fields[I].Size));298  }299  return MDNode::get(Context, Ops);300}301 302MDNode *MDBuilder::createTBAAAccessTag(MDNode *BaseType, MDNode *AccessType,303                                       uint64_t Offset, uint64_t Size,304                                       bool IsImmutable) {305  IntegerType *Int64 = Type::getInt64Ty(Context);306  auto *OffsetNode = createConstant(ConstantInt::get(Int64, Offset));307  auto *SizeNode = createConstant(ConstantInt::get(Int64, Size));308  if (IsImmutable) {309    auto *ImmutabilityFlagNode = createConstant(ConstantInt::get(Int64, 1));310    return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode,311                                 ImmutabilityFlagNode});312  }313  return MDNode::get(Context, {BaseType, AccessType, OffsetNode, SizeNode});314}315 316MDNode *MDBuilder::createMutableTBAAAccessTag(MDNode *Tag) {317  MDNode *BaseType = cast<MDNode>(Tag->getOperand(0));318  MDNode *AccessType = cast<MDNode>(Tag->getOperand(1));319  Metadata *OffsetNode = Tag->getOperand(2);320  uint64_t Offset = mdconst::extract<ConstantInt>(OffsetNode)->getZExtValue();321 322  bool NewFormat = isa<MDNode>(AccessType->getOperand(0));323 324  // See if the tag is already mutable.325  unsigned ImmutabilityFlagOp = NewFormat ? 4 : 3;326  if (Tag->getNumOperands() <= ImmutabilityFlagOp)327    return Tag;328 329  // If Tag is already mutable then return it.330  Metadata *ImmutabilityFlagNode = Tag->getOperand(ImmutabilityFlagOp);331  if (!mdconst::extract<ConstantInt>(ImmutabilityFlagNode)->getValue())332    return Tag;333 334  // Otherwise, create another node.335  if (!NewFormat)336    return createTBAAStructTagNode(BaseType, AccessType, Offset);337 338  Metadata *SizeNode = Tag->getOperand(3);339  uint64_t Size = mdconst::extract<ConstantInt>(SizeNode)->getZExtValue();340  return createTBAAAccessTag(BaseType, AccessType, Offset, Size);341}342 343MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) {344  Metadata *Vals[] = {345      createString("loop_header_weight"),346      createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)),347  };348  return MDNode::get(Context, Vals);349}350 351MDNode *MDBuilder::createPseudoProbeDesc(uint64_t GUID, uint64_t Hash,352                                         StringRef FName) {353  auto *Int64Ty = Type::getInt64Ty(Context);354  SmallVector<Metadata *, 3> Ops(3);355  Ops[0] = createConstant(ConstantInt::get(Int64Ty, GUID));356  Ops[1] = createConstant(ConstantInt::get(Int64Ty, Hash));357  Ops[2] = createString(FName);358  return MDNode::get(Context, Ops);359}360 361MDNode *362MDBuilder::createLLVMStats(ArrayRef<std::pair<StringRef, uint64_t>> LLVMStats) {363  auto *Int64Ty = Type::getInt64Ty(Context);364  SmallVector<Metadata *, 4> Ops(LLVMStats.size() * 2);365  for (size_t I = 0; I < LLVMStats.size(); I++) {366    Ops[I * 2] = createString(LLVMStats[I].first);367    Ops[I * 2 + 1] =368        createConstant(ConstantInt::get(Int64Ty, LLVMStats[I].second));369  }370  return MDNode::get(Context, Ops);371}372