diff options
Diffstat (limited to 'contrib/llvm/lib/IR')
49 files changed, 5094 insertions, 3639 deletions
diff --git a/contrib/llvm/lib/IR/AsmWriter.cpp b/contrib/llvm/lib/IR/AsmWriter.cpp index a7499bc..c494d6c 100644 --- a/contrib/llvm/lib/IR/AsmWriter.cpp +++ b/contrib/llvm/lib/IR/AsmWriter.cpp @@ -49,6 +49,218 @@ AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {} // Helper Functions //===----------------------------------------------------------------------===// +namespace { +struct OrderMap { + DenseMap<const Value *, std::pair<unsigned, bool>> IDs; + + unsigned size() const { return IDs.size(); } + std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; } + std::pair<unsigned, bool> lookup(const Value *V) const { + return IDs.lookup(V); + } + void index(const Value *V) { + // Explicitly sequence get-size and insert-value operations to avoid UB. + unsigned ID = IDs.size() + 1; + IDs[V].first = ID; + } +}; +} + +static void orderValue(const Value *V, OrderMap &OM) { + if (OM.lookup(V).first) + return; + + if (const Constant *C = dyn_cast<Constant>(V)) + if (C->getNumOperands() && !isa<GlobalValue>(C)) + for (const Value *Op : C->operands()) + if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op)) + orderValue(Op, OM); + + // Note: we cannot cache this lookup above, since inserting into the map + // changes the map's size, and thus affects the other IDs. + OM.index(V); +} + +static OrderMap orderModule(const Module *M) { + // This needs to match the order used by ValueEnumerator::ValueEnumerator() + // and ValueEnumerator::incorporateFunction(). + OrderMap OM; + + for (const GlobalVariable &G : M->globals()) { + if (G.hasInitializer()) + if (!isa<GlobalValue>(G.getInitializer())) + orderValue(G.getInitializer(), OM); + orderValue(&G, OM); + } + for (const GlobalAlias &A : M->aliases()) { + if (!isa<GlobalValue>(A.getAliasee())) + orderValue(A.getAliasee(), OM); + orderValue(&A, OM); + } + for (const Function &F : *M) { + if (F.hasPrefixData()) + if (!isa<GlobalValue>(F.getPrefixData())) + orderValue(F.getPrefixData(), OM); + + if (F.hasPrologueData()) + if (!isa<GlobalValue>(F.getPrologueData())) + orderValue(F.getPrologueData(), OM); + + orderValue(&F, OM); + + if (F.isDeclaration()) + continue; + + for (const Argument &A : F.args()) + orderValue(&A, OM); + for (const BasicBlock &BB : F) { + orderValue(&BB, OM); + for (const Instruction &I : BB) { + for (const Value *Op : I.operands()) + if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) || + isa<InlineAsm>(*Op)) + orderValue(Op, OM); + orderValue(&I, OM); + } + } + } + return OM; +} + +static void predictValueUseListOrderImpl(const Value *V, const Function *F, + unsigned ID, const OrderMap &OM, + UseListOrderStack &Stack) { + // Predict use-list order for this one. + typedef std::pair<const Use *, unsigned> Entry; + SmallVector<Entry, 64> List; + for (const Use &U : V->uses()) + // Check if this user will be serialized. + if (OM.lookup(U.getUser()).first) + List.push_back(std::make_pair(&U, List.size())); + + if (List.size() < 2) + // We may have lost some users. + return; + + bool GetsReversed = + !isa<GlobalVariable>(V) && !isa<Function>(V) && !isa<BasicBlock>(V); + if (auto *BA = dyn_cast<BlockAddress>(V)) + ID = OM.lookup(BA->getBasicBlock()).first; + std::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) { + const Use *LU = L.first; + const Use *RU = R.first; + if (LU == RU) + return false; + + auto LID = OM.lookup(LU->getUser()).first; + auto RID = OM.lookup(RU->getUser()).first; + + // If ID is 4, then expect: 7 6 5 1 2 3. + if (LID < RID) { + if (GetsReversed) + if (RID <= ID) + return true; + return false; + } + if (RID < LID) { + if (GetsReversed) + if (LID <= ID) + return false; + return true; + } + + // LID and RID are equal, so we have different operands of the same user. + // Assume operands are added in order for all instructions. + if (GetsReversed) + if (LID <= ID) + return LU->getOperandNo() < RU->getOperandNo(); + return LU->getOperandNo() > RU->getOperandNo(); + }); + + if (std::is_sorted( + List.begin(), List.end(), + [](const Entry &L, const Entry &R) { return L.second < R.second; })) + // Order is already correct. + return; + + // Store the shuffle. + Stack.emplace_back(V, F, List.size()); + assert(List.size() == Stack.back().Shuffle.size() && "Wrong size"); + for (size_t I = 0, E = List.size(); I != E; ++I) + Stack.back().Shuffle[I] = List[I].second; +} + +static void predictValueUseListOrder(const Value *V, const Function *F, + OrderMap &OM, UseListOrderStack &Stack) { + auto &IDPair = OM[V]; + assert(IDPair.first && "Unmapped value"); + if (IDPair.second) + // Already predicted. + return; + + // Do the actual prediction. + IDPair.second = true; + if (!V->use_empty() && std::next(V->use_begin()) != V->use_end()) + predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack); + + // Recursive descent into constants. + if (const Constant *C = dyn_cast<Constant>(V)) + if (C->getNumOperands()) // Visit GlobalValues. + for (const Value *Op : C->operands()) + if (isa<Constant>(Op)) // Visit GlobalValues. + predictValueUseListOrder(Op, F, OM, Stack); +} + +static UseListOrderStack predictUseListOrder(const Module *M) { + OrderMap OM = orderModule(M); + + // Use-list orders need to be serialized after all the users have been added + // to a value, or else the shuffles will be incomplete. Store them per + // function in a stack. + // + // Aside from function order, the order of values doesn't matter much here. + UseListOrderStack Stack; + + // We want to visit the functions backward now so we can list function-local + // constants in the last Function they're used in. Module-level constants + // have already been visited above. + for (auto I = M->rbegin(), E = M->rend(); I != E; ++I) { + const Function &F = *I; + if (F.isDeclaration()) + continue; + for (const BasicBlock &BB : F) + predictValueUseListOrder(&BB, &F, OM, Stack); + for (const Argument &A : F.args()) + predictValueUseListOrder(&A, &F, OM, Stack); + for (const BasicBlock &BB : F) + for (const Instruction &I : BB) + for (const Value *Op : I.operands()) + if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues. + predictValueUseListOrder(Op, &F, OM, Stack); + for (const BasicBlock &BB : F) + for (const Instruction &I : BB) + predictValueUseListOrder(&I, &F, OM, Stack); + } + + // Visit globals last. + for (const GlobalVariable &G : M->globals()) + predictValueUseListOrder(&G, nullptr, OM, Stack); + for (const Function &F : *M) + predictValueUseListOrder(&F, nullptr, OM, Stack); + for (const GlobalAlias &A : M->aliases()) + predictValueUseListOrder(&A, nullptr, OM, Stack); + for (const GlobalVariable &G : M->globals()) + if (G.hasInitializer()) + predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack); + for (const GlobalAlias &A : M->aliases()) + predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack); + for (const Function &F : *M) + if (F.hasPrefixData()) + predictValueUseListOrder(F.getPrefixData(), nullptr, OM, Stack); + + return Stack; +} + static const Module *getModuleFromVal(const Value *V) { if (const Argument *MA = dyn_cast<Argument>(V)) return MA->getParent() ? MA->getParent()->getParent() : nullptr; @@ -75,9 +287,11 @@ static void PrintCallingConv(unsigned cc, raw_ostream &Out) { case CallingConv::AnyReg: Out << "anyregcc"; break; case CallingConv::PreserveMost: Out << "preserve_mostcc"; break; case CallingConv::PreserveAll: Out << "preserve_allcc"; break; + case CallingConv::GHC: Out << "ghccc"; break; case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break; case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break; case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break; + case CallingConv::X86_VectorCall:Out << "x86_vectorcallcc"; break; case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break; case CallingConv::ARM_APCS: Out << "arm_apcscc"; break; case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break; @@ -347,6 +561,8 @@ public: FunctionProcessed = false; } + const Function *getFunction() const { return TheFunction; } + /// After calling incorporateFunction, use this method to remove the /// most recently incorporated function from the SlotTracker. This /// will reset the state of the machine back to just the module contents. @@ -418,13 +634,6 @@ static SlotTracker *createSlotTracker(const Value *V) { if (const Function *Func = dyn_cast<Function>(V)) return new SlotTracker(Func); - if (const MDNode *MD = dyn_cast<MDNode>(V)) { - if (!MD->isFunctionLocal()) - return new SlotTracker(MD->getFunction()); - - return new SlotTracker((Function *)nullptr); - } - return nullptr; } @@ -437,16 +646,14 @@ static SlotTracker *createSlotTracker(const Value *V) { // Module level constructor. Causes the contents of the Module (sans functions) // to be added to the slot table. SlotTracker::SlotTracker(const Module *M) - : TheModule(M), TheFunction(nullptr), FunctionProcessed(false), - mNext(0), fNext(0), mdnNext(0), asNext(0) { -} + : TheModule(M), TheFunction(nullptr), FunctionProcessed(false), mNext(0), + fNext(0), mdnNext(0), asNext(0) {} // Function level constructor. Causes the contents of the Module and the one // function provided to be added to the slot table. SlotTracker::SlotTracker(const Function *F) - : TheModule(F ? F->getParent() : nullptr), TheFunction(F), - FunctionProcessed(false), mNext(0), fNext(0), mdnNext(0), asNext(0) { -} + : TheModule(F ? F->getParent() : nullptr), TheFunction(F), + FunctionProcessed(false), mNext(0), fNext(0), mdnNext(0), asNext(0) {} inline void SlotTracker::initialize() { if (TheModule) { @@ -508,7 +715,7 @@ void SlotTracker::processFunction() { ST_DEBUG("Inserting Instructions:\n"); - SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst; + SmallVector<std::pair<unsigned, MDNode *>, 4> MDForInst; // Add all of the basic blocks and instructions with no names. for (Function::const_iterator BB = TheFunction->begin(), @@ -528,8 +735,9 @@ void SlotTracker::processFunction() { if (Function *F = CI->getCalledFunction()) if (F->isIntrinsic()) for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) - if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i))) - CreateMetadataSlot(N); + if (auto *V = dyn_cast_or_null<MetadataAsValue>(I->getOperand(i))) + if (MDNode *N = dyn_cast<MDNode>(V->getMetadata())) + CreateMetadataSlot(N); // Add all the call attributes to the table. AttributeSet Attrs = CI->getAttributes().getFnAttributes(); @@ -640,16 +848,10 @@ void SlotTracker::CreateFunctionSlot(const Value *V) { void SlotTracker::CreateMetadataSlot(const MDNode *N) { assert(N && "Can't insert a null Value into SlotTracker!"); - // Don't insert if N is a function-local metadata, these are always printed - // inline. - if (!N->isFunctionLocal()) { - mdn_iterator I = mdnMap.find(N); - if (I != mdnMap.end()) - return; - - unsigned DestSlot = mdnNext++; - mdnMap[N] = DestSlot; - } + unsigned DestSlot = mdnNext; + if (!mdnMap.insert(std::make_pair(N, DestSlot)).second) + return; + ++mdnNext; // Recursively add any MDNodes referenced by operands. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) @@ -678,6 +880,11 @@ static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, SlotTracker *Machine, const Module *Context); +static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD, + TypePrinting *TypePrinter, + SlotTracker *Machine, const Module *Context, + bool FromValue = false); + static const char *getPredicateText(unsigned predicate) { const char * pred = "unknown"; switch (predicate) { @@ -1042,20 +1249,21 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV, Out << "<placeholder or erroneous Constant>"; } -static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, - TypePrinting *TypePrinter, - SlotTracker *Machine, - const Module *Context) { +static void writeMDTuple(raw_ostream &Out, const MDTuple *Node, + TypePrinting *TypePrinter, SlotTracker *Machine, + const Module *Context) { Out << "!{"; for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) { - const Value *V = Node->getOperand(mi); - if (!V) + const Metadata *MD = Node->getOperand(mi); + if (!MD) Out << "null"; - else { + else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) { + Value *V = MDV->getValue(); TypePrinter->print(V->getType(), Out); Out << ' '; - WriteAsOperandInternal(Out, Node->getOperand(mi), - TypePrinter, Machine, Context); + WriteAsOperandInternal(Out, V, TypePrinter, Machine, Context); + } else { + WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context); } if (mi + 1 != me) Out << ", "; @@ -1064,6 +1272,60 @@ static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, Out << "}"; } +namespace { +struct FieldSeparator { + bool Skip; + FieldSeparator() : Skip(true) {} +}; +raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) { + if (FS.Skip) { + FS.Skip = false; + return OS; + } + return OS << ", "; +} +} // end namespace + +static void writeMDLocation(raw_ostream &Out, const MDLocation *DL, + TypePrinting *TypePrinter, SlotTracker *Machine, + const Module *Context) { + Out << "!MDLocation("; + FieldSeparator FS; + // Always output the line, since 0 is a relevant and important value for it. + Out << FS << "line: " << DL->getLine(); + if (DL->getColumn()) + Out << FS << "column: " << DL->getColumn(); + Out << FS << "scope: "; + WriteAsOperandInternal(Out, DL->getScope(), TypePrinter, Machine, Context); + if (DL->getInlinedAt()) { + Out << FS << "inlinedAt: "; + WriteAsOperandInternal(Out, DL->getInlinedAt(), TypePrinter, Machine, + Context); + } + Out << ")"; +} + +static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, + TypePrinting *TypePrinter, + SlotTracker *Machine, + const Module *Context) { + assert(isa<UniquableMDNode>(Node) && "Expected uniquable MDNode"); + + auto *Uniquable = cast<UniquableMDNode>(Node); + if (Uniquable->isDistinct()) + Out << "distinct "; + + switch (Uniquable->getMetadataID()) { + default: + llvm_unreachable("Expected uniquable MDNode"); +#define HANDLE_UNIQUABLE_LEAF(CLASS) \ + case Metadata::CLASS##Kind: \ + write##CLASS(Out, cast<CLASS>(Uniquable), TypePrinter, Machine, Context); \ + break; +#include "llvm/IR/Metadata.def" + } +} + // Full implementation of printing a Value as an operand with support for // TypePrinting, etc. static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, @@ -1099,31 +1361,9 @@ static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, return; } - if (const MDNode *N = dyn_cast<MDNode>(V)) { - if (N->isFunctionLocal()) { - // Print metadata inline, not via slot reference number. - WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context); - return; - } - - if (!Machine) { - if (N->isFunctionLocal()) - Machine = new SlotTracker(N->getFunction()); - else - Machine = new SlotTracker(Context); - } - int Slot = Machine->getMetadataSlot(N); - if (Slot == -1) - Out << "<badref>"; - else - Out << '!' << Slot; - return; - } - - if (const MDString *MDS = dyn_cast<MDString>(V)) { - Out << "!\""; - PrintEscapedString(MDS->getString(), Out); - Out << '"'; + if (auto *MD = dyn_cast<MetadataAsValue>(V)) { + WriteAsOperandInternal(Out, MD->getMetadata(), TypePrinter, Machine, + Context, /* FromValue */ true); return; } @@ -1166,6 +1406,40 @@ static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, Out << "<badref>"; } +static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD, + TypePrinting *TypePrinter, + SlotTracker *Machine, const Module *Context, + bool FromValue) { + if (const MDNode *N = dyn_cast<MDNode>(MD)) { + if (!Machine) + Machine = new SlotTracker(Context); + int Slot = Machine->getMetadataSlot(N); + if (Slot == -1) + // Give the pointer value instead of "badref", since this comes up all + // the time when debugging. + Out << "<" << N << ">"; + else + Out << '!' << Slot; + return; + } + + if (const MDString *MDS = dyn_cast<MDString>(MD)) { + Out << "!\""; + PrintEscapedString(MDS->getString(), Out); + Out << '"'; + return; + } + + auto *V = cast<ValueAsMetadata>(MD); + assert(TypePrinter && "TypePrinter required for metadata values"); + assert((FromValue || !isa<LocalAsMetadata>(V)) && + "Unexpected function-local metadata outside of value argument"); + + TypePrinter->print(V->getValue()->getType(), Out); + Out << ' '; + WriteAsOperandInternal(Out, V->getValue(), TypePrinter, Machine, Context); +} + void AssemblyWriter::init() { if (!TheModule) return; @@ -1279,6 +1553,9 @@ void AssemblyWriter::writeParamOperand(const Value *Operand, void AssemblyWriter::printModule(const Module *M) { Machine.initialize(); + if (shouldPreserveAssemblyUseListOrder()) + UseListOrders = predictUseListOrder(M); + if (!M->getModuleIdentifier().empty() && // Don't print the ID if it will start a new line (which would // require a comment char before it). @@ -1339,9 +1616,13 @@ void AssemblyWriter::printModule(const Module *M) { I != E; ++I) printAlias(I); + // Output global use-lists. + printUseLists(nullptr); + // Output all of the functions. for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) printFunction(I); + assert(UseListOrders.empty() && "All use-lists should have been consumed"); // Output all attribute groups. if (!Machine.as_empty()) { @@ -1455,6 +1736,24 @@ static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM, } } +static void maybePrintComdat(formatted_raw_ostream &Out, + const GlobalObject &GO) { + const Comdat *C = GO.getComdat(); + if (!C) + return; + + if (isa<GlobalVariable>(GO)) + Out << ','; + Out << " comdat"; + + if (GO.getName() == C->getName()) + return; + + Out << '('; + PrintLLVMName(Out, C->getName(), ComdatPrefix); + Out << ')'; +} + void AssemblyWriter::printGlobal(const GlobalVariable *GV) { if (GV->isMaterializable()) Out << "; Materializable\n"; @@ -1488,10 +1787,7 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) { PrintEscapedString(GV->getSection(), Out); Out << '"'; } - if (GV->hasComdat()) { - Out << ", comdat "; - PrintLLVMName(Out, GV->getComdat()->getName(), ComdatPrefix); - } + maybePrintComdat(Out, *GV); if (GV->getAlignment()) Out << ", align " << GV->getAlignment(); @@ -1509,6 +1805,7 @@ void AssemblyWriter::printAlias(const GlobalAlias *GA) { PrintLLVMName(Out, GA); Out << " = "; } + PrintLinkage(GA->getLinkage(), Out); PrintVisibility(GA->getVisibility(), Out); PrintDLLStorageClass(GA->getDLLStorageClass(), Out); PrintThreadLocalModel(GA->getThreadLocalMode(), Out); @@ -1517,8 +1814,6 @@ void AssemblyWriter::printAlias(const GlobalAlias *GA) { Out << "alias "; - PrintLinkage(GA->getLinkage(), Out); - const Constant *Aliasee = GA->getAliasee(); if (!Aliasee) { @@ -1673,10 +1968,7 @@ void AssemblyWriter::printFunction(const Function *F) { PrintEscapedString(F->getSection(), Out); Out << '"'; } - if (F->hasComdat()) { - Out << " comdat "; - PrintLLVMName(Out, F->getComdat()->getName(), ComdatPrefix); - } + maybePrintComdat(Out, *F); if (F->getAlignment()) Out << " align " << F->getAlignment(); if (F->hasGC()) @@ -1685,6 +1977,11 @@ void AssemblyWriter::printFunction(const Function *F) { Out << " prefix "; writeOperand(F->getPrefixData(), true); } + if (F->hasPrologueData()) { + Out << " prologue "; + writeOperand(F->getPrologueData(), true); + } + if (F->isDeclaration()) { Out << '\n'; } else { @@ -1693,6 +1990,9 @@ void AssemblyWriter::printFunction(const Function *F) { for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I) printBasicBlock(I); + // Output the function's use-lists. + printUseLists(F); + Out << "}\n"; } @@ -1956,6 +2256,14 @@ void AssemblyWriter::printInstruction(const Instruction &I) { Out << ", "; writeParamOperand(CI->getArgOperand(op), PAL, op + 1); } + + // Emit an ellipsis if this is a musttail call in a vararg function. This + // is only to aid readability, musttail calls forward varargs by default. + if (CI->isMustTailCall() && CI->getParent() && + CI->getParent()->getParent() && + CI->getParent()->getParent()->isVarArg()) + Out << ", ..."; + Out << ')'; if (PAL.hasAttributes(AttributeSet::FunctionIndex)) Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes()); @@ -2088,7 +2396,7 @@ void AssemblyWriter::printInstruction(const Instruction &I) { } // Print Metadata info. - SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD; + SmallVector<std::pair<unsigned, MDNode *>, 4> InstMD; I.getAllMetadata(InstMD); if (!InstMD.empty()) { SmallVector<StringRef, 8> MDNames; @@ -2113,8 +2421,8 @@ static void WriteMDNodeComment(const MDNode *Node, if (Node->getNumOperands() < 1) return; - Value *Op = Node->getOperand(0); - if (!Op || !isa<ConstantInt>(Op) || cast<ConstantInt>(Op)->getBitWidth() < 32) + Metadata *Op = Node->getOperand(0); + if (!Op || !isa<MDString>(Op)) return; DIDescriptor Desc(Node); @@ -2132,7 +2440,7 @@ static void WriteMDNodeComment(const MDNode *Node, } void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) { - Out << '!' << Slot << " = metadata "; + Out << '!' << Slot << " = "; printMDNodeBody(Node); } @@ -2170,6 +2478,45 @@ void AssemblyWriter::writeAllAttributeGroups() { } // namespace llvm +void AssemblyWriter::printUseListOrder(const UseListOrder &Order) { + bool IsInFunction = Machine.getFunction(); + if (IsInFunction) + Out << " "; + + Out << "uselistorder"; + if (const BasicBlock *BB = + IsInFunction ? nullptr : dyn_cast<BasicBlock>(Order.V)) { + Out << "_bb "; + writeOperand(BB->getParent(), false); + Out << ", "; + writeOperand(BB, false); + } else { + Out << " "; + writeOperand(Order.V, true); + } + Out << ", { "; + + assert(Order.Shuffle.size() >= 2 && "Shuffle too small"); + Out << Order.Shuffle[0]; + for (unsigned I = 1, E = Order.Shuffle.size(); I != E; ++I) + Out << ", " << Order.Shuffle[I]; + Out << " }\n"; +} + +void AssemblyWriter::printUseLists(const Function *F) { + auto hasMore = + [&]() { return !UseListOrders.empty() && UseListOrders.back().F == F; }; + if (!hasMore()) + // Nothing to do. + return; + + Out << "\n; uselistorder directives\n"; + while (hasMore()) { + printUseListOrder(UseListOrders.back()); + UseListOrders.pop_back(); + } +} + //===----------------------------------------------------------------------===// // External Interface declarations //===----------------------------------------------------------------------===// @@ -2245,18 +2592,14 @@ void Value::print(raw_ostream &ROS) const { W.printFunction(F); else W.printAlias(cast<GlobalAlias>(GV)); - } else if (const MDNode *N = dyn_cast<MDNode>(this)) { - const Function *F = N->getFunction(); - SlotTracker SlotTable(F); - AssemblyWriter W(OS, SlotTable, F ? F->getParent() : nullptr, nullptr); - W.printMDNodeBody(N); + } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) { + V->getMetadata()->print(ROS); } else if (const Constant *C = dyn_cast<Constant>(this)) { TypePrinting TypePrinter; TypePrinter.print(C->getType(), OS); OS << ' '; WriteConstantInternal(OS, C, TypePrinter, nullptr, nullptr); - } else if (isa<InlineAsm>(this) || isa<MDString>(this) || - isa<Argument>(this)) { + } else if (isa<InlineAsm>(this) || isa<Argument>(this)) { this->printAsOperand(OS); } else { llvm_unreachable("Unknown value to print out!"); @@ -2266,9 +2609,8 @@ void Value::print(raw_ostream &ROS) const { void Value::printAsOperand(raw_ostream &O, bool PrintType, const Module *M) const { // Fast path: Don't construct and populate a TypePrinting object if we // won't be needing any types printed. - if (!PrintType && - ((!isa<Constant>(this) && !isa<MDNode>(this)) || - hasName() || isa<GlobalValue>(this))) { + if (!PrintType && ((!isa<Constant>(this) && !isa<MetadataAsValue>(this)) || + hasName() || isa<GlobalValue>(this))) { WriteAsOperandInternal(O, this, nullptr, nullptr, M); return; } @@ -2287,11 +2629,37 @@ void Value::printAsOperand(raw_ostream &O, bool PrintType, const Module *M) cons WriteAsOperandInternal(O, this, &TypePrinter, nullptr, M); } +void Metadata::print(raw_ostream &ROS) const { + formatted_raw_ostream OS(ROS); + if (auto *N = dyn_cast<MDNode>(this)) { + SlotTracker SlotTable(static_cast<Function *>(nullptr)); + AssemblyWriter W(OS, SlotTable, nullptr, nullptr); + W.printMDNodeBody(N); + + return; + } + printAsOperand(OS); +} + +void Metadata::printAsOperand(raw_ostream &ROS, bool PrintType, + const Module *M) const { + formatted_raw_ostream OS(ROS); + + std::unique_ptr<TypePrinting> TypePrinter; + if (PrintType) { + TypePrinter.reset(new TypePrinting); + if (M) + TypePrinter->incorporateTypes(*M); + } + WriteAsOperandInternal(OS, this, TypePrinter.get(), nullptr, M, + /* FromValue */ true); +} + // Value::dump - allow easy printing of Values from the debugger. void Value::dump() const { print(dbgs()); dbgs() << '\n'; } // Type::dump - allow easy printing of Types from the debugger. -void Type::dump() const { print(dbgs()); } +void Type::dump() const { print(dbgs()); dbgs() << '\n'; } // Module::dump() - Allow printing of Modules from the debugger. void Module::dump() const { print(dbgs(), nullptr); } @@ -2301,3 +2669,8 @@ void Comdat::dump() const { print(dbgs()); } // NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger. void NamedMDNode::dump() const { print(dbgs()); } + +void Metadata::dump() const { + print(dbgs()); + dbgs() << '\n'; +} diff --git a/contrib/llvm/lib/IR/AsmWriter.h b/contrib/llvm/lib/IR/AsmWriter.h index aef9c8a..60da5ad 100644 --- a/contrib/llvm/lib/IR/AsmWriter.h +++ b/contrib/llvm/lib/IR/AsmWriter.h @@ -12,14 +12,15 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_IR_ASSEMBLYWRITER_H -#define LLVM_IR_ASSEMBLYWRITER_H +#ifndef LLVM_LIB_IR_ASMWRITER_H +#define LLVM_LIB_IR_ASMWRITER_H #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SetVector.h" #include "llvm/IR/Attributes.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/TypeFinder.h" +#include "llvm/IR/UseListOrder.h" #include "llvm/Support/FormattedStream.h" namespace llvm { @@ -73,6 +74,7 @@ private: TypePrinting TypePrinter; AssemblyAnnotationWriter *AnnotationWriter; SetVector<const Comdat *> Comdats; + UseListOrderStack UseListOrders; public: /// Construct an AssemblyWriter with an external SlotTracker @@ -111,6 +113,9 @@ public: void printInstructionLine(const Instruction &I); void printInstruction(const Instruction &I); + void printUseListOrder(const UseListOrder &Order); + void printUseLists(const Function *F); + private: void init(); @@ -121,4 +126,4 @@ private: } // namespace llvm -#endif //LLVM_IR_ASMWRITER_H +#endif diff --git a/contrib/llvm/lib/IR/AttributeImpl.h b/contrib/llvm/lib/IR/AttributeImpl.h index cc6d557..0448dc17 100644 --- a/contrib/llvm/lib/IR/AttributeImpl.h +++ b/contrib/llvm/lib/IR/AttributeImpl.h @@ -13,8 +13,8 @@ /// //===----------------------------------------------------------------------===// -#ifndef LLVM_ATTRIBUTESIMPL_H -#define LLVM_ATTRIBUTESIMPL_H +#ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H +#define LLVM_LIB_IR_ATTRIBUTEIMPL_H #include "llvm/ADT/FoldingSet.h" #include "llvm/IR/Attributes.h" diff --git a/contrib/llvm/lib/IR/AutoUpgrade.cpp b/contrib/llvm/lib/IR/AutoUpgrade.cpp index 459bd88..e3544df 100644 --- a/contrib/llvm/lib/IR/AutoUpgrade.cpp +++ b/contrib/llvm/lib/IR/AutoUpgrade.cpp @@ -15,6 +15,7 @@ #include "llvm/IR/CFG.h" #include "llvm/IR/CallSite.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/DIBuilder.h" #include "llvm/IR/DebugInfo.h" #include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/Function.h" @@ -43,6 +44,22 @@ static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID, return true; } +// Upgrade the declarations of intrinsic functions whose 8-bit immediate mask +// arguments have changed their type from i32 to i8. +static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID, + Function *&NewFn) { + // Check that the last argument is an i32. + Type *LastArgType = F->getFunctionType()->getParamType( + F->getFunctionType()->getNumParams() - 1); + if (!LastArgType->isIntegerTy(32)) + return false; + + // Move this function aside and map down. + F->setName(F->getName() + ".old"); + NewFn = Intrinsic::getDeclaration(F->getParent(), IID); + return true; +} + static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { assert(F && "Illegal to upgrade a non-existent Function."); @@ -90,6 +107,20 @@ static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { } break; } + case 'd': { + if (Name.startswith("dbg.declare") && F->arg_size() == 2) { + F->setName(Name + ".old"); + NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::dbg_declare); + return true; + } + if (Name.startswith("dbg.value") && F->arg_size() == 3) { + F->setName(Name + ".old"); + NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::dbg_value); + return true; + } + break; + } + case 'o': // We only need to change the name to match the mangling including the // address space. @@ -130,6 +161,51 @@ static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { if (Name == "x86.sse41.ptestnzc") return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn); } + // Several blend and other instructions with maskes used the wrong number of + // bits. + if (Name == "x86.sse41.pblendw") + return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_pblendw, + NewFn); + if (Name == "x86.sse41.blendpd") + return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_blendpd, + NewFn); + if (Name == "x86.sse41.blendps") + return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_blendps, + NewFn); + if (Name == "x86.sse41.insertps") + return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps, + NewFn); + if (Name == "x86.sse41.dppd") + return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd, + NewFn); + if (Name == "x86.sse41.dpps") + return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps, + NewFn); + if (Name == "x86.sse41.mpsadbw") + return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw, + NewFn); + if (Name == "x86.avx.blend.pd.256") + return UpgradeX86IntrinsicsWith8BitMask( + F, Intrinsic::x86_avx_blend_pd_256, NewFn); + if (Name == "x86.avx.blend.ps.256") + return UpgradeX86IntrinsicsWith8BitMask( + F, Intrinsic::x86_avx_blend_ps_256, NewFn); + if (Name == "x86.avx.dp.ps.256") + return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256, + NewFn); + if (Name == "x86.avx2.pblendw") + return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_pblendw, + NewFn); + if (Name == "x86.avx2.pblendd.128") + return UpgradeX86IntrinsicsWith8BitMask( + F, Intrinsic::x86_avx2_pblendd_128, NewFn); + if (Name == "x86.avx2.pblendd.256") + return UpgradeX86IntrinsicsWith8BitMask( + F, Intrinsic::x86_avx2_pblendd_256, NewFn); + if (Name == "x86.avx2.mpsadbw") + return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw, + NewFn); + // frcz.ss/sd may need to have an argument dropped if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) { F->setName(Name + ".old"); @@ -173,66 +249,28 @@ bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { return Upgraded; } -static bool UpgradeGlobalStructors(GlobalVariable *GV) { - ArrayType *ATy = dyn_cast<ArrayType>(GV->getType()->getElementType()); - StructType *OldTy = - ATy ? dyn_cast<StructType>(ATy->getElementType()) : nullptr; - - // Only upgrade an array of a two field struct with the appropriate field - // types. - if (!OldTy || OldTy->getNumElements() != 2) - return false; - - // Get the upgraded 3 element type. - PointerType *VoidPtrTy = Type::getInt8Ty(GV->getContext())->getPointerTo(); - Type *Tys[3] = { - OldTy->getElementType(0), - OldTy->getElementType(1), - VoidPtrTy - }; - StructType *NewTy = - StructType::get(GV->getContext(), Tys, /*isPacked=*/false); - - // Build new constants with a null third field filled in. - Constant *OldInitC = GV->getInitializer(); - ConstantArray *OldInit = dyn_cast<ConstantArray>(OldInitC); - if (!OldInit && !isa<ConstantAggregateZero>(OldInitC)) - return false; - std::vector<Constant *> Initializers; - if (OldInit) { - for (Use &U : OldInit->operands()) { - ConstantStruct *Init = cast<ConstantStruct>(&U); - Constant *NewInit = - ConstantStruct::get(NewTy, Init->getOperand(0), Init->getOperand(1), - Constant::getNullValue(VoidPtrTy), nullptr); - Initializers.push_back(NewInit); - } - } - assert(Initializers.size() == ATy->getNumElements()); - - // Replace the old GV with a new one. - ATy = ArrayType::get(NewTy, Initializers.size()); - Constant *NewInit = ConstantArray::get(ATy, Initializers); - GlobalVariable *NewGV = new GlobalVariable( - *GV->getParent(), ATy, GV->isConstant(), GV->getLinkage(), NewInit, "", - GV, GV->getThreadLocalMode(), GV->getType()->getAddressSpace(), - GV->isExternallyInitialized()); - NewGV->copyAttributesFrom(GV); - NewGV->takeName(GV); - assert(GV->use_empty() && "program cannot use initializer list"); - GV->eraseFromParent(); - return true; -} - bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) { - if (GV->getName() == "llvm.global_ctors" || - GV->getName() == "llvm.global_dtors") - return UpgradeGlobalStructors(GV); - // Nothing to do yet. return false; } +static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) { + if (!DbgNode || Elt >= DbgNode->getNumOperands()) + return nullptr; + return dyn_cast_or_null<MDNode>(DbgNode->getOperand(Elt)); +} + +static MetadataAsValue *getExpression(Value *VarOperand, Function *F) { + // Old-style DIVariables have an optional expression as the 8th element. + DIExpression Expr(getNodeField( + cast<MDNode>(cast<MetadataAsValue>(VarOperand)->getMetadata()), 8)); + if (!Expr) { + DIBuilder DIB(*F->getParent(), /*AllowUnresolved*/ false); + Expr = DIB.createExpression(); + } + return MetadataAsValue::get(F->getContext(), Expr); +} + // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the // upgraded intrinsic. All argument and return casting must be provided in // order to seamlessly integrate with existing context. @@ -269,8 +307,9 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { Builder.SetInsertPoint(CI->getParent(), CI); Module *M = F->getParent(); - SmallVector<Value *, 1> Elts; - Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1)); + SmallVector<Metadata *, 1> Elts; + Elts.push_back( + ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1))); MDNode *Node = MDNode::get(C, Elts); Value *Arg0 = CI->getArgOperand(0); @@ -396,12 +435,32 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { } std::string Name = CI->getName().str(); - CI->setName(Name + ".old"); + if (!Name.empty()) + CI->setName(Name + ".old"); switch (NewFn->getIntrinsicID()) { default: llvm_unreachable("Unknown function for CallInst upgrade."); + // Upgrade debug intrinsics to use an additional DIExpression argument. + case Intrinsic::dbg_declare: { + auto NewCI = + Builder.CreateCall3(NewFn, CI->getArgOperand(0), CI->getArgOperand(1), + getExpression(CI->getArgOperand(1), F), Name); + NewCI->setDebugLoc(CI->getDebugLoc()); + CI->replaceAllUsesWith(NewCI); + CI->eraseFromParent(); + return; + } + case Intrinsic::dbg_value: { + auto NewCI = Builder.CreateCall4( + NewFn, CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), + getExpression(CI->getArgOperand(2), F), Name); + NewCI->setDebugLoc(CI->getDebugLoc()); + CI->replaceAllUsesWith(NewCI); + CI->eraseFromParent(); + return; + } case Intrinsic::ctlz: case Intrinsic::cttz: assert(CI->getNumArgOperands() == 1 && @@ -419,14 +478,6 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { CI->eraseFromParent(); return; - case Intrinsic::arm_neon_vclz: { - // Change name from llvm.arm.neon.vclz.* to llvm.ctlz.* - CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0), - Builder.getFalse(), - "llvm.ctlz." + Name.substr(14))); - CI->eraseFromParent(); - return; - } case Intrinsic::ctpop: { CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0))); CI->eraseFromParent(); @@ -468,6 +519,34 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { CI->eraseFromParent(); return; } + + case Intrinsic::x86_sse41_pblendw: + case Intrinsic::x86_sse41_blendpd: + case Intrinsic::x86_sse41_blendps: + case Intrinsic::x86_sse41_insertps: + case Intrinsic::x86_sse41_dppd: + case Intrinsic::x86_sse41_dpps: + case Intrinsic::x86_sse41_mpsadbw: + case Intrinsic::x86_avx_blend_pd_256: + case Intrinsic::x86_avx_blend_ps_256: + case Intrinsic::x86_avx_dp_ps_256: + case Intrinsic::x86_avx2_pblendw: + case Intrinsic::x86_avx2_pblendd_128: + case Intrinsic::x86_avx2_pblendd_256: + case Intrinsic::x86_avx2_mpsadbw: { + // Need to truncate the last argument from i32 to i8 -- this argument models + // an inherently 8-bit immediate operand to these x86 instructions. + SmallVector<Value *, 4> Args(CI->arg_operands().begin(), + CI->arg_operands().end()); + + // Replace the last argument with a trunc. + Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc"); + + CallInst *NewCall = Builder.CreateCall(NewFn, Args); + CI->replaceAllUsesWith(NewCall); + CI->eraseFromParent(); + return; + } } } @@ -501,22 +580,18 @@ void llvm::UpgradeInstWithTBAATag(Instruction *I) { return; if (MD->getNumOperands() == 3) { - Value *Elts[] = { - MD->getOperand(0), - MD->getOperand(1) - }; + Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)}; MDNode *ScalarType = MDNode::get(I->getContext(), Elts); // Create a MDNode <ScalarType, ScalarType, offset 0, const> - Value *Elts2[] = { - ScalarType, ScalarType, - Constant::getNullValue(Type::getInt64Ty(I->getContext())), - MD->getOperand(2) - }; + Metadata *Elts2[] = {ScalarType, ScalarType, + ConstantAsMetadata::get(Constant::getNullValue( + Type::getInt64Ty(I->getContext()))), + MD->getOperand(2)}; I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2)); } else { // Create a MDNode <MD, MD, offset 0> - Value *Elts[] = {MD, MD, - Constant::getNullValue(Type::getInt64Ty(I->getContext()))}; + Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue( + Type::getInt64Ty(I->getContext())))}; I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts)); } } diff --git a/contrib/llvm/lib/IR/BasicBlock.cpp b/contrib/llvm/lib/IR/BasicBlock.cpp index ba07433..98a3062 100644 --- a/contrib/llvm/lib/IR/BasicBlock.cpp +++ b/contrib/llvm/lib/IR/BasicBlock.cpp @@ -19,7 +19,6 @@ #include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/LeakDetector.h" #include "llvm/IR/Type.h" #include <algorithm> using namespace llvm; @@ -47,20 +46,24 @@ BasicBlock::BasicBlock(LLVMContext &C, const Twine &Name, Function *NewParent, BasicBlock *InsertBefore) : Value(Type::getLabelTy(C), Value::BasicBlockVal), Parent(nullptr) { - // Make sure that we get added to a function - LeakDetector::addGarbageObject(this); - - if (InsertBefore) { - assert(NewParent && + if (NewParent) + insertInto(NewParent, InsertBefore); + else + assert(!InsertBefore && "Cannot insert block before another block with no function!"); - NewParent->getBasicBlockList().insert(InsertBefore, this); - } else if (NewParent) { - NewParent->getBasicBlockList().push_back(this); - } setName(Name); } +void BasicBlock::insertInto(Function *NewParent, BasicBlock *InsertBefore) { + assert(NewParent && "Expected a parent"); + assert(!Parent && "Already has a parent"); + + if (InsertBefore) + NewParent->getBasicBlockList().insert(InsertBefore, this); + else + NewParent->getBasicBlockList().push_back(this); +} BasicBlock::~BasicBlock() { // If the address of the block is taken and it is being deleted (e.g. because @@ -87,14 +90,8 @@ BasicBlock::~BasicBlock() { } void BasicBlock::setParent(Function *parent) { - if (getParent()) - LeakDetector::addGarbageObject(this); - // Set Parent=parent, updating instruction symtab entries as appropriate. InstList.setSymTabObject(&Parent, parent); - - if (getParent()) - LeakDetector::removeGarbageObject(this); } void BasicBlock::removeFromParent() { @@ -131,6 +128,37 @@ const TerminatorInst *BasicBlock::getTerminator() const { return dyn_cast<TerminatorInst>(&InstList.back()); } +CallInst *BasicBlock::getTerminatingMustTailCall() { + if (InstList.empty()) + return nullptr; + ReturnInst *RI = dyn_cast<ReturnInst>(&InstList.back()); + if (!RI || RI == &InstList.front()) + return nullptr; + + Instruction *Prev = RI->getPrevNode(); + if (!Prev) + return nullptr; + + if (Value *RV = RI->getReturnValue()) { + if (RV != Prev) + return nullptr; + + // Look through the optional bitcast. + if (auto *BI = dyn_cast<BitCastInst>(Prev)) { + RV = BI->getOperand(0); + Prev = BI->getPrevNode(); + if (!Prev || RV != Prev) + return nullptr; + } + } + + if (auto *CI = dyn_cast<CallInst>(Prev)) { + if (CI->isMustTailCall()) + return CI; + } + return nullptr; +} + Instruction* BasicBlock::getFirstNonPHI() { BasicBlock::iterator i = begin(); // All valid basic blocks should have a terminator, diff --git a/contrib/llvm/lib/IR/ConstantFold.cpp b/contrib/llvm/lib/IR/ConstantFold.cpp index 395ac39..9176bf2 100644 --- a/contrib/llvm/lib/IR/ConstantFold.cpp +++ b/contrib/llvm/lib/IR/ConstantFold.cpp @@ -27,12 +27,14 @@ #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Operator.h" +#include "llvm/IR/PatternMatch.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MathExtras.h" #include <limits> using namespace llvm; +using namespace llvm::PatternMatch; //===----------------------------------------------------------------------===// // ConstantFold*Instruction Implementations @@ -593,8 +595,13 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, bool ignored; uint64_t x[2]; uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth(); - (void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI, - APFloat::rmTowardZero, &ignored); + if (APFloat::opInvalidOp == + V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI, + APFloat::rmTowardZero, &ignored)) { + // Undefined behavior invoked - the destination type can't represent + // the input constant. + return UndefValue::get(DestTy); + } APInt Val(DestBitWidth, x); return ConstantInt::get(FPC->getContext(), Val); } @@ -653,9 +660,13 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, APInt api = CI->getValue(); APFloat apf(DestTy->getFltSemantics(), APInt::getNullValue(DestTy->getPrimitiveSizeInBits())); - (void)apf.convertFromAPInt(api, - opc==Instruction::SIToFP, - APFloat::rmNearestTiesToEven); + if (APFloat::opOverflow & + apf.convertFromAPInt(api, opc==Instruction::SIToFP, + APFloat::rmNearestTiesToEven)) { + // Undefined behavior invoked - the destination type can't represent + // the input constant. + return UndefValue::get(DestTy); + } return ConstantFP::get(V->getContext(), apf); } return nullptr; @@ -674,6 +685,9 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, } return nullptr; case Instruction::Trunc: { + if (V->getType()->isVectorTy()) + return nullptr; + uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth(); if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { return ConstantInt::get(V->getContext(), @@ -901,49 +915,70 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, return C1; return Constant::getNullValue(C1->getType()); // undef & X -> 0 case Instruction::Mul: { - ConstantInt *CI; - // X * undef -> undef if X is odd or undef - if (((CI = dyn_cast<ConstantInt>(C1)) && CI->getValue()[0]) || - ((CI = dyn_cast<ConstantInt>(C2)) && CI->getValue()[0]) || - (isa<UndefValue>(C1) && isa<UndefValue>(C2))) - return UndefValue::get(C1->getType()); + // undef * undef -> undef + if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) + return C1; + const APInt *CV; + // X * undef -> undef if X is odd + if (match(C1, m_APInt(CV)) || match(C2, m_APInt(CV))) + if ((*CV)[0]) + return UndefValue::get(C1->getType()); // X * undef -> 0 otherwise return Constant::getNullValue(C1->getType()); } - case Instruction::UDiv: case Instruction::SDiv: + case Instruction::UDiv: + // X / undef -> undef + if (match(C1, m_Zero())) + return C2; + // undef / 0 -> undef // undef / 1 -> undef - if (Opcode == Instruction::UDiv || Opcode == Instruction::SDiv) - if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) - if (CI2->isOne()) - return C1; - // FALL THROUGH + if (match(C2, m_Zero()) || match(C2, m_One())) + return C1; + // undef / X -> 0 otherwise + return Constant::getNullValue(C1->getType()); case Instruction::URem: case Instruction::SRem: - if (!isa<UndefValue>(C2)) // undef / X -> 0 - return Constant::getNullValue(C1->getType()); - return C2; // X / undef -> undef + // X % undef -> undef + if (match(C2, m_Undef())) + return C2; + // undef % 0 -> undef + if (match(C2, m_Zero())) + return C1; + // undef % X -> 0 otherwise + return Constant::getNullValue(C1->getType()); case Instruction::Or: // X | undef -> -1 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef return C1; return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0 case Instruction::LShr: - if (isa<UndefValue>(C2) && isa<UndefValue>(C1)) - return C1; // undef lshr undef -> undef - return Constant::getNullValue(C1->getType()); // X lshr undef -> 0 - // undef lshr X -> 0 + // X >>l undef -> undef + if (isa<UndefValue>(C2)) + return C2; + // undef >>l 0 -> undef + if (match(C2, m_Zero())) + return C1; + // undef >>l X -> 0 + return Constant::getNullValue(C1->getType()); case Instruction::AShr: - if (!isa<UndefValue>(C2)) // undef ashr X --> all ones - return Constant::getAllOnesValue(C1->getType()); - else if (isa<UndefValue>(C1)) - return C1; // undef ashr undef -> undef - else - return C1; // X ashr undef --> X + // X >>a undef -> undef + if (isa<UndefValue>(C2)) + return C2; + // undef >>a 0 -> undef + if (match(C2, m_Zero())) + return C1; + // TODO: undef >>a X -> undef if the shift is exact + // undef >>a X -> 0 + return Constant::getNullValue(C1->getType()); case Instruction::Shl: - if (isa<UndefValue>(C2) && isa<UndefValue>(C1)) - return C1; // undef shl undef -> undef - // undef << X -> 0 or X << undef -> 0 + // X << undef -> undef + if (isa<UndefValue>(C2)) + return C2; + // undef << 0 -> undef + if (match(C2, m_Zero())) + return C1; + // undef << X -> 0 return Constant::getNullValue(C1->getType()); } } @@ -1336,9 +1371,24 @@ static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) { static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1, const GlobalValue *GV2) { + auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) { + if (GV->hasExternalWeakLinkage() || GV->hasWeakAnyLinkage()) + return true; + if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) { + Type *Ty = GVar->getType()->getPointerElementType(); + // A global with opaque type might end up being zero sized. + if (!Ty->isSized()) + return true; + // A global with an empty type might lie at the address of any other + // global. + if (Ty->isEmptyTy()) + return true; + } + return false; + }; // Don't try to decide equality of aliases. if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2)) - if (!GV1->hasExternalWeakLinkage() || !GV2->hasExternalWeakLinkage()) + if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2)) return ICmpInst::ICMP_NE; return ICmpInst::BAD_ICMP_PREDICATE; } @@ -2144,9 +2194,10 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, // If all indices are known integers and normalized, we can do a simple // check for the "inbounds" property. - if (!Unknown && !inBounds && - isa<GlobalVariable>(C) && isInBoundsIndices(Idxs)) - return ConstantExpr::getInBoundsGetElementPtr(C, Idxs); + if (!Unknown && !inBounds) + if (auto *GV = dyn_cast<GlobalVariable>(C)) + if (!GV->hasExternalWeakLinkage() && isInBoundsIndices(Idxs)) + return ConstantExpr::getInBoundsGetElementPtr(C, Idxs); return nullptr; } diff --git a/contrib/llvm/lib/IR/ConstantFold.h b/contrib/llvm/lib/IR/ConstantFold.h index e12f27a..a516abe 100644 --- a/contrib/llvm/lib/IR/ConstantFold.h +++ b/contrib/llvm/lib/IR/ConstantFold.h @@ -16,8 +16,8 @@ // //===----------------------------------------------------------------------===// -#ifndef CONSTANTFOLDING_H -#define CONSTANTFOLDING_H +#ifndef LLVM_LIB_IR_CONSTANTFOLD_H +#define LLVM_LIB_IR_CONSTANTFOLD_H #include "llvm/ADT/ArrayRef.h" diff --git a/contrib/llvm/lib/IR/Constants.cpp b/contrib/llvm/lib/IR/Constants.cpp index b815936a..1d2602a 100644 --- a/contrib/llvm/lib/IR/Constants.cpp +++ b/contrib/llvm/lib/IR/Constants.cpp @@ -107,6 +107,28 @@ bool Constant::isAllOnesValue() const { return false; } +bool Constant::isOneValue() const { + // Check for 1 integers + if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) + return CI->isOne(); + + // Check for FP which are bitcasted from 1 integers + if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) + return CFP->getValueAPF().bitcastToAPInt() == 1; + + // Check for constant vectors which are splats of 1 values. + if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) + if (Constant *Splat = CV->getSplatValue()) + return Splat->isOneValue(); + + // Check for constant vectors which are splats of 1 values. + if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) + if (Constant *Splat = CV->getSplatValue()) + return Splat->isOneValue(); + + return false; +} + bool Constant::isMinSignedValue() const { // Check for INT_MIN integers if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) @@ -129,6 +151,29 @@ bool Constant::isMinSignedValue() const { return false; } +bool Constant::isNotMinSignedValue() const { + // Check for INT_MIN integers + if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) + return !CI->isMinValue(/*isSigned=*/true); + + // Check for FP which are bitcasted from INT_MIN integers + if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) + return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue(); + + // Check for constant vectors which are splats of INT_MIN values. + if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) + if (Constant *Splat = CV->getSplatValue()) + return Splat->isNotMinSignedValue(); + + // Check for constant vectors which are splats of INT_MIN values. + if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) + if (Constant *Splat = CV->getSplatValue()) + return Splat->isNotMinSignedValue(); + + // It *may* contain INT_MIN, we can't tell. + return false; +} + // Constructor to create a '0' constant of arbitrary type... Constant *Constant::getNullValue(Type *Ty) { switch (Ty->getTypeID()) { @@ -261,7 +306,7 @@ void Constant::destroyConstantImpl() { } static bool canTrapImpl(const Constant *C, - SmallPtrSet<const ConstantExpr *, 4> &NonTrappingOps) { + SmallPtrSetImpl<const ConstantExpr *> &NonTrappingOps) { assert(C->getType()->isFirstClassType() && "Cannot evaluate aggregate vals!"); // The only thing that could possibly trap are constant exprs. const ConstantExpr *CE = dyn_cast<ConstantExpr>(C); @@ -271,7 +316,7 @@ static bool canTrapImpl(const Constant *C, // ConstantExpr traps if any operands can trap. for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) { if (ConstantExpr *Op = dyn_cast<ConstantExpr>(CE->getOperand(i))) { - if (NonTrappingOps.insert(Op) && canTrapImpl(Op, NonTrappingOps)) + if (NonTrappingOps.insert(Op).second && canTrapImpl(Op, NonTrappingOps)) return true; } } @@ -318,7 +363,7 @@ ConstHasGlobalValuePredicate(const Constant *C, const Constant *ConstOp = dyn_cast<Constant>(Op); if (!ConstOp) continue; - if (Visited.insert(ConstOp)) + if (Visited.insert(ConstOp).second) WorkList.push_back(ConstOp); } } @@ -509,19 +554,17 @@ Constant *ConstantInt::getFalse(Type *Ty) { ConstantInt::getFalse(Ty->getContext())); } - -// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap -// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the -// operator== and operator!= to ensure that the DenseMap doesn't attempt to -// compare APInt's of different widths, which would violate an APInt class -// invariant which generates an assertion. +// Get a ConstantInt from an APInt. ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) { - // Get the corresponding integer type for the bit width of the value. - IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); // get an existing value or the insertion position LLVMContextImpl *pImpl = Context.pImpl; - ConstantInt *&Slot = pImpl->IntConstants[DenseMapAPIntKeyInfo::KeyTy(V, ITy)]; - if (!Slot) Slot = new ConstantInt(ITy, V); + ConstantInt *&Slot = pImpl->IntConstants[V]; + if (!Slot) { + // Get the corresponding integer type for the bit width of the value. + IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); + Slot = new ConstantInt(ITy, V); + } + assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth())); return Slot; } @@ -644,7 +687,7 @@ Constant *ConstantFP::getZeroValueForNegation(Type *Ty) { ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { LLVMContextImpl* pImpl = Context.pImpl; - ConstantFP *&Slot = pImpl->FPConstants[DenseMapAPFloatKeyInfo::KeyTy(V)]; + ConstantFP *&Slot = pImpl->FPConstants[V]; if (!Slot) { Type *Ty; @@ -781,6 +824,11 @@ ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V) } Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) { + if (Constant *C = getImpl(Ty, V)) + return C; + return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V); +} +Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) { // Empty arrays are canonicalized to ConstantAggregateZero. if (V.empty()) return ConstantAggregateZero::get(Ty); @@ -789,7 +837,6 @@ Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) { assert(V[i]->getType() == Ty->getElementType() && "Wrong type in array element initializer"); } - LLVMContextImpl *pImpl = Ty->getContext().pImpl; // If this is an all-zero array, return a ConstantAggregateZero object. If // all undef, return an UndefValue, if "all simple", then return a @@ -871,7 +918,7 @@ Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) { } // Otherwise, we really do want to create a ConstantArray. - return pImpl->ArrayConstants.getOrCreate(Ty, V); + return nullptr; } /// getTypeForElements - Return an anonymous struct type to use for a constant @@ -959,9 +1006,14 @@ ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V) // ConstantVector accessors. Constant *ConstantVector::get(ArrayRef<Constant*> V) { + if (Constant *C = getImpl(V)) + return C; + VectorType *Ty = VectorType::get(V.front()->getType(), V.size()); + return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V); +} +Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) { assert(!V.empty() && "Vectors can't be empty"); VectorType *T = VectorType::get(V.front()->getType(), V.size()); - LLVMContextImpl *pImpl = T->getContext().pImpl; // If this is an all-undef or all-zero vector, return a // ConstantAggregateZero or UndefValue. @@ -1053,7 +1105,7 @@ Constant *ConstantVector::get(ArrayRef<Constant*> V) { // Otherwise, the element type isn't compatible with ConstantDataVector, or // the operand list constants a ConstantExpr or something else strange. - return pImpl->VectorConstants.getOrCreate(T, V); + return nullptr; } Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) { @@ -1141,8 +1193,8 @@ ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const { /// getWithOperands - This returns the current constant expression with the /// operands replaced with the specified values. The specified array must /// have the same number of operands as our current one. -Constant *ConstantExpr:: -getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const { +Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty, + bool OnlyIfReduced) const { assert(Ops.size() == getNumOperands() && "Operand count mismatch!"); bool AnyChange = Ty != getType(); for (unsigned i = 0; i != Ops.size(); ++i) @@ -1151,6 +1203,7 @@ getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const { if (!AnyChange) // No operands changed, return self. return const_cast<ConstantExpr*>(this); + Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr; switch (getOpcode()) { case Instruction::Trunc: case Instruction::ZExt: @@ -1165,28 +1218,34 @@ getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const { case Instruction::IntToPtr: case Instruction::BitCast: case Instruction::AddrSpaceCast: - return ConstantExpr::getCast(getOpcode(), Ops[0], Ty); + return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced); case Instruction::Select: - return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); + return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2], OnlyIfReducedTy); case Instruction::InsertElement: - return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); + return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2], + OnlyIfReducedTy); case Instruction::ExtractElement: - return ConstantExpr::getExtractElement(Ops[0], Ops[1]); + return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy); case Instruction::InsertValue: - return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices()); + return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(), + OnlyIfReducedTy); case Instruction::ExtractValue: - return ConstantExpr::getExtractValue(Ops[0], getIndices()); + return ConstantExpr::getExtractValue(Ops[0], getIndices(), OnlyIfReducedTy); case Instruction::ShuffleVector: - return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); + return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2], + OnlyIfReducedTy); case Instruction::GetElementPtr: return ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1), - cast<GEPOperator>(this)->isInBounds()); + cast<GEPOperator>(this)->isInBounds(), + OnlyIfReducedTy); case Instruction::ICmp: case Instruction::FCmp: - return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]); + return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1], + OnlyIfReducedTy); default: assert(getNumOperands() == 2 && "Must be binary operator?"); - return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData); + return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData, + OnlyIfReducedTy); } } @@ -1447,27 +1506,21 @@ void BlockAddress::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) { // and return early. BlockAddress *&NewBA = getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)]; - if (!NewBA) { - getBasicBlock()->AdjustBlockAddressRefCount(-1); - - // Remove the old entry, this can't cause the map to rehash (just a - // tombstone will get added). - getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(), - getBasicBlock())); - NewBA = this; - setOperand(0, NewF); - setOperand(1, NewBB); - getBasicBlock()->AdjustBlockAddressRefCount(1); + if (NewBA) { + replaceUsesOfWithOnConstantImpl(NewBA); return; } - // Otherwise, I do need to replace this with an existing value. - assert(NewBA != this && "I didn't contain From!"); - - // Everyone using this now uses the replacement. - replaceAllUsesWith(NewBA); + getBasicBlock()->AdjustBlockAddressRefCount(-1); - destroyConstant(); + // Remove the old entry, this can't cause the map to rehash (just a + // tombstone will get added). + getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(), + getBasicBlock())); + NewBA = this; + setOperand(0, NewF); + setOperand(1, NewBB); + getBasicBlock()->AdjustBlockAddressRefCount(1); } //---- ConstantExpr::get() implementations. @@ -1475,22 +1528,26 @@ void BlockAddress::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) { /// This is a utility function to handle folding of casts and lookup of the /// cast in the ExprConstants map. It is used by the various get* methods below. -static inline Constant *getFoldedCast( - Instruction::CastOps opc, Constant *C, Type *Ty) { +static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty, + bool OnlyIfReduced = false) { assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); // Fold a few common cases if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty)) return FC; + if (OnlyIfReduced) + return nullptr; + LLVMContextImpl *pImpl = Ty->getContext().pImpl; // Look up the constant in the table first to ensure uniqueness. - ExprMapKeyType Key(opc, C); + ConstantExprKeyType Key(opc, C); return pImpl->ExprConstants.getOrCreate(Ty, Key); } -Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty) { +Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty, + bool OnlyIfReduced) { Instruction::CastOps opc = Instruction::CastOps(oc); assert(Instruction::isCast(opc) && "opcode out of range"); assert(C && Ty && "Null arguments to getCast"); @@ -1499,19 +1556,32 @@ Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty) { switch (opc) { default: llvm_unreachable("Invalid cast opcode"); - case Instruction::Trunc: return getTrunc(C, Ty); - case Instruction::ZExt: return getZExt(C, Ty); - case Instruction::SExt: return getSExt(C, Ty); - case Instruction::FPTrunc: return getFPTrunc(C, Ty); - case Instruction::FPExt: return getFPExtend(C, Ty); - case Instruction::UIToFP: return getUIToFP(C, Ty); - case Instruction::SIToFP: return getSIToFP(C, Ty); - case Instruction::FPToUI: return getFPToUI(C, Ty); - case Instruction::FPToSI: return getFPToSI(C, Ty); - case Instruction::PtrToInt: return getPtrToInt(C, Ty); - case Instruction::IntToPtr: return getIntToPtr(C, Ty); - case Instruction::BitCast: return getBitCast(C, Ty); - case Instruction::AddrSpaceCast: return getAddrSpaceCast(C, Ty); + case Instruction::Trunc: + return getTrunc(C, Ty, OnlyIfReduced); + case Instruction::ZExt: + return getZExt(C, Ty, OnlyIfReduced); + case Instruction::SExt: + return getSExt(C, Ty, OnlyIfReduced); + case Instruction::FPTrunc: + return getFPTrunc(C, Ty, OnlyIfReduced); + case Instruction::FPExt: + return getFPExtend(C, Ty, OnlyIfReduced); + case Instruction::UIToFP: + return getUIToFP(C, Ty, OnlyIfReduced); + case Instruction::SIToFP: + return getSIToFP(C, Ty, OnlyIfReduced); + case Instruction::FPToUI: + return getFPToUI(C, Ty, OnlyIfReduced); + case Instruction::FPToSI: + return getFPToSI(C, Ty, OnlyIfReduced); + case Instruction::PtrToInt: + return getPtrToInt(C, Ty, OnlyIfReduced); + case Instruction::IntToPtr: + return getIntToPtr(C, Ty, OnlyIfReduced); + case Instruction::BitCast: + return getBitCast(C, Ty, OnlyIfReduced); + case Instruction::AddrSpaceCast: + return getAddrSpaceCast(C, Ty, OnlyIfReduced); } } @@ -1584,7 +1654,7 @@ Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) { return getCast(opcode, C, Ty); } -Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty) { +Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) { #ifndef NDEBUG bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; bool toVec = Ty->getTypeID() == Type::VectorTyID; @@ -1595,10 +1665,10 @@ Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty) { assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& "SrcTy must be larger than DestTy for Trunc!"); - return getFoldedCast(Instruction::Trunc, C, Ty); + return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced); } -Constant *ConstantExpr::getSExt(Constant *C, Type *Ty) { +Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) { #ifndef NDEBUG bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; bool toVec = Ty->getTypeID() == Type::VectorTyID; @@ -1609,10 +1679,10 @@ Constant *ConstantExpr::getSExt(Constant *C, Type *Ty) { assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& "SrcTy must be smaller than DestTy for SExt!"); - return getFoldedCast(Instruction::SExt, C, Ty); + return getFoldedCast(Instruction::SExt, C, Ty, OnlyIfReduced); } -Constant *ConstantExpr::getZExt(Constant *C, Type *Ty) { +Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) { #ifndef NDEBUG bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; bool toVec = Ty->getTypeID() == Type::VectorTyID; @@ -1623,10 +1693,10 @@ Constant *ConstantExpr::getZExt(Constant *C, Type *Ty) { assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& "SrcTy must be smaller than DestTy for ZExt!"); - return getFoldedCast(Instruction::ZExt, C, Ty); + return getFoldedCast(Instruction::ZExt, C, Ty, OnlyIfReduced); } -Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty) { +Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) { #ifndef NDEBUG bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; bool toVec = Ty->getTypeID() == Type::VectorTyID; @@ -1635,10 +1705,10 @@ Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty) { assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& "This is an illegal floating point truncation!"); - return getFoldedCast(Instruction::FPTrunc, C, Ty); + return getFoldedCast(Instruction::FPTrunc, C, Ty, OnlyIfReduced); } -Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty) { +Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty, bool OnlyIfReduced) { #ifndef NDEBUG bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; bool toVec = Ty->getTypeID() == Type::VectorTyID; @@ -1647,10 +1717,10 @@ Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty) { assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& "This is an illegal floating point extension!"); - return getFoldedCast(Instruction::FPExt, C, Ty); + return getFoldedCast(Instruction::FPExt, C, Ty, OnlyIfReduced); } -Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty) { +Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) { #ifndef NDEBUG bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; bool toVec = Ty->getTypeID() == Type::VectorTyID; @@ -1658,10 +1728,10 @@ Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty) { assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() && "This is an illegal uint to floating point cast!"); - return getFoldedCast(Instruction::UIToFP, C, Ty); + return getFoldedCast(Instruction::UIToFP, C, Ty, OnlyIfReduced); } -Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty) { +Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) { #ifndef NDEBUG bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; bool toVec = Ty->getTypeID() == Type::VectorTyID; @@ -1669,10 +1739,10 @@ Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty) { assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() && "This is an illegal sint to floating point cast!"); - return getFoldedCast(Instruction::SIToFP, C, Ty); + return getFoldedCast(Instruction::SIToFP, C, Ty, OnlyIfReduced); } -Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty) { +Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced) { #ifndef NDEBUG bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; bool toVec = Ty->getTypeID() == Type::VectorTyID; @@ -1680,10 +1750,10 @@ Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty) { assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() && "This is an illegal floating point to uint cast!"); - return getFoldedCast(Instruction::FPToUI, C, Ty); + return getFoldedCast(Instruction::FPToUI, C, Ty, OnlyIfReduced); } -Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty) { +Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced) { #ifndef NDEBUG bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; bool toVec = Ty->getTypeID() == Type::VectorTyID; @@ -1691,10 +1761,11 @@ Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty) { assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() && "This is an illegal floating point to sint cast!"); - return getFoldedCast(Instruction::FPToSI, C, Ty); + return getFoldedCast(Instruction::FPToSI, C, Ty, OnlyIfReduced); } -Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy) { +Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy, + bool OnlyIfReduced) { assert(C->getType()->getScalarType()->isPointerTy() && "PtrToInt source must be pointer or pointer vector"); assert(DstTy->getScalarType()->isIntegerTy() && @@ -1703,10 +1774,11 @@ Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy) { if (isa<VectorType>(C->getType())) assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&& "Invalid cast between a different number of vector elements"); - return getFoldedCast(Instruction::PtrToInt, C, DstTy); + return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced); } -Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy) { +Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy, + bool OnlyIfReduced) { assert(C->getType()->getScalarType()->isIntegerTy() && "IntToPtr source must be integer or integer vector"); assert(DstTy->getScalarType()->isPointerTy() && @@ -1715,10 +1787,11 @@ Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy) { if (isa<VectorType>(C->getType())) assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&& "Invalid cast between a different number of vector elements"); - return getFoldedCast(Instruction::IntToPtr, C, DstTy); + return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced); } -Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy) { +Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy, + bool OnlyIfReduced) { assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) && "Invalid constantexpr bitcast!"); @@ -1726,10 +1799,11 @@ Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy) { // speedily. if (C->getType() == DstTy) return C; - return getFoldedCast(Instruction::BitCast, C, DstTy); + return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced); } -Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy) { +Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy, + bool OnlyIfReduced) { assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) && "Invalid constantexpr addrspacecast!"); @@ -1746,11 +1820,11 @@ Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy) { } C = getBitCast(C, MidTy); } - return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy); + return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced); } Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2, - unsigned Flags) { + unsigned Flags, Type *OnlyIfReducedTy) { // Check the operands for consistency first. assert(Opcode >= Instruction::BinaryOpsBegin && Opcode < Instruction::BinaryOpsEnd && @@ -1819,8 +1893,11 @@ Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2, if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) return FC; // Fold a few common cases. + if (OnlyIfReducedTy == C1->getType()) + return nullptr; + Constant *ArgVec[] = { C1, C2 }; - ExprMapKeyType Key(Opcode, ArgVec, 0, Flags); + ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags); LLVMContextImpl *pImpl = C1->getContext().pImpl; return pImpl->ExprConstants.getOrCreate(C1->getType(), Key); @@ -1840,7 +1917,7 @@ Constant *ConstantExpr::getAlignOf(Type* Ty) { // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1 // Note that a non-inbounds gep is used, as null isn't within any object. Type *AligningTy = - StructType::get(Type::getInt1Ty(Ty->getContext()), Ty, NULL); + StructType::get(Type::getInt1Ty(Ty->getContext()), Ty, nullptr); Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0)); Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0); Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); @@ -1868,8 +1945,8 @@ Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) { Type::getInt64Ty(Ty->getContext())); } -Constant *ConstantExpr::getCompare(unsigned short Predicate, - Constant *C1, Constant *C2) { +Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1, + Constant *C2, bool OnlyIfReduced) { assert(C1->getType() == C2->getType() && "Op types should be identical!"); switch (Predicate) { @@ -1880,31 +1957,35 @@ Constant *ConstantExpr::getCompare(unsigned short Predicate, case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE: case CmpInst::FCMP_TRUE: - return getFCmp(Predicate, C1, C2); + return getFCmp(Predicate, C1, C2, OnlyIfReduced); case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT: case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE: case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT: case CmpInst::ICMP_SLE: - return getICmp(Predicate, C1, C2); + return getICmp(Predicate, C1, C2, OnlyIfReduced); } } -Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2) { +Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2, + Type *OnlyIfReducedTy) { assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands"); if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2)) return SC; // Fold common cases + if (OnlyIfReducedTy == V1->getType()) + return nullptr; + Constant *ArgVec[] = { C, V1, V2 }; - ExprMapKeyType Key(Instruction::Select, ArgVec); + ConstantExprKeyType Key(Instruction::Select, ArgVec); LLVMContextImpl *pImpl = C->getContext().pImpl; return pImpl->ExprConstants.getOrCreate(V1->getType(), Key); } Constant *ConstantExpr::getGetElementPtr(Constant *C, ArrayRef<Value *> Idxs, - bool InBounds) { + bool InBounds, Type *OnlyIfReducedTy) { assert(C->getType()->isPtrOrPtrVectorTy() && "Non-pointer type for constant GetElementPtr expression"); @@ -1919,6 +2000,9 @@ Constant *ConstantExpr::getGetElementPtr(Constant *C, ArrayRef<Value *> Idxs, if (VectorType *VecTy = dyn_cast<VectorType>(C->getType())) ReqTy = VectorType::get(ReqTy, VecTy->getNumElements()); + if (OnlyIfReducedTy == ReqTy) + return nullptr; + // Look up the constant in the table first to ensure uniqueness std::vector<Constant*> ArgVec; ArgVec.reserve(1 + Idxs.size()); @@ -1932,15 +2016,15 @@ Constant *ConstantExpr::getGetElementPtr(Constant *C, ArrayRef<Value *> Idxs, "getelementptr index type missmatch"); ArgVec.push_back(cast<Constant>(Idxs[i])); } - const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec, 0, - InBounds ? GEPOperator::IsInBounds : 0); + const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0, + InBounds ? GEPOperator::IsInBounds : 0); LLVMContextImpl *pImpl = C->getContext().pImpl; return pImpl->ExprConstants.getOrCreate(ReqTy, Key); } -Constant * -ConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) { +Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS, + Constant *RHS, bool OnlyIfReduced) { assert(LHS->getType() == RHS->getType()); assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE && pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate"); @@ -1948,10 +2032,13 @@ ConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) { if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS)) return FC; // Fold a few common cases... + if (OnlyIfReduced) + return nullptr; + // Look up the constant in the table first to ensure uniqueness Constant *ArgVec[] = { LHS, RHS }; // Get the key type with both the opcode and predicate - const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred); + const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred); Type *ResultTy = Type::getInt1Ty(LHS->getContext()); if (VectorType *VT = dyn_cast<VectorType>(LHS->getType())) @@ -1961,18 +2048,21 @@ ConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) { return pImpl->ExprConstants.getOrCreate(ResultTy, Key); } -Constant * -ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, Constant *RHS) { +Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, + Constant *RHS, bool OnlyIfReduced) { assert(LHS->getType() == RHS->getType()); assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate"); if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS)) return FC; // Fold a few common cases... + if (OnlyIfReduced) + return nullptr; + // Look up the constant in the table first to ensure uniqueness Constant *ArgVec[] = { LHS, RHS }; // Get the key type with both the opcode and predicate - const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred); + const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred); Type *ResultTy = Type::getInt1Ty(LHS->getContext()); if (VectorType *VT = dyn_cast<VectorType>(LHS->getType())) @@ -1982,7 +2072,8 @@ ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, Constant *RHS) { return pImpl->ExprConstants.getOrCreate(ResultTy, Key); } -Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { +Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx, + Type *OnlyIfReducedTy) { assert(Val->getType()->isVectorTy() && "Tried to create extractelement operation on non-vector type!"); assert(Idx->getType()->isIntegerTy() && @@ -1991,17 +2082,20 @@ Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx)) return FC; // Fold a few common cases. + Type *ReqTy = Val->getType()->getVectorElementType(); + if (OnlyIfReducedTy == ReqTy) + return nullptr; + // Look up the constant in the table first to ensure uniqueness Constant *ArgVec[] = { Val, Idx }; - const ExprMapKeyType Key(Instruction::ExtractElement, ArgVec); + const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec); LLVMContextImpl *pImpl = Val->getContext().pImpl; - Type *ReqTy = Val->getType()->getVectorElementType(); return pImpl->ExprConstants.getOrCreate(ReqTy, Key); } -Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, - Constant *Idx) { +Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, + Constant *Idx, Type *OnlyIfReducedTy) { assert(Val->getType()->isVectorTy() && "Tried to create insertelement operation on non-vector type!"); assert(Elt->getType() == Val->getType()->getVectorElementType() && @@ -2011,16 +2105,20 @@ Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx)) return FC; // Fold a few common cases. + + if (OnlyIfReducedTy == Val->getType()) + return nullptr; + // Look up the constant in the table first to ensure uniqueness Constant *ArgVec[] = { Val, Elt, Idx }; - const ExprMapKeyType Key(Instruction::InsertElement, ArgVec); + const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec); LLVMContextImpl *pImpl = Val->getContext().pImpl; return pImpl->ExprConstants.getOrCreate(Val->getType(), Key); } -Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, - Constant *Mask) { +Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, + Constant *Mask, Type *OnlyIfReducedTy) { assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && "Invalid shuffle vector constant expr operands!"); @@ -2031,16 +2129,20 @@ Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, Type *EltTy = V1->getType()->getVectorElementType(); Type *ShufTy = VectorType::get(EltTy, NElts); + if (OnlyIfReducedTy == ShufTy) + return nullptr; + // Look up the constant in the table first to ensure uniqueness Constant *ArgVec[] = { V1, V2, Mask }; - const ExprMapKeyType Key(Instruction::ShuffleVector, ArgVec); + const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec); LLVMContextImpl *pImpl = ShufTy->getContext().pImpl; return pImpl->ExprConstants.getOrCreate(ShufTy, Key); } Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val, - ArrayRef<unsigned> Idxs) { + ArrayRef<unsigned> Idxs, + Type *OnlyIfReducedTy) { assert(Agg->getType()->isFirstClassType() && "Non-first-class type for constant insertvalue expression"); @@ -2052,15 +2154,18 @@ Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val, if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs)) return FC; + if (OnlyIfReducedTy == ReqTy) + return nullptr; + Constant *ArgVec[] = { Agg, Val }; - const ExprMapKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs); + const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs); LLVMContextImpl *pImpl = Agg->getContext().pImpl; return pImpl->ExprConstants.getOrCreate(ReqTy, Key); } -Constant *ConstantExpr::getExtractValue(Constant *Agg, - ArrayRef<unsigned> Idxs) { +Constant *ConstantExpr::getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs, + Type *OnlyIfReducedTy) { assert(Agg->getType()->isFirstClassType() && "Tried to create extractelement operation on non-first-class type!"); @@ -2073,8 +2178,11 @@ Constant *ConstantExpr::getExtractValue(Constant *Agg, if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs)) return FC; + if (OnlyIfReducedTy == ReqTy) + return nullptr; + Constant *ArgVec[] = { Agg }; - const ExprMapKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs); + const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs); LLVMContextImpl *pImpl = Agg->getContext().pImpl; return pImpl->ExprConstants.getOrCreate(ReqTy, Key); @@ -2326,14 +2434,16 @@ Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) { return ConstantAggregateZero::get(Ty); // Do a lookup to see if we have already formed one of these. - StringMap<ConstantDataSequential*>::MapEntryTy &Slot = - Ty->getContext().pImpl->CDSConstants.GetOrCreateValue(Elements); + auto &Slot = + *Ty->getContext() + .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr)) + .first; // The bucket can point to a linked list of different CDS's that have the same // body but different types. For example, 0,0,0,1 could be a 4 element array // of i8, or a 1-element array of i32. They'll both end up in the same /// StringMap bucket, linked up by their Next pointers. Walk the list. - ConstantDataSequential **Entry = &Slot.getValue(); + ConstantDataSequential **Entry = &Slot.second; for (ConstantDataSequential *Node = *Entry; Node; Entry = &Node->Next, Node = *Entry) if (Node->getType() == Ty) @@ -2342,10 +2452,10 @@ Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) { // Okay, we didn't get a hit. Create a node of the right class, link it in, // and return it. if (isa<ArrayType>(Ty)) - return *Entry = new ConstantDataArray(Ty, Slot.getKeyData()); + return *Entry = new ConstantDataArray(Ty, Slot.first().data()); assert(isa<VectorType>(Ty)); - return *Entry = new ConstantDataVector(Ty, Slot.getKeyData()); + return *Entry = new ConstantDataVector(Ty, Slot.first().data()); } void ConstantDataSequential::destroyConstant() { @@ -2431,7 +2541,7 @@ Constant *ConstantDataArray::getString(LLVMContext &Context, StringRef Str, bool AddNull) { if (!AddNull) { const uint8_t *Data = reinterpret_cast<const uint8_t *>(Str.data()); - return get(Context, ArrayRef<uint8_t>(const_cast<uint8_t *>(Data), + return get(Context, makeArrayRef(const_cast<uint8_t *>(Data), Str.size())); } @@ -2602,7 +2712,7 @@ bool ConstantDataSequential::isCString() const { } /// getSplatValue - If this is a splat constant, meaning that all of the -/// elements have the same value, return that value. Otherwise return NULL. +/// elements have the same value, return that value. Otherwise return nullptr. Constant *ConstantDataVector::getSplatValue() const { const char *Base = getRawDataValues().data(); @@ -2630,16 +2740,23 @@ Constant *ConstantDataVector::getSplatValue() const { /// work, but would be really slow because it would have to unique each updated /// array instance. /// +void Constant::replaceUsesOfWithOnConstantImpl(Constant *Replacement) { + // I do need to replace this with an existing value. + assert(Replacement != this && "I didn't contain From!"); + + // Everyone using this now uses the replacement. + replaceAllUsesWith(Replacement); + + // Delete the old constant! + destroyConstant(); +} + void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) { assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); Constant *ToC = cast<Constant>(To); - LLVMContextImpl *pImpl = getType()->getContext().pImpl; - SmallVector<Constant*, 8> Values; - LLVMContextImpl::ArrayConstantsTy::LookupKey Lookup; - Lookup.first = cast<ArrayType>(getType()); Values.reserve(getNumOperands()); // Build replacement array. // Fill values with the modified operands of the constant array. Also, @@ -2658,51 +2775,25 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To, AllSame &= Val == ToC; } - Constant *Replacement = nullptr; if (AllSame && ToC->isNullValue()) { - Replacement = ConstantAggregateZero::get(getType()); - } else if (AllSame && isa<UndefValue>(ToC)) { - Replacement = UndefValue::get(getType()); - } else { - // Check to see if we have this array type already. - Lookup.second = makeArrayRef(Values); - LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I = - pImpl->ArrayConstants.find(Lookup); - - if (I != pImpl->ArrayConstants.map_end()) { - Replacement = I->first; - } else { - // Okay, the new shape doesn't exist in the system yet. Instead of - // creating a new constant array, inserting it, replaceallusesof'ing the - // old with the new, then deleting the old... just update the current one - // in place! - pImpl->ArrayConstants.remove(this); - - // Update to the new value. Optimize for the case when we have a single - // operand that we're changing, but handle bulk updates efficiently. - if (NumUpdated == 1) { - unsigned OperandToUpdate = U - OperandList; - assert(getOperand(OperandToUpdate) == From && - "ReplaceAllUsesWith broken!"); - setOperand(OperandToUpdate, ToC); - } else { - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - if (getOperand(i) == From) - setOperand(i, ToC); - } - pImpl->ArrayConstants.insert(this); - return; - } + replaceUsesOfWithOnConstantImpl(ConstantAggregateZero::get(getType())); + return; + } + if (AllSame && isa<UndefValue>(ToC)) { + replaceUsesOfWithOnConstantImpl(UndefValue::get(getType())); + return; } - // Otherwise, I do need to replace this with an existing value. - assert(Replacement != this && "I didn't contain From!"); - - // Everyone using this now uses the replacement. - replaceAllUsesWith(Replacement); + // Check for any other type of constant-folding. + if (Constant *C = getImpl(getType(), Values)) { + replaceUsesOfWithOnConstantImpl(C); + return; + } - // Delete the old constant! - destroyConstant(); + // Update to the new value. + if (Constant *C = getContext().pImpl->ArrayConstants.replaceOperandsInPlace( + Values, this, From, ToC, NumUpdated, U - OperandList)) + replaceUsesOfWithOnConstantImpl(C); } void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, @@ -2714,8 +2805,6 @@ void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!"); SmallVector<Constant*, 8> Values; - LLVMContextImpl::StructConstantsTy::LookupKey Lookup; - Lookup.first = cast<StructType>(getType()); Values.reserve(getNumOperands()); // Build replacement struct. // Fill values with the modified operands of the constant struct. Also, @@ -2742,64 +2831,47 @@ void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To, } Values[OperandToUpdate] = ToC; - LLVMContextImpl *pImpl = getContext().pImpl; - - Constant *Replacement = nullptr; if (isAllZeros) { - Replacement = ConstantAggregateZero::get(getType()); - } else if (isAllUndef) { - Replacement = UndefValue::get(getType()); - } else { - // Check to see if we have this struct type already. - Lookup.second = makeArrayRef(Values); - LLVMContextImpl::StructConstantsTy::MapTy::iterator I = - pImpl->StructConstants.find(Lookup); - - if (I != pImpl->StructConstants.map_end()) { - Replacement = I->first; - } else { - // Okay, the new shape doesn't exist in the system yet. Instead of - // creating a new constant struct, inserting it, replaceallusesof'ing the - // old with the new, then deleting the old... just update the current one - // in place! - pImpl->StructConstants.remove(this); - - // Update to the new value. - setOperand(OperandToUpdate, ToC); - pImpl->StructConstants.insert(this); - return; - } + replaceUsesOfWithOnConstantImpl(ConstantAggregateZero::get(getType())); + return; + } + if (isAllUndef) { + replaceUsesOfWithOnConstantImpl(UndefValue::get(getType())); + return; } - assert(Replacement != this && "I didn't contain From!"); - - // Everyone using this now uses the replacement. - replaceAllUsesWith(Replacement); - - // Delete the old constant! - destroyConstant(); + // Update to the new value. + if (Constant *C = getContext().pImpl->StructConstants.replaceOperandsInPlace( + Values, this, From, ToC)) + replaceUsesOfWithOnConstantImpl(C); } void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) { assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); + Constant *ToC = cast<Constant>(To); SmallVector<Constant*, 8> Values; Values.reserve(getNumOperands()); // Build replacement array... + unsigned NumUpdated = 0; for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { Constant *Val = getOperand(i); - if (Val == From) Val = cast<Constant>(To); + if (Val == From) { + ++NumUpdated; + Val = ToC; + } Values.push_back(Val); } - Constant *Replacement = get(Values); - assert(Replacement != this && "I didn't contain From!"); - - // Everyone using this now uses the replacement. - replaceAllUsesWith(Replacement); + if (Constant *C = getImpl(Values)) { + replaceUsesOfWithOnConstantImpl(C); + return; + } - // Delete the old constant! - destroyConstant(); + // Update to the new value. + if (Constant *C = getContext().pImpl->VectorConstants.replaceOperandsInPlace( + Values, this, From, ToC, NumUpdated, U - OperandList)) + replaceUsesOfWithOnConstantImpl(C); } void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, @@ -2808,19 +2880,26 @@ void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, Constant *To = cast<Constant>(ToV); SmallVector<Constant*, 8> NewOps; + unsigned NumUpdated = 0; for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { Constant *Op = getOperand(i); - NewOps.push_back(Op == From ? To : Op); + if (Op == From) { + ++NumUpdated; + Op = To; + } + NewOps.push_back(Op); } + assert(NumUpdated && "I didn't contain From!"); - Constant *Replacement = getWithOperands(NewOps); - assert(Replacement != this && "I didn't contain From!"); - - // Everyone using this now uses the replacement. - replaceAllUsesWith(Replacement); + if (Constant *C = getWithOperands(NewOps, getType(), true)) { + replaceUsesOfWithOnConstantImpl(C); + return; + } - // Delete the old constant! - destroyConstant(); + // Update to the new value. + if (Constant *C = getContext().pImpl->ExprConstants.replaceOperandsInPlace( + NewOps, this, From, To, NumUpdated, U - OperandList)) + replaceUsesOfWithOnConstantImpl(C); } Instruction *ConstantExpr::getAsInstruction() { diff --git a/contrib/llvm/lib/IR/ConstantsContext.h b/contrib/llvm/lib/IR/ConstantsContext.h index f06509f..571dec2 100644 --- a/contrib/llvm/lib/IR/ConstantsContext.h +++ b/contrib/llvm/lib/IR/ConstantsContext.h @@ -12,8 +12,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CONSTANTSCONTEXT_H -#define LLVM_CONSTANTSCONTEXT_H +#ifndef LLVM_LIB_IR_CONSTANTSCONTEXT_H +#define LLVM_LIB_IR_CONSTANTSCONTEXT_H #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/Hashing.h" @@ -29,8 +29,6 @@ #define DEBUG_TYPE "ir" namespace llvm { -template<class ValType> -struct ConstantTraits; /// UnaryConstantExpr - This class is private to Constants.cpp, and is used /// behind the scenes to implement unary constant exprs. @@ -169,11 +167,10 @@ public: void *operator new(size_t s) { return User::operator new(s, 1); } - ExtractValueConstantExpr(Constant *Agg, - const SmallVector<unsigned, 4> &IdxList, + ExtractValueConstantExpr(Constant *Agg, ArrayRef<unsigned> IdxList, Type *DestTy) - : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1), - Indices(IdxList) { + : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1), + Indices(IdxList.begin(), IdxList.end()) { Op<0>() = Agg; } @@ -196,10 +193,9 @@ public: return User::operator new(s, 2); } InsertValueConstantExpr(Constant *Agg, Constant *Val, - const SmallVector<unsigned, 4> &IdxList, - Type *DestTy) - : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2), - Indices(IdxList) { + ArrayRef<unsigned> IdxList, Type *DestTy) + : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2), + Indices(IdxList.begin(), IdxList.end()) { Op<0>() = Agg; Op<1>() = Val; } @@ -316,379 +312,241 @@ struct OperandTraits<CompareConstantExpr> : }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value) -struct ExprMapKeyType { - ExprMapKeyType(unsigned opc, - ArrayRef<Constant*> ops, - unsigned short flags = 0, - unsigned short optionalflags = 0, - ArrayRef<unsigned> inds = None) - : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags), - operands(ops.begin(), ops.end()), indices(inds.begin(), inds.end()) {} - uint8_t opcode; - uint8_t subclassoptionaldata; - uint16_t subclassdata; - std::vector<Constant*> operands; - SmallVector<unsigned, 4> indices; - bool operator==(const ExprMapKeyType& that) const { - return this->opcode == that.opcode && - this->subclassdata == that.subclassdata && - this->subclassoptionaldata == that.subclassoptionaldata && - this->operands == that.operands && - this->indices == that.indices; - } - bool operator<(const ExprMapKeyType & that) const { - return std::tie(opcode, operands, subclassdata, subclassoptionaldata, - indices) < - std::tie(that.opcode, that.operands, that.subclassdata, - that.subclassoptionaldata, that.indices); - } - - bool operator!=(const ExprMapKeyType& that) const { - return !(*this == that); - } -}; - -struct InlineAsmKeyType { - InlineAsmKeyType(StringRef AsmString, - StringRef Constraints, bool hasSideEffects, - bool isAlignStack, InlineAsm::AsmDialect asmDialect) - : asm_string(AsmString), constraints(Constraints), - has_side_effects(hasSideEffects), is_align_stack(isAlignStack), - asm_dialect(asmDialect) {} - std::string asm_string; - std::string constraints; - bool has_side_effects; - bool is_align_stack; - InlineAsm::AsmDialect asm_dialect; - bool operator==(const InlineAsmKeyType& that) const { - return this->asm_string == that.asm_string && - this->constraints == that.constraints && - this->has_side_effects == that.has_side_effects && - this->is_align_stack == that.is_align_stack && - this->asm_dialect == that.asm_dialect; - } - bool operator<(const InlineAsmKeyType& that) const { - return std::tie(asm_string, constraints, has_side_effects, is_align_stack, - asm_dialect) < - std::tie(that.asm_string, that.constraints, that.has_side_effects, - that.is_align_stack, that.asm_dialect); - } - - bool operator!=(const InlineAsmKeyType& that) const { - return !(*this == that); - } -}; +template <class ConstantClass> struct ConstantAggrKeyType; +struct InlineAsmKeyType; +struct ConstantExprKeyType; -// The number of operands for each ConstantCreator::create method is -// determined by the ConstantTraits template. -// ConstantCreator - A class that is used to create constants by -// ConstantUniqueMap*. This class should be partially specialized if there is -// something strange that needs to be done to interface to the ctor for the -// constant. -// -template<typename T, typename Alloc> -struct ConstantTraits< std::vector<T, Alloc> > { - static unsigned uses(const std::vector<T, Alloc>& v) { - return v.size(); - } -}; - -template<> -struct ConstantTraits<Constant *> { - static unsigned uses(Constant * const & v) { - return 1; - } +template <class ConstantClass> struct ConstantInfo; +template <> struct ConstantInfo<ConstantExpr> { + typedef ConstantExprKeyType ValType; + typedef Type TypeClass; }; - -template<class ConstantClass, class TypeClass, class ValType> -struct ConstantCreator { - static ConstantClass *create(TypeClass *Ty, const ValType &V) { - return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V); +template <> struct ConstantInfo<InlineAsm> { + typedef InlineAsmKeyType ValType; + typedef PointerType TypeClass; +}; +template <> struct ConstantInfo<ConstantArray> { + typedef ConstantAggrKeyType<ConstantArray> ValType; + typedef ArrayType TypeClass; +}; +template <> struct ConstantInfo<ConstantStruct> { + typedef ConstantAggrKeyType<ConstantStruct> ValType; + typedef StructType TypeClass; +}; +template <> struct ConstantInfo<ConstantVector> { + typedef ConstantAggrKeyType<ConstantVector> ValType; + typedef VectorType TypeClass; +}; + +template <class ConstantClass> struct ConstantAggrKeyType { + ArrayRef<Constant *> Operands; + ConstantAggrKeyType(ArrayRef<Constant *> Operands) : Operands(Operands) {} + ConstantAggrKeyType(ArrayRef<Constant *> Operands, const ConstantClass *) + : Operands(Operands) {} + ConstantAggrKeyType(const ConstantClass *C, + SmallVectorImpl<Constant *> &Storage) { + assert(Storage.empty() && "Expected empty storage"); + for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I) + Storage.push_back(C->getOperand(I)); + Operands = Storage; + } + + bool operator==(const ConstantAggrKeyType &X) const { + return Operands == X.Operands; + } + bool operator==(const ConstantClass *C) const { + if (Operands.size() != C->getNumOperands()) + return false; + for (unsigned I = 0, E = Operands.size(); I != E; ++I) + if (Operands[I] != C->getOperand(I)) + return false; + return true; } -}; - -template<class ConstantClass, class TypeClass> -struct ConstantArrayCreator { - static ConstantClass *create(TypeClass *Ty, ArrayRef<Constant*> V) { - return new(V.size()) ConstantClass(Ty, V); + unsigned getHash() const { + return hash_combine_range(Operands.begin(), Operands.end()); } -}; -template<class ConstantClass> -struct ConstantKeyData { - typedef void ValType; - static ValType getValType(ConstantClass *C) { - llvm_unreachable("Unknown Constant type!"); + typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass; + ConstantClass *create(TypeClass *Ty) const { + return new (Operands.size()) ConstantClass(Ty, Operands); } }; -template<> -struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> { - static ConstantExpr *create(Type *Ty, const ExprMapKeyType &V, - unsigned short pred = 0) { - if (Instruction::isCast(V.opcode)) - return new UnaryConstantExpr(V.opcode, V.operands[0], Ty); - if ((V.opcode >= Instruction::BinaryOpsBegin && - V.opcode < Instruction::BinaryOpsEnd)) - return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1], - V.subclassoptionaldata); - if (V.opcode == Instruction::Select) - return new SelectConstantExpr(V.operands[0], V.operands[1], - V.operands[2]); - if (V.opcode == Instruction::ExtractElement) - return new ExtractElementConstantExpr(V.operands[0], V.operands[1]); - if (V.opcode == Instruction::InsertElement) - return new InsertElementConstantExpr(V.operands[0], V.operands[1], - V.operands[2]); - if (V.opcode == Instruction::ShuffleVector) - return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1], - V.operands[2]); - if (V.opcode == Instruction::InsertValue) - return new InsertValueConstantExpr(V.operands[0], V.operands[1], - V.indices, Ty); - if (V.opcode == Instruction::ExtractValue) - return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty); - if (V.opcode == Instruction::GetElementPtr) { - std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end()); - return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty, - V.subclassoptionaldata); +struct InlineAsmKeyType { + StringRef AsmString; + StringRef Constraints; + bool HasSideEffects; + bool IsAlignStack; + InlineAsm::AsmDialect AsmDialect; + + InlineAsmKeyType(StringRef AsmString, StringRef Constraints, + bool HasSideEffects, bool IsAlignStack, + InlineAsm::AsmDialect AsmDialect) + : AsmString(AsmString), Constraints(Constraints), + HasSideEffects(HasSideEffects), IsAlignStack(IsAlignStack), + AsmDialect(AsmDialect) {} + InlineAsmKeyType(const InlineAsm *Asm, SmallVectorImpl<Constant *> &) + : AsmString(Asm->getAsmString()), Constraints(Asm->getConstraintString()), + HasSideEffects(Asm->hasSideEffects()), + IsAlignStack(Asm->isAlignStack()), AsmDialect(Asm->getDialect()) {} + + bool operator==(const InlineAsmKeyType &X) const { + return HasSideEffects == X.HasSideEffects && + IsAlignStack == X.IsAlignStack && AsmDialect == X.AsmDialect && + AsmString == X.AsmString && Constraints == X.Constraints; + } + bool operator==(const InlineAsm *Asm) const { + return HasSideEffects == Asm->hasSideEffects() && + IsAlignStack == Asm->isAlignStack() && + AsmDialect == Asm->getDialect() && + AsmString == Asm->getAsmString() && + Constraints == Asm->getConstraintString(); + } + unsigned getHash() const { + return hash_combine(AsmString, Constraints, HasSideEffects, IsAlignStack, + AsmDialect); + } + + typedef ConstantInfo<InlineAsm>::TypeClass TypeClass; + InlineAsm *create(TypeClass *Ty) const { + return new InlineAsm(Ty, AsmString, Constraints, HasSideEffects, + IsAlignStack, AsmDialect); + } +}; + +struct ConstantExprKeyType { + uint8_t Opcode; + uint8_t SubclassOptionalData; + uint16_t SubclassData; + ArrayRef<Constant *> Ops; + ArrayRef<unsigned> Indexes; + + ConstantExprKeyType(unsigned Opcode, ArrayRef<Constant *> Ops, + unsigned short SubclassData = 0, + unsigned short SubclassOptionalData = 0, + ArrayRef<unsigned> Indexes = None) + : Opcode(Opcode), SubclassOptionalData(SubclassOptionalData), + SubclassData(SubclassData), Ops(Ops), Indexes(Indexes) {} + ConstantExprKeyType(ArrayRef<Constant *> Operands, const ConstantExpr *CE) + : Opcode(CE->getOpcode()), + SubclassOptionalData(CE->getRawSubclassOptionalData()), + SubclassData(CE->isCompare() ? CE->getPredicate() : 0), Ops(Operands), + Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {} + ConstantExprKeyType(const ConstantExpr *CE, + SmallVectorImpl<Constant *> &Storage) + : Opcode(CE->getOpcode()), + SubclassOptionalData(CE->getRawSubclassOptionalData()), + SubclassData(CE->isCompare() ? CE->getPredicate() : 0), + Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) { + assert(Storage.empty() && "Expected empty storage"); + for (unsigned I = 0, E = CE->getNumOperands(); I != E; ++I) + Storage.push_back(CE->getOperand(I)); + Ops = Storage; + } + + bool operator==(const ConstantExprKeyType &X) const { + return Opcode == X.Opcode && SubclassData == X.SubclassData && + SubclassOptionalData == X.SubclassOptionalData && Ops == X.Ops && + Indexes == X.Indexes; + } + + bool operator==(const ConstantExpr *CE) const { + if (Opcode != CE->getOpcode()) + return false; + if (SubclassOptionalData != CE->getRawSubclassOptionalData()) + return false; + if (Ops.size() != CE->getNumOperands()) + return false; + if (SubclassData != (CE->isCompare() ? CE->getPredicate() : 0)) + return false; + for (unsigned I = 0, E = Ops.size(); I != E; ++I) + if (Ops[I] != CE->getOperand(I)) + return false; + if (Indexes != (CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>())) + return false; + return true; + } + + unsigned getHash() const { + return hash_combine(Opcode, SubclassOptionalData, SubclassData, + hash_combine_range(Ops.begin(), Ops.end()), + hash_combine_range(Indexes.begin(), Indexes.end())); + } + + typedef ConstantInfo<ConstantExpr>::TypeClass TypeClass; + ConstantExpr *create(TypeClass *Ty) const { + switch (Opcode) { + default: + if (Instruction::isCast(Opcode)) + return new UnaryConstantExpr(Opcode, Ops[0], Ty); + if ((Opcode >= Instruction::BinaryOpsBegin && + Opcode < Instruction::BinaryOpsEnd)) + return new BinaryConstantExpr(Opcode, Ops[0], Ops[1], + SubclassOptionalData); + llvm_unreachable("Invalid ConstantExpr!"); + case Instruction::Select: + return new SelectConstantExpr(Ops[0], Ops[1], Ops[2]); + case Instruction::ExtractElement: + return new ExtractElementConstantExpr(Ops[0], Ops[1]); + case Instruction::InsertElement: + return new InsertElementConstantExpr(Ops[0], Ops[1], Ops[2]); + case Instruction::ShuffleVector: + return new ShuffleVectorConstantExpr(Ops[0], Ops[1], Ops[2]); + case Instruction::InsertValue: + return new InsertValueConstantExpr(Ops[0], Ops[1], Indexes, Ty); + case Instruction::ExtractValue: + return new ExtractValueConstantExpr(Ops[0], Indexes, Ty); + case Instruction::GetElementPtr: + return GetElementPtrConstantExpr::Create(Ops[0], Ops.slice(1), Ty, + SubclassOptionalData); + case Instruction::ICmp: + return new CompareConstantExpr(Ty, Instruction::ICmp, SubclassData, + Ops[0], Ops[1]); + case Instruction::FCmp: + return new CompareConstantExpr(Ty, Instruction::FCmp, SubclassData, + Ops[0], Ops[1]); } - - // The compare instructions are weird. We have to encode the predicate - // value and it is combined with the instruction opcode by multiplying - // the opcode by one hundred. We must decode this to get the predicate. - if (V.opcode == Instruction::ICmp) - return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata, - V.operands[0], V.operands[1]); - if (V.opcode == Instruction::FCmp) - return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata, - V.operands[0], V.operands[1]); - llvm_unreachable("Invalid ConstantExpr!"); - } -}; - -template<> -struct ConstantKeyData<ConstantExpr> { - typedef ExprMapKeyType ValType; - static ValType getValType(ConstantExpr *CE) { - std::vector<Constant*> Operands; - Operands.reserve(CE->getNumOperands()); - for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) - Operands.push_back(cast<Constant>(CE->getOperand(i))); - return ExprMapKeyType(CE->getOpcode(), Operands, - CE->isCompare() ? CE->getPredicate() : 0, - CE->getRawSubclassOptionalData(), - CE->hasIndices() ? - CE->getIndices() : ArrayRef<unsigned>()); - } -}; - -template<> -struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> { - static InlineAsm *create(PointerType *Ty, const InlineAsmKeyType &Key) { - return new InlineAsm(Ty, Key.asm_string, Key.constraints, - Key.has_side_effects, Key.is_align_stack, - Key.asm_dialect); } }; -template<> -struct ConstantKeyData<InlineAsm> { - typedef InlineAsmKeyType ValType; - static ValType getValType(InlineAsm *Asm) { - return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(), - Asm->hasSideEffects(), Asm->isAlignStack(), - Asm->getDialect()); - } -}; - -template<class ValType, class ValRefType, class TypeClass, class ConstantClass, - bool HasLargeKey = false /*true for arrays and structs*/ > -class ConstantUniqueMap { -public: - typedef std::pair<TypeClass*, ValType> MapKey; - typedef std::map<MapKey, ConstantClass *> MapTy; - typedef std::map<ConstantClass *, typename MapTy::iterator> InverseMapTy; -private: - /// Map - This is the main map from the element descriptor to the Constants. - /// This is the primary way we avoid creating two of the same shape - /// constant. - MapTy Map; - - /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping - /// from the constants to their element in Map. This is important for - /// removal of constants from the array, which would otherwise have to scan - /// through the map with very large keys. - InverseMapTy InverseMap; - -public: - typename MapTy::iterator map_begin() { return Map.begin(); } - typename MapTy::iterator map_end() { return Map.end(); } - - void freeConstants() { - for (typename MapTy::iterator I=Map.begin(), E=Map.end(); - I != E; ++I) { - // Asserts that use_empty(). - delete I->second; - } - } - - /// InsertOrGetItem - Return an iterator for the specified element. - /// If the element exists in the map, the returned iterator points to the - /// entry and Exists=true. If not, the iterator points to the newly - /// inserted entry and returns Exists=false. Newly inserted entries have - /// I->second == 0, and should be filled in. - typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, ConstantClass *> - &InsertVal, - bool &Exists) { - std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal); - Exists = !IP.second; - return IP.first; - } - -private: - typename MapTy::iterator FindExistingElement(ConstantClass *CP) { - if (HasLargeKey) { - typename InverseMapTy::iterator IMI = InverseMap.find(CP); - assert(IMI != InverseMap.end() && IMI->second != Map.end() && - IMI->second->second == CP && - "InverseMap corrupt!"); - return IMI->second; - } - - typename MapTy::iterator I = - Map.find(MapKey(static_cast<TypeClass*>(CP->getType()), - ConstantKeyData<ConstantClass>::getValType(CP))); - if (I == Map.end() || I->second != CP) { - // FIXME: This should not use a linear scan. If this gets to be a - // performance problem, someone should look at this. - for (I = Map.begin(); I != Map.end() && I->second != CP; ++I) - /* empty */; - } - return I; - } - - ConstantClass *Create(TypeClass *Ty, ValRefType V, - typename MapTy::iterator I) { - ConstantClass* Result = - ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V); - - assert(Result->getType() == Ty && "Type specified is not correct!"); - I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result)); - - if (HasLargeKey) // Remember the reverse mapping if needed. - InverseMap.insert(std::make_pair(Result, I)); - - return Result; - } +template <class ConstantClass> class ConstantUniqueMap { public: - - /// getOrCreate - Return the specified constant from the map, creating it if - /// necessary. - ConstantClass *getOrCreate(TypeClass *Ty, ValRefType V) { - MapKey Lookup(Ty, V); - ConstantClass* Result = nullptr; - - typename MapTy::iterator I = Map.find(Lookup); - // Is it in the map? - if (I != Map.end()) - Result = I->second; - - if (!Result) { - // If no preexisting value, create one now... - Result = Create(Ty, V, I); - } - - return Result; - } - - void remove(ConstantClass *CP) { - typename MapTy::iterator I = FindExistingElement(CP); - assert(I != Map.end() && "Constant not found in constant table!"); - assert(I->second == CP && "Didn't find correct element?"); - - if (HasLargeKey) // Remember the reverse mapping if needed. - InverseMap.erase(CP); - - Map.erase(I); - } - - /// MoveConstantToNewSlot - If we are about to change C to be the element - /// specified by I, update our internal data structures to reflect this - /// fact. - void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) { - // First, remove the old location of the specified constant in the map. - typename MapTy::iterator OldI = FindExistingElement(C); - assert(OldI != Map.end() && "Constant not found in constant table!"); - assert(OldI->second == C && "Didn't find correct element?"); - - // Remove the old entry from the map. - Map.erase(OldI); - - // Update the inverse map so that we know that this constant is now - // located at descriptor I. - if (HasLargeKey) { - assert(I->second == C && "Bad inversemap entry!"); - InverseMap[C] = I; - } - } + typedef typename ConstantInfo<ConstantClass>::ValType ValType; + typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass; + typedef std::pair<TypeClass *, ValType> LookupKey; - void dump() const { - DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); - } -}; - -// Unique map for aggregate constants -template<class TypeClass, class ConstantClass> -class ConstantAggrUniqueMap { -public: - typedef ArrayRef<Constant*> Operands; - typedef std::pair<TypeClass*, Operands> LookupKey; private: struct MapInfo { - typedef DenseMapInfo<ConstantClass*> ConstantClassInfo; - typedef DenseMapInfo<Constant*> ConstantInfo; - typedef DenseMapInfo<TypeClass*> TypeClassInfo; - static inline ConstantClass* getEmptyKey() { + typedef DenseMapInfo<ConstantClass *> ConstantClassInfo; + static inline ConstantClass *getEmptyKey() { return ConstantClassInfo::getEmptyKey(); } - static inline ConstantClass* getTombstoneKey() { + static inline ConstantClass *getTombstoneKey() { return ConstantClassInfo::getTombstoneKey(); } static unsigned getHashValue(const ConstantClass *CP) { - SmallVector<Constant*, 8> CPOperands; - CPOperands.reserve(CP->getNumOperands()); - for (unsigned I = 0, E = CP->getNumOperands(); I < E; ++I) - CPOperands.push_back(CP->getOperand(I)); - return getHashValue(LookupKey(CP->getType(), CPOperands)); + SmallVector<Constant *, 8> Storage; + return getHashValue(LookupKey(CP->getType(), ValType(CP, Storage))); } static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) { return LHS == RHS; } static unsigned getHashValue(const LookupKey &Val) { - return hash_combine(Val.first, hash_combine_range(Val.second.begin(), - Val.second.end())); + return hash_combine(Val.first, Val.second.getHash()); } static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) { if (RHS == getEmptyKey() || RHS == getTombstoneKey()) return false; - if (LHS.first != RHS->getType() - || LHS.second.size() != RHS->getNumOperands()) + if (LHS.first != RHS->getType()) return false; - for (unsigned I = 0, E = RHS->getNumOperands(); I < E; ++I) { - if (LHS.second[I] != RHS->getOperand(I)) - return false; - } - return true; + return LHS.second == RHS; } }; + public: typedef DenseMap<ConstantClass *, char, MapInfo> MapTy; private: - /// Map - This is the main map from the element descriptor to the Constants. - /// This is the primary way we avoid creating two of the same shape - /// constant. MapTy Map; public: @@ -696,44 +554,33 @@ public: typename MapTy::iterator map_end() { return Map.end(); } void freeConstants() { - for (typename MapTy::iterator I=Map.begin(), E=Map.end(); - I != E; ++I) { + for (auto &I : Map) // Asserts that use_empty(). - delete I->first; - } + delete I.first; } private: - typename MapTy::iterator findExistingElement(ConstantClass *CP) { - return Map.find(CP); - } - - ConstantClass *Create(TypeClass *Ty, Operands V, typename MapTy::iterator I) { - ConstantClass* Result = - ConstantArrayCreator<ConstantClass,TypeClass>::create(Ty, V); + ConstantClass *create(TypeClass *Ty, ValType V) { + ConstantClass *Result = V.create(Ty); assert(Result->getType() == Ty && "Type specified is not correct!"); - Map[Result] = '\0'; + insert(Result); return Result; } -public: - /// getOrCreate - Return the specified constant from the map, creating it if - /// necessary. - ConstantClass *getOrCreate(TypeClass *Ty, Operands V) { +public: + /// Return the specified constant from the map, creating it if necessary. + ConstantClass *getOrCreate(TypeClass *Ty, ValType V) { LookupKey Lookup(Ty, V); - ConstantClass* Result = nullptr; + ConstantClass *Result = nullptr; - typename MapTy::iterator I = Map.find_as(Lookup); - // Is it in the map? - if (I != Map.end()) + auto I = find(Lookup); + if (I == Map.end()) + Result = create(Ty, V); + else Result = I->first; - - if (!Result) { - // If no preexisting value, create one now... - Result = Create(Ty, V, I); - } + assert(Result && "Unexpected nullptr"); return Result; } @@ -744,23 +591,44 @@ public: } /// Insert the constant into its proper slot. - void insert(ConstantClass *CP) { - Map[CP] = '\0'; - } + void insert(ConstantClass *CP) { Map[CP] = '\0'; } /// Remove this constant from the map void remove(ConstantClass *CP) { - typename MapTy::iterator I = findExistingElement(CP); + typename MapTy::iterator I = Map.find(CP); assert(I != Map.end() && "Constant not found in constant table!"); assert(I->first == CP && "Didn't find correct element?"); Map.erase(I); } - void dump() const { - DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); + ConstantClass *replaceOperandsInPlace(ArrayRef<Constant *> Operands, + ConstantClass *CP, Value *From, + Constant *To, unsigned NumUpdated = 0, + unsigned OperandNo = ~0u) { + LookupKey Lookup(CP->getType(), ValType(Operands, CP)); + auto I = find(Lookup); + if (I != Map.end()) + return I->first; + + // Update to the new value. Optimize for the case when we have a single + // operand that we're changing, but handle bulk updates efficiently. + remove(CP); + if (NumUpdated == 1) { + assert(OperandNo < CP->getNumOperands() && "Invalid index"); + assert(CP->getOperand(OperandNo) != To && "I didn't contain From!"); + CP->setOperand(OperandNo, To); + } else { + for (unsigned I = 0, E = CP->getNumOperands(); I != E; ++I) + if (CP->getOperand(I) == From) + CP->setOperand(I, To); + } + insert(CP); + return nullptr; } + + void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); } }; -} +} // end namespace llvm #endif diff --git a/contrib/llvm/lib/IR/Core.cpp b/contrib/llvm/lib/IR/Core.cpp index 87099a6..753d9c2 100644 --- a/contrib/llvm/lib/IR/Core.cpp +++ b/contrib/llvm/lib/IR/Core.cpp @@ -183,20 +183,22 @@ void LLVMDumpModule(LLVMModuleRef M) { LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename, char **ErrorMessage) { - std::string error; - raw_fd_ostream dest(Filename, error, sys::fs::F_Text); - if (!error.empty()) { - *ErrorMessage = strdup(error.c_str()); + std::error_code EC; + raw_fd_ostream dest(Filename, EC, sys::fs::F_Text); + if (EC) { + *ErrorMessage = strdup(EC.message().c_str()); return true; } unwrap(M)->print(dest, nullptr); - if (!error.empty()) { - *ErrorMessage = strdup(error.c_str()); + dest.close(); + + if (dest.has_error()) { + *ErrorMessage = strdup("Error printing to file"); return true; } - dest.flush(); + return false; } @@ -554,12 +556,31 @@ int LLVMHasMetadata(LLVMValueRef Inst) { } LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) { - return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID)); + auto *I = unwrap<Instruction>(Inst); + assert(I && "Expected instruction"); + if (auto *MD = I->getMetadata(KindID)) + return wrap(MetadataAsValue::get(I->getContext(), MD)); + return nullptr; } -void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) { - unwrap<Instruction>(Inst)->setMetadata(KindID, - MD ? unwrap<MDNode>(MD) : nullptr); +// MetadataAsValue uses a canonical format which strips the actual MDNode for +// MDNode with just a single constant value, storing just a ConstantAsMetadata +// This undoes this canonicalization, reconstructing the MDNode. +static MDNode *extractMDNode(MetadataAsValue *MAV) { + Metadata *MD = MAV->getMetadata(); + assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) && + "Expected a metadata node or a canonicalized constant"); + + if (MDNode *N = dyn_cast<MDNode>(MD)) + return N; + + return MDNode::get(MAV->getContext(), MD); +} + +void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) { + MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr; + + unwrap<Instruction>(Inst)->setMetadata(KindID, N); } /*--.. Conversion functions ................................................--*/ @@ -571,6 +592,21 @@ void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) { LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST) +LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) { + if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val))) + if (isa<MDNode>(MD->getMetadata()) || + isa<ValueAsMetadata>(MD->getMetadata())) + return Val; + return nullptr; +} + +LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) { + if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val))) + if (isa<MDString>(MD->getMetadata())) + return Val; + return nullptr; +} + /*--.. Operations on Uses ..................................................--*/ LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) { Value *V = unwrap(Val); @@ -596,21 +632,45 @@ LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) { } /*--.. Operations on Users .................................................--*/ + +static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N, + unsigned Index) { + Metadata *Op = N->getOperand(Index); + if (!Op) + return nullptr; + if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) + return wrap(C->getValue()); + return wrap(MetadataAsValue::get(Context, Op)); +} + LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) { Value *V = unwrap(Val); - if (MDNode *MD = dyn_cast<MDNode>(V)) - return wrap(MD->getOperand(Index)); + if (auto *MD = dyn_cast<MetadataAsValue>(V)) { + if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) { + assert(Index == 0 && "Function-local metadata can only have one operand"); + return wrap(L->getValue()); + } + return getMDNodeOperandImpl(V->getContext(), + cast<MDNode>(MD->getMetadata()), Index); + } + return wrap(cast<User>(V)->getOperand(Index)); } +LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) { + Value *V = unwrap(Val); + return wrap(&cast<User>(V)->getOperandUse(Index)); +} + void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) { unwrap<User>(Val)->setOperand(Index, unwrap(Op)); } int LLVMGetNumOperands(LLVMValueRef Val) { Value *V = unwrap(Val); - if (MDNode *MD = dyn_cast<MDNode>(V)) - return MD->getNumOperands(); + if (isa<MetadataAsValue>(V)) + return LLVMGetMDNodeNumOperands(Val); + return cast<User>(V)->getNumOperands(); } @@ -651,7 +711,9 @@ LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) { LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str, unsigned SLen) { - return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen))); + LLVMContext &Context = *unwrap(C); + return wrap(MetadataAsValue::get( + Context, MDString::get(Context, StringRef(Str, SLen)))); } LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) { @@ -660,8 +722,29 @@ LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) { LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals, unsigned Count) { - return wrap(MDNode::get(*unwrap(C), - makeArrayRef(unwrap<Value>(Vals, Count), Count))); + LLVMContext &Context = *unwrap(C); + SmallVector<Metadata *, 8> MDs; + for (auto *OV : makeArrayRef(Vals, Count)) { + Value *V = unwrap(OV); + Metadata *MD; + if (!V) + MD = nullptr; + else if (auto *C = dyn_cast<Constant>(V)) + MD = ConstantAsMetadata::get(C); + else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) { + MD = MDV->getMetadata(); + assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata " + "outside of direct argument to call"); + } else { + // This is function-local metadata. Pretend to make an MDNode. + assert(Count == 1 && + "Expected only one operand to function-local metadata"); + return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V))); + } + + MDs.push_back(MD); + } + return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs))); } LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) { @@ -669,25 +752,35 @@ LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) { } const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) { - if (const MDString *S = dyn_cast<MDString>(unwrap(V))) { - *Len = S->getString().size(); - return S->getString().data(); - } + if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V))) + if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) { + *Len = S->getString().size(); + return S->getString().data(); + } *Len = 0; return nullptr; } unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) { - return cast<MDNode>(unwrap(V))->getNumOperands(); + auto *MD = cast<MetadataAsValue>(unwrap(V)); + if (isa<ValueAsMetadata>(MD->getMetadata())) + return 1; + return cast<MDNode>(MD->getMetadata())->getNumOperands(); } void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) { - const MDNode *N = cast<MDNode>(unwrap(V)); + auto *MD = cast<MetadataAsValue>(unwrap(V)); + if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) { + *Dest = wrap(MDV->getValue()); + return; + } + const auto *N = cast<MDNode>(MD->getMetadata()); const unsigned numOperands = N->getNumOperands(); + LLVMContext &Context = unwrap(V)->getContext(); for (unsigned i = 0; i < numOperands; i++) - Dest[i] = wrap(N->getOperand(i)); + Dest[i] = getMDNodeOperandImpl(Context, N, i); } unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name) @@ -703,8 +796,9 @@ void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRe NamedMDNode *N = unwrap(M)->getNamedMetadata(name); if (!N) return; + LLVMContext &Context = unwrap(M)->getContext(); for (unsigned i=0;i<N->getNumOperands();i++) - Dest[i] = wrap(N->getOperand(i)); + Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i))); } void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name, @@ -713,9 +807,9 @@ void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name, NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name); if (!N) return; - MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr; - if (Op) - N->addOperand(Op); + if (!Val) + return; + N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val))); } /*--.. Operations on scalar constants ......................................--*/ @@ -767,6 +861,27 @@ long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) { return unwrap<ConstantInt>(ConstantVal)->getSExtValue(); } +double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) { + ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ; + Type *Ty = cFP->getType(); + + if (Ty->isFloatTy()) { + *LosesInfo = false; + return cFP->getValueAPF().convertToFloat(); + } + + if (Ty->isDoubleTy()) { + *LosesInfo = false; + return cFP->getValueAPF().convertToDouble(); + } + + bool APFLosesInfo; + APFloat APF = cFP->getValueAPF(); + APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &APFLosesInfo); + *LosesInfo = APFLosesInfo; + return APF.convertToDouble(); +} + /*--.. Operations on composite constants ...................................--*/ LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str, @@ -790,11 +905,27 @@ LLVMValueRef LLVMConstString(const char *Str, unsigned Length, return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length, DontNullTerminate); } + +LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef c, unsigned idx) { + return wrap(static_cast<ConstantDataSequential*>(unwrap(c))->getElementAsConstant(idx)); +} + +LLVMBool LLVMIsConstantString(LLVMValueRef c) { + return static_cast<ConstantDataSequential*>(unwrap(c))->isString(); +} + +const char *LLVMGetAsString(LLVMValueRef c, size_t* Length) { + StringRef str = static_cast<ConstantDataSequential*>(unwrap(c))->getAsString(); + *Length = str.size(); + return str.data(); +} + LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy, LLVMValueRef *ConstantVals, unsigned Length) { ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length); return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V)); } + LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count, LLVMBool Packed) { return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count, @@ -1859,12 +1990,27 @@ LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) { return (LLVMIntPredicate)0; } +LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) { + if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst))) + return (LLVMRealPredicate)I->getPredicate(); + if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) + if (CE->getOpcode() == Instruction::FCmp) + return (LLVMRealPredicate)CE->getPredicate(); + return (LLVMRealPredicate)0; +} + LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) { if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) return map_to_llvmopcode(C->getOpcode()); return (LLVMOpcode)0; } +LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) { + if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) + return wrap(C->clone()); + return nullptr; +} + /*--.. Call and invoke instructions ........................................--*/ unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { @@ -1926,6 +2072,34 @@ void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) { unwrap<CallInst>(Call)->setTailCall(isTailCall); } +/*--.. Operations on terminators ...........................................--*/ + +unsigned LLVMGetNumSuccessors(LLVMValueRef Term) { + return unwrap<TerminatorInst>(Term)->getNumSuccessors(); +} + +LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) { + return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i)); +} + +void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) { + return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block)); +} + +/*--.. Operations on branch instructions (only) ............................--*/ + +LLVMBool LLVMIsConditional(LLVMValueRef Branch) { + return unwrap<BranchInst>(Branch)->isConditional(); +} + +LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) { + return wrap(unwrap<BranchInst>(Branch)->getCondition()); +} + +void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) { + return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond)); +} + /*--.. Operations on switch instructions (only) ............................--*/ LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) { @@ -2005,13 +2179,16 @@ void LLVMDisposeBuilder(LLVMBuilderRef Builder) { /*--.. Metadata builders ...................................................--*/ void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) { - MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr; + MDNode *Loc = + L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr; unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc)); } LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) { - return wrap(unwrap(Builder)->getCurrentDebugLocation() - .getAsMDNode(unwrap(Builder)->getContext())); + LLVMContext &Context = unwrap(Builder)->getContext(); + return wrap(MetadataAsValue::get( + Context, + unwrap(Builder)->getCurrentDebugLocation().getAsMDNode(Context))); } void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) { @@ -2313,7 +2490,7 @@ static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) { case LLVMAtomicOrderingSequentiallyConsistent: return SequentiallyConsistent; } - + llvm_unreachable("Invalid LLVMAtomicOrdering value!"); } @@ -2632,10 +2809,9 @@ LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange( const char *BufferName, LLVMBool RequiresNullTerminator) { - return wrap(MemoryBuffer::getMemBuffer( - StringRef(InputData, InputDataLength), - StringRef(BufferName), - RequiresNullTerminator)); + return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength), + StringRef(BufferName), + RequiresNullTerminator).release()); } LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy( @@ -2643,9 +2819,9 @@ LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy( size_t InputDataLength, const char *BufferName) { - return wrap(MemoryBuffer::getMemBufferCopy( - StringRef(InputData, InputDataLength), - StringRef(BufferName))); + return wrap( + MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength), + StringRef(BufferName)).release()); } const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) { diff --git a/contrib/llvm/lib/IR/DIBuilder.cpp b/contrib/llvm/lib/IR/DIBuilder.cpp index 218787c..856bb3c 100644 --- a/contrib/llvm/lib/IR/DIBuilder.cpp +++ b/contrib/llvm/lib/IR/DIBuilder.cpp @@ -23,30 +23,64 @@ using namespace llvm; using namespace llvm::dwarf; -static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) { - assert((Tag & LLVMDebugVersionMask) == 0 && - "Tag too large for debug encoding!"); - return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion); +namespace { +class HeaderBuilder { + SmallVector<char, 256> Chars; + +public: + explicit HeaderBuilder(Twine T) { T.toVector(Chars); } + HeaderBuilder(const HeaderBuilder &X) : Chars(X.Chars) {} + HeaderBuilder(HeaderBuilder &&X) : Chars(std::move(X.Chars)) {} + + template <class Twineable> HeaderBuilder &concat(Twineable &&X) { + Chars.push_back(0); + Twine(X).toVector(Chars); + return *this; + } + + MDString *get(LLVMContext &Context) const { + return MDString::get(Context, StringRef(Chars.begin(), Chars.size())); + } + + static HeaderBuilder get(unsigned Tag) { + return HeaderBuilder("0x" + Twine::utohexstr(Tag)); + } +}; } -DIBuilder::DIBuilder(Module &m) +DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes) : M(m), VMContext(M.getContext()), TempEnumTypes(nullptr), TempRetainTypes(nullptr), TempSubprograms(nullptr), TempGVs(nullptr), - DeclareFn(nullptr), ValueFn(nullptr) {} + DeclareFn(nullptr), ValueFn(nullptr), + AllowUnresolvedNodes(AllowUnresolvedNodes) {} + +static bool isUnresolved(MDNode *N) { + return N && + (isa<MDNodeFwdDecl>(N) || !cast<UniquableMDNode>(N)->isResolved()); +} + +void DIBuilder::trackIfUnresolved(MDNode *N) { + if (!AllowUnresolvedNodes) { + assert(!isUnresolved(N) && "Cannot handle unresolved nodes"); + return; + } + if (isUnresolved(N)) + UnresolvedNodes.emplace_back(N); + return; +} -/// finalize - Construct any deferred debug info descriptors. void DIBuilder::finalize() { DIArray Enums = getOrCreateArray(AllEnumTypes); DIType(TempEnumTypes).replaceAllUsesWith(Enums); - SmallVector<Value *, 16> RetainValues; + SmallVector<Metadata *, 16> RetainValues; // Declarations and definitions of the same type may be retained. Some // clients RAUW these pairs, leaving duplicates in the retained types // list. Use a set to remove the duplicates while we transform the // TrackingVHs back into Values. - SmallPtrSet<Value *, 16> RetainSet; + SmallPtrSet<Metadata *, 16> RetainSet; for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++) - if (RetainSet.insert(AllRetainTypes[I])) + if (RetainSet.insert(AllRetainTypes[I]).second) RetainValues.push_back(AllRetainTypes[I]); DIArray RetainTypes = getOrCreateArray(RetainValues); DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes); @@ -55,13 +89,10 @@ void DIBuilder::finalize() { DIType(TempSubprograms).replaceAllUsesWith(SPs); for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) { DISubprogram SP(SPs.getElement(i)); - SmallVector<Value *, 4> Variables; - if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) { - for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii) - Variables.push_back(NMD->getOperand(ii)); - NMD->eraseFromParent(); - } if (MDNode *Temp = SP.getVariablesNodes()) { + SmallVector<Metadata *, 4> Variables; + for (Metadata *V : PreservedVariables.lookup(SP)) + Variables.push_back(V); DIArray AV = getOrCreateArray(Variables); DIType(Temp).replaceAllUsesWith(AV); } @@ -70,15 +101,24 @@ void DIBuilder::finalize() { DIArray GVs = getOrCreateArray(AllGVs); DIType(TempGVs).replaceAllUsesWith(GVs); - SmallVector<Value *, 16> RetainValuesI; + SmallVector<Metadata *, 16> RetainValuesI; for (unsigned I = 0, E = AllImportedModules.size(); I < E; I++) RetainValuesI.push_back(AllImportedModules[I]); DIArray IMs = getOrCreateArray(RetainValuesI); DIType(TempImportedModules).replaceAllUsesWith(IMs); + + // Now that all temp nodes have been replaced or deleted, resolve remaining + // cycles. + for (const auto &N : UnresolvedNodes) + if (N) + cast<UniquableMDNode>(N)->resolveCycles(); + UnresolvedNodes.clear(); + + // Can't handle unresolved nodes anymore. + AllowUnresolvedNodes = false; } -/// getNonCompileUnitScope - If N is compile unit return NULL otherwise return -/// N. +/// If N is compile unit return NULL otherwise return N. static MDNode *getNonCompileUnitScope(MDNode *N) { if (DIDescriptor(N).isCompileUnit()) return nullptr; @@ -88,15 +128,11 @@ static MDNode *getNonCompileUnitScope(MDNode *N) { static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename, StringRef Directory) { assert(!Filename.empty() && "Unable to create file without name"); - Value *Pair[] = { - MDString::get(VMContext, Filename), - MDString::get(VMContext, Directory) - }; + Metadata *Pair[] = {MDString::get(VMContext, Filename), + MDString::get(VMContext, Directory)}; return MDNode::get(VMContext, Pair); } -/// createCompileUnit - A CompileUnit provides an anchor for all debugging -/// information generated during this instance of compilation. DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename, StringRef Directory, StringRef Producer, bool isOptimized, @@ -110,7 +146,7 @@ DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename, "Invalid Language tag"); assert(!Filename.empty() && "Unable to create compile unit without filename"); - Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) }; + Metadata *TElts[] = {HeaderBuilder::get(DW_TAG_base_type).get(VMContext)}; TempEnumTypes = MDNode::getTemporary(VMContext, TElts); TempRetainTypes = MDNode::getTemporary(VMContext, TElts); @@ -121,22 +157,18 @@ DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename, TempImportedModules = MDNode::getTemporary(VMContext, TElts); - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit), - createFilePathPair(VMContext, Filename, Directory), - ConstantInt::get(Type::getInt32Ty(VMContext), Lang), - MDString::get(VMContext, Producer), - ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized), - MDString::get(VMContext, Flags), - ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer), - TempEnumTypes, - TempRetainTypes, - TempSubprograms, - TempGVs, - TempImportedModules, - MDString::get(VMContext, SplitName), - ConstantInt::get(Type::getInt32Ty(VMContext), Kind) - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_compile_unit) + .concat(Lang) + .concat(Producer) + .concat(isOptimized) + .concat(Flags) + .concat(RunTimeVer) + .concat(SplitName) + .concat(Kind) + .get(VMContext), + createFilePathPair(VMContext, Filename, Directory), + TempEnumTypes, TempRetainTypes, TempSubprograms, TempGVs, + TempImportedModules}; MDNode *CUNode = MDNode::get(VMContext, Elts); @@ -150,35 +182,21 @@ DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename, NMD->addOperand(CUNode); } + trackIfUnresolved(CUNode); return DICompileUnit(CUNode); } static DIImportedEntity createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope Context, - Value *NS, unsigned Line, StringRef Name, - SmallVectorImpl<TrackingVH<MDNode>> &AllImportedModules) { + Metadata *NS, unsigned Line, StringRef Name, + SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) { const MDNode *R; - if (Name.empty()) { - Value *Elts[] = { - GetTagConstant(C, Tag), - Context, - NS, - ConstantInt::get(Type::getInt32Ty(C), Line), - }; - R = MDNode::get(C, Elts); - } else { - Value *Elts[] = { - GetTagConstant(C, Tag), - Context, - NS, - ConstantInt::get(Type::getInt32Ty(C), Line), - MDString::get(C, Name) - }; - R = MDNode::get(C, Elts); - } + Metadata *Elts[] = {HeaderBuilder::get(Tag).concat(Line).concat(Name).get(C), + Context, NS}; + R = MDNode::get(C, Elts); DIImportedEntity M(R); assert(M.Verify() && "Imported module should be valid"); - AllImportedModules.push_back(TrackingVH<MDNode>(M)); + AllImportedModules.emplace_back(M.get()); return M; } @@ -197,10 +215,14 @@ DIImportedEntity DIBuilder::createImportedModule(DIScope Context, } DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context, - DIScope Decl, + DIDescriptor Decl, unsigned Line, StringRef Name) { + // Make sure to use the unique identifier based metadata reference for + // types that have one. + Metadata *V = + Decl.isType() ? static_cast<Metadata *>(DIType(Decl).getRef()) : Decl; return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration, - Context, Decl.getRef(), Line, Name, + Context, V, Line, Name, AllImportedModules); } @@ -211,215 +233,187 @@ DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context, Context, Imp, Line, Name, AllImportedModules); } -/// createFile - Create a file descriptor to hold debugging information -/// for a file. DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) { - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_file_type), - createFilePathPair(VMContext, Filename, Directory) - }; + Metadata *Elts[] = { + HeaderBuilder::get(dwarf::DW_TAG_file_type).get(VMContext), + createFilePathPair(VMContext, Filename, Directory)}; return DIFile(MDNode::get(VMContext, Elts)); } -/// createEnumerator - Create a single enumerator value. DIEnumerator DIBuilder::createEnumerator(StringRef Name, int64_t Val) { assert(!Name.empty() && "Unable to create enumerator without name"); - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_enumerator), - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt64Ty(VMContext), Val) - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_enumerator) + .concat(Name) + .concat(Val) + .get(VMContext)}; return DIEnumerator(MDNode::get(VMContext, Elts)); } -/// \brief Create a DWARF unspecified type. DIBasicType DIBuilder::createUnspecifiedType(StringRef Name) { assert(!Name.empty() && "Unable to create type without name"); // Unspecified types are encoded in DIBasicType format. Line number, filename, // size, alignment, offset and flags are always empty here. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type), - nullptr, // Filename - nullptr, // Unused - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags; - ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding + Metadata *Elts[] = { + HeaderBuilder::get(dwarf::DW_TAG_unspecified_type) + .concat(Name) + .concat(0) + .concat(0) + .concat(0) + .concat(0) + .concat(0) + .concat(0) + .get(VMContext), + nullptr, // Filename + nullptr // Unused }; return DIBasicType(MDNode::get(VMContext, Elts)); } -/// \brief Create C++11 nullptr type. DIBasicType DIBuilder::createNullPtrType() { return createUnspecifiedType("decltype(nullptr)"); } -/// createBasicType - Create debugging information entry for a basic -/// type, e.g 'char'. DIBasicType DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits, uint64_t AlignInBits, unsigned Encoding) { assert(!Name.empty() && "Unable to create type without name"); // Basic types are encoded in DIBasicType format. Line number, filename, // offset and flags are always empty here. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_base_type), - nullptr, // File/directory name - nullptr, // Unused - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line - ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags; - ConstantInt::get(Type::getInt32Ty(VMContext), Encoding) + Metadata *Elts[] = { + HeaderBuilder::get(dwarf::DW_TAG_base_type) + .concat(Name) + .concat(0) // Line + .concat(SizeInBits) + .concat(AlignInBits) + .concat(0) // Offset + .concat(0) // Flags + .concat(Encoding) + .get(VMContext), + nullptr, // Filename + nullptr // Unused }; return DIBasicType(MDNode::get(VMContext, Elts)); } -/// createQualifiedType - Create debugging information entry for a qualified -/// type, e.g. 'const int'. DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) { // Qualified types are encoded in DIDerivedType format. - Value *Elts[] = { - GetTagConstant(VMContext, Tag), - nullptr, // Filename - nullptr, // Unused - MDString::get(VMContext, StringRef()), // Empty name. - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags - FromTy.getRef() - }; + Metadata *Elts[] = {HeaderBuilder::get(Tag) + .concat(StringRef()) // Name + .concat(0) // Line + .concat(0) // Size + .concat(0) // Align + .concat(0) // Offset + .concat(0) // Flags + .get(VMContext), + nullptr, // Filename + nullptr, // Unused + FromTy.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } -/// createPointerType - Create debugging information entry for a pointer. DIDerivedType DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits, uint64_t AlignInBits, StringRef Name) { // Pointer types are encoded in DIDerivedType format. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type), - nullptr, // Filename - nullptr, // Unused - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line - ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags - PointeeTy.getRef() - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_pointer_type) + .concat(Name) + .concat(0) // Line + .concat(SizeInBits) + .concat(AlignInBits) + .concat(0) // Offset + .concat(0) // Flags + .get(VMContext), + nullptr, // Filename + nullptr, // Unused + PointeeTy.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } -DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy, - DIType Base) { +DIDerivedType +DIBuilder::createMemberPointerType(DIType PointeeTy, DIType Base, + uint64_t SizeInBits, uint64_t AlignInBits) { // Pointer types are encoded in DIDerivedType format. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type), - nullptr, // Filename - nullptr, // Unused - nullptr, - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags - PointeeTy.getRef(), - Base.getRef() - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_ptr_to_member_type) + .concat(StringRef()) + .concat(0) // Line + .concat(SizeInBits) // Size + .concat(AlignInBits) // Align + .concat(0) // Offset + .concat(0) // Flags + .get(VMContext), + nullptr, // Filename + nullptr, // Unused + PointeeTy.getRef(), Base.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } -/// createReferenceType - Create debugging information entry for a reference -/// type. DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) { assert(RTy.isType() && "Unable to create reference type"); // References are encoded in DIDerivedType format. - Value *Elts[] = { - GetTagConstant(VMContext, Tag), - nullptr, // Filename - nullptr, // TheCU, - nullptr, // Name - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags - RTy.getRef() - }; + Metadata *Elts[] = {HeaderBuilder::get(Tag) + .concat(StringRef()) // Name + .concat(0) // Line + .concat(0) // Size + .concat(0) // Align + .concat(0) // Offset + .concat(0) // Flags + .get(VMContext), + nullptr, // Filename + nullptr, // TheCU, + RTy.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } -/// createTypedef - Create debugging information entry for a typedef. DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File, unsigned LineNo, DIDescriptor Context) { // typedefs are encoded in DIDerivedType format. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_typedef), - File.getFileNode(), - DIScope(getNonCompileUnitScope(Context)).getRef(), - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), LineNo), - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags - Ty.getRef() - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_typedef) + .concat(Name) + .concat(LineNo) + .concat(0) // Size + .concat(0) // Align + .concat(0) // Offset + .concat(0) // Flags + .get(VMContext), + File.getFileNode(), + DIScope(getNonCompileUnitScope(Context)).getRef(), + Ty.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } -/// createFriend - Create debugging information entry for a 'friend'. DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) { // typedefs are encoded in DIDerivedType format. assert(Ty.isType() && "Invalid type!"); assert(FriendTy.isType() && "Invalid friend type!"); - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_friend), - nullptr, - Ty.getRef(), - nullptr, // Name - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags - FriendTy.getRef() - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_friend) + .concat(StringRef()) // Name + .concat(0) // Line + .concat(0) // Size + .concat(0) // Align + .concat(0) // Offset + .concat(0) // Flags + .get(VMContext), + nullptr, Ty.getRef(), FriendTy.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } -/// createInheritance - Create debugging information entry to establish -/// inheritance relationship between two types. DIDerivedType DIBuilder::createInheritance(DIType Ty, DIType BaseTy, uint64_t BaseOffset, unsigned Flags) { assert(Ty.isType() && "Unable to create inheritance"); // TAG_inheritance is encoded in DIDerivedType format. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_inheritance), - nullptr, - Ty.getRef(), - nullptr, // Name - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align - ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset), - ConstantInt::get(Type::getInt32Ty(VMContext), Flags), - BaseTy.getRef() - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_inheritance) + .concat(StringRef()) // Name + .concat(0) // Line + .concat(0) // Size + .concat(0) // Align + .concat(BaseOffset) + .concat(Flags) + .get(VMContext), + nullptr, Ty.getRef(), BaseTy.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } -/// createMemberType - Create debugging information entry for a member. DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber, uint64_t SizeInBits, @@ -427,76 +421,47 @@ DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name, uint64_t OffsetInBits, unsigned Flags, DIType Ty) { // TAG_member is encoded in DIDerivedType format. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_member), - File.getFileNode(), - DIScope(getNonCompileUnitScope(Scope)).getRef(), - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), - ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits), - ConstantInt::get(Type::getInt32Ty(VMContext), Flags), - Ty.getRef() - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member) + .concat(Name) + .concat(LineNumber) + .concat(SizeInBits) + .concat(AlignInBits) + .concat(OffsetInBits) + .concat(Flags) + .get(VMContext), + File.getFileNode(), + DIScope(getNonCompileUnitScope(Scope)).getRef(), + Ty.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } -/// createStaticMemberType - Create debugging information entry for a -/// C++ static data member. -DIDerivedType -DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name, - DIFile File, unsigned LineNumber, - DIType Ty, unsigned Flags, - llvm::Value *Val) { - // TAG_member is encoded in DIDerivedType format. - Flags |= DIDescriptor::FlagStaticMember; - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_member), - File.getFileNode(), - DIScope(getNonCompileUnitScope(Scope)).getRef(), - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), Flags), - Ty.getRef(), - Val - }; - return DIDerivedType(MDNode::get(VMContext, Elts)); +static Metadata *getConstantOrNull(Constant *C) { + if (C) + return ConstantAsMetadata::get(C); + return nullptr; } -/// createObjCIVar - Create debugging information entry for Objective-C -/// instance variable. -DIDerivedType -DIBuilder::createObjCIVar(StringRef Name, DIFile File, unsigned LineNumber, - uint64_t SizeInBits, uint64_t AlignInBits, - uint64_t OffsetInBits, unsigned Flags, DIType Ty, - StringRef PropertyName, StringRef GetterName, - StringRef SetterName, unsigned PropertyAttributes) { +DIDerivedType DIBuilder::createStaticMemberType(DIDescriptor Scope, + StringRef Name, DIFile File, + unsigned LineNumber, DIType Ty, + unsigned Flags, + llvm::Constant *Val) { // TAG_member is encoded in DIDerivedType format. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_member), - File.getFileNode(), - getNonCompileUnitScope(File), - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), - ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits), - ConstantInt::get(Type::getInt32Ty(VMContext), Flags), - Ty, - MDString::get(VMContext, PropertyName), - MDString::get(VMContext, GetterName), - MDString::get(VMContext, SetterName), - ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes) - }; + Flags |= DIDescriptor::FlagStaticMember; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member) + .concat(Name) + .concat(LineNumber) + .concat(0) // Size + .concat(0) // Align + .concat(0) // Offset + .concat(Flags) + .get(VMContext), + File.getFileNode(), + DIScope(getNonCompileUnitScope(Scope)).getRef(), + Ty.getRef(), getConstantOrNull(Val)}; return DIDerivedType(MDNode::get(VMContext, Elts)); } -/// createObjCIVar - Create debugging information entry for Objective-C -/// instance variable. DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File, unsigned LineNumber, uint64_t SizeInBits, @@ -504,88 +469,65 @@ DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File, uint64_t OffsetInBits, unsigned Flags, DIType Ty, MDNode *PropertyNode) { // TAG_member is encoded in DIDerivedType format. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_member), - File.getFileNode(), - getNonCompileUnitScope(File), - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), - ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits), - ConstantInt::get(Type::getInt32Ty(VMContext), Flags), - Ty, - PropertyNode - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member) + .concat(Name) + .concat(LineNumber) + .concat(SizeInBits) + .concat(AlignInBits) + .concat(OffsetInBits) + .concat(Flags) + .get(VMContext), + File.getFileNode(), getNonCompileUnitScope(File), Ty, + PropertyNode}; return DIDerivedType(MDNode::get(VMContext, Elts)); } -/// createObjCProperty - Create debugging information entry for Objective-C -/// property. DIObjCProperty DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber, StringRef GetterName, StringRef SetterName, unsigned PropertyAttributes, DIType Ty) { - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property), - MDString::get(VMContext, Name), - File, - ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), - MDString::get(VMContext, GetterName), - MDString::get(VMContext, SetterName), - ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes), - Ty - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_APPLE_property) + .concat(Name) + .concat(LineNumber) + .concat(GetterName) + .concat(SetterName) + .concat(PropertyAttributes) + .get(VMContext), + File, Ty}; return DIObjCProperty(MDNode::get(VMContext, Elts)); } -/// createTemplateTypeParameter - Create debugging information for template -/// type parameter. DITemplateTypeParameter DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name, DIType Ty, MDNode *File, unsigned LineNo, unsigned ColumnNo) { - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter), - DIScope(getNonCompileUnitScope(Context)).getRef(), - MDString::get(VMContext, Name), - Ty.getRef(), - File, - ConstantInt::get(Type::getInt32Ty(VMContext), LineNo), - ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo) - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_template_type_parameter) + .concat(Name) + .concat(LineNo) + .concat(ColumnNo) + .get(VMContext), + DIScope(getNonCompileUnitScope(Context)).getRef(), + Ty.getRef(), File}; return DITemplateTypeParameter(MDNode::get(VMContext, Elts)); } -DITemplateValueParameter -DIBuilder::createTemplateValueParameter(unsigned Tag, DIDescriptor Context, - StringRef Name, DIType Ty, - Value *Val, MDNode *File, - unsigned LineNo, - unsigned ColumnNo) { - Value *Elts[] = { - GetTagConstant(VMContext, Tag), - DIScope(getNonCompileUnitScope(Context)).getRef(), - MDString::get(VMContext, Name), - Ty.getRef(), - Val, - File, - ConstantInt::get(Type::getInt32Ty(VMContext), LineNo), - ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo) - }; +static DITemplateValueParameter createTemplateValueParameterHelper( + LLVMContext &VMContext, unsigned Tag, DIDescriptor Context, StringRef Name, + DIType Ty, Metadata *MD, MDNode *File, unsigned LineNo, unsigned ColumnNo) { + Metadata *Elts[] = { + HeaderBuilder::get(Tag).concat(Name).concat(LineNo).concat(ColumnNo).get( + VMContext), + DIScope(getNonCompileUnitScope(Context)).getRef(), Ty.getRef(), MD, File}; return DITemplateValueParameter(MDNode::get(VMContext, Elts)); } -/// createTemplateValueParameter - Create debugging information for template -/// value parameter. DITemplateValueParameter DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name, - DIType Ty, Value *Val, - MDNode *File, unsigned LineNo, - unsigned ColumnNo) { - return createTemplateValueParameter(dwarf::DW_TAG_template_value_parameter, - Context, Name, Ty, Val, File, LineNo, - ColumnNo); + DIType Ty, Constant *Val, MDNode *File, + unsigned LineNo, unsigned ColumnNo) { + return createTemplateValueParameterHelper( + VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty, + getConstantOrNull(Val), File, LineNo, ColumnNo); } DITemplateValueParameter @@ -593,8 +535,8 @@ DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name, DIType Ty, StringRef Val, MDNode *File, unsigned LineNo, unsigned ColumnNo) { - return createTemplateValueParameter( - dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty, + return createTemplateValueParameterHelper( + VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty, MDString::get(VMContext, Val), File, LineNo, ColumnNo); } @@ -603,12 +545,11 @@ DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name, DIType Ty, DIArray Val, MDNode *File, unsigned LineNo, unsigned ColumnNo) { - return createTemplateValueParameter(dwarf::DW_TAG_GNU_template_parameter_pack, - Context, Name, Ty, Val, File, LineNo, - ColumnNo); + return createTemplateValueParameterHelper( + VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty, + Val, File, LineNo, ColumnNo); } -/// createClassType - Create debugging information entry for a class. DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name, DIFile File, unsigned LineNumber, uint64_t SizeInBits, @@ -622,24 +563,20 @@ DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name, assert((!Context || Context.isScope() || Context.isType()) && "createClassType should be called with a valid Context"); // TAG_class_type is encoded in DICompositeType format. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_class_type), - File.getFileNode(), - DIScope(getNonCompileUnitScope(Context)).getRef(), - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), - ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), - ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits), - ConstantInt::get(Type::getInt32Ty(VMContext), Flags), - DerivedFrom.getRef(), - Elements, - ConstantInt::get(Type::getInt32Ty(VMContext), 0), - VTableHolder.getRef(), - TemplateParams, - UniqueIdentifier.empty() ? nullptr - : MDString::get(VMContext, UniqueIdentifier) - }; + Metadata *Elts[] = { + HeaderBuilder::get(dwarf::DW_TAG_class_type) + .concat(Name) + .concat(LineNumber) + .concat(SizeInBits) + .concat(AlignInBits) + .concat(OffsetInBits) + .concat(Flags) + .concat(0) + .get(VMContext), + File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(), + DerivedFrom.getRef(), Elements, VTableHolder.getRef(), TemplateParams, + UniqueIdentifier.empty() ? nullptr + : MDString::get(VMContext, UniqueIdentifier)}; DICompositeType R(MDNode::get(VMContext, Elts)); assert(R.isCompositeType() && "createClassType should return a DICompositeType"); @@ -648,7 +585,6 @@ DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name, return R; } -/// createStructType - Create debugging information entry for a struct. DICompositeType DIBuilder::createStructType(DIDescriptor Context, StringRef Name, DIFile File, unsigned LineNumber, @@ -660,24 +596,20 @@ DICompositeType DIBuilder::createStructType(DIDescriptor Context, DIType VTableHolder, StringRef UniqueIdentifier) { // TAG_structure_type is encoded in DICompositeType format. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_structure_type), - File.getFileNode(), - DIScope(getNonCompileUnitScope(Context)).getRef(), - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), - ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), - ConstantInt::get(Type::getInt32Ty(VMContext), Flags), - DerivedFrom.getRef(), - Elements, - ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang), - VTableHolder.getRef(), - nullptr, - UniqueIdentifier.empty() ? nullptr - : MDString::get(VMContext, UniqueIdentifier) - }; + Metadata *Elts[] = { + HeaderBuilder::get(dwarf::DW_TAG_structure_type) + .concat(Name) + .concat(LineNumber) + .concat(SizeInBits) + .concat(AlignInBits) + .concat(0) + .concat(Flags) + .concat(RunTimeLang) + .get(VMContext), + File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(), + DerivedFrom.getRef(), Elements, VTableHolder.getRef(), nullptr, + UniqueIdentifier.empty() ? nullptr + : MDString::get(VMContext, UniqueIdentifier)}; DICompositeType R(MDNode::get(VMContext, Elts)); assert(R.isCompositeType() && "createStructType should return a DICompositeType"); @@ -686,7 +618,6 @@ DICompositeType DIBuilder::createStructType(DIDescriptor Context, return R; } -/// createUnionType - Create debugging information entry for an union. DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber, uint64_t SizeInBits, @@ -695,80 +626,65 @@ DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name, unsigned RunTimeLang, StringRef UniqueIdentifier) { // TAG_union_type is encoded in DICompositeType format. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_union_type), - File.getFileNode(), - DIScope(getNonCompileUnitScope(Scope)).getRef(), - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), - ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), Flags), - nullptr, - Elements, - ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang), - nullptr, - nullptr, - UniqueIdentifier.empty() ? nullptr - : MDString::get(VMContext, UniqueIdentifier) - }; + Metadata *Elts[] = { + HeaderBuilder::get(dwarf::DW_TAG_union_type) + .concat(Name) + .concat(LineNumber) + .concat(SizeInBits) + .concat(AlignInBits) + .concat(0) // Offset + .concat(Flags) + .concat(RunTimeLang) + .get(VMContext), + File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), + nullptr, Elements, nullptr, nullptr, + UniqueIdentifier.empty() ? nullptr + : MDString::get(VMContext, UniqueIdentifier)}; DICompositeType R(MDNode::get(VMContext, Elts)); if (!UniqueIdentifier.empty()) retainType(R); return R; } -/// createSubroutineType - Create subroutine type. -DICompositeType DIBuilder::createSubroutineType(DIFile File, - DIArray ParameterTypes, - unsigned Flags) { +DISubroutineType DIBuilder::createSubroutineType(DIFile File, + DITypeArray ParameterTypes, + unsigned Flags) { // TAG_subroutine_type is encoded in DICompositeType format. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type), - Constant::getNullValue(Type::getInt32Ty(VMContext)), - nullptr, - MDString::get(VMContext, ""), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align - ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), Flags), // Flags - nullptr, - ParameterTypes, - ConstantInt::get(Type::getInt32Ty(VMContext), 0), - nullptr, - nullptr, - nullptr // Type Identifer + Metadata *Elts[] = { + HeaderBuilder::get(dwarf::DW_TAG_subroutine_type) + .concat(StringRef()) + .concat(0) // Line + .concat(0) // Size + .concat(0) // Align + .concat(0) // Offset + .concat(Flags) // Flags + .concat(0) + .get(VMContext), + nullptr, nullptr, nullptr, ParameterTypes, nullptr, nullptr, + nullptr // Type Identifer }; - return DICompositeType(MDNode::get(VMContext, Elts)); + return DISubroutineType(MDNode::get(VMContext, Elts)); } -/// createEnumerationType - Create debugging information entry for an -/// enumeration. DICompositeType DIBuilder::createEnumerationType( DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber, uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements, DIType UnderlyingType, StringRef UniqueIdentifier) { // TAG_enumeration_type is encoded in DICompositeType format. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type), - File.getFileNode(), - DIScope(getNonCompileUnitScope(Scope)).getRef(), - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), - ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags - UnderlyingType.getRef(), - Elements, - ConstantInt::get(Type::getInt32Ty(VMContext), 0), - nullptr, - nullptr, - UniqueIdentifier.empty() ? nullptr - : MDString::get(VMContext, UniqueIdentifier) - }; + Metadata *Elts[] = { + HeaderBuilder::get(dwarf::DW_TAG_enumeration_type) + .concat(Name) + .concat(LineNumber) + .concat(SizeInBits) + .concat(AlignInBits) + .concat(0) // Offset + .concat(0) // Flags + .concat(0) + .get(VMContext), + File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), + UnderlyingType.getRef(), Elements, nullptr, nullptr, + UniqueIdentifier.empty() ? nullptr + : MDString::get(VMContext, UniqueIdentifier)}; DICompositeType CTy(MDNode::get(VMContext, Elts)); AllEnumTypes.push_back(CTy); if (!UniqueIdentifier.empty()) @@ -776,138 +692,115 @@ DICompositeType DIBuilder::createEnumerationType( return CTy; } -/// createArrayType - Create debugging information entry for an array. DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits, DIType Ty, DIArray Subscripts) { // TAG_array_type is encoded in DICompositeType format. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_array_type), - nullptr, // Filename/Directory, - nullptr, // Unused - MDString::get(VMContext, ""), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line - ConstantInt::get(Type::getInt64Ty(VMContext), Size), - ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags - Ty.getRef(), - Subscripts, - ConstantInt::get(Type::getInt32Ty(VMContext), 0), - nullptr, - nullptr, - nullptr // Type Identifer + Metadata *Elts[] = { + HeaderBuilder::get(dwarf::DW_TAG_array_type) + .concat(StringRef()) + .concat(0) // Line + .concat(Size) + .concat(AlignInBits) + .concat(0) // Offset + .concat(0) // Flags + .concat(0) + .get(VMContext), + nullptr, // Filename/Directory, + nullptr, // Unused + Ty.getRef(), Subscripts, nullptr, nullptr, + nullptr // Type Identifer }; return DICompositeType(MDNode::get(VMContext, Elts)); } -/// createVectorType - Create debugging information entry for a vector. DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits, DIType Ty, DIArray Subscripts) { // A vector is an array type with the FlagVector flag applied. - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_array_type), - nullptr, // Filename/Directory, - nullptr, // Unused - MDString::get(VMContext, ""), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line - ConstantInt::get(Type::getInt64Ty(VMContext), Size), - ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector), - Ty.getRef(), - Subscripts, - ConstantInt::get(Type::getInt32Ty(VMContext), 0), - nullptr, - nullptr, - nullptr // Type Identifer + Metadata *Elts[] = { + HeaderBuilder::get(dwarf::DW_TAG_array_type) + .concat("") + .concat(0) // Line + .concat(Size) + .concat(AlignInBits) + .concat(0) // Offset + .concat(DIType::FlagVector) + .concat(0) + .get(VMContext), + nullptr, // Filename/Directory, + nullptr, // Unused + Ty.getRef(), Subscripts, nullptr, nullptr, + nullptr // Type Identifer }; return DICompositeType(MDNode::get(VMContext, Elts)); } -/// createArtificialType - Create a new DIType with "artificial" flag set. -DIType DIBuilder::createArtificialType(DIType Ty) { - if (Ty.isArtificial()) - return Ty; +static HeaderBuilder setTypeFlagsInHeader(StringRef Header, + unsigned FlagsToSet) { + DIHeaderFieldIterator I(Header); + std::advance(I, 6); - SmallVector<Value *, 9> Elts; - MDNode *N = Ty; - assert (N && "Unexpected input DIType!"); - for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) - Elts.push_back(N->getOperand(i)); + unsigned Flags; + if (I->getAsInteger(0, Flags)) + Flags = 0; + Flags |= FlagsToSet; - unsigned CurFlags = Ty.getFlags(); - CurFlags = CurFlags | DIType::FlagArtificial; + return HeaderBuilder(Twine(I.getPrefix())).concat(Flags).concat( + I.getSuffix()); +} - // Flags are stored at this slot. - // FIXME: Add an enum for this magic value. - Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags); +static DIType createTypeWithFlags(LLVMContext &Context, DIType Ty, + unsigned FlagsToSet) { + SmallVector<Metadata *, 9> Elts; + MDNode *N = Ty; + assert(N && "Unexpected input DIType!"); + // Update header field. + Elts.push_back(setTypeFlagsInHeader(Ty.getHeader(), FlagsToSet).get(Context)); + for (unsigned I = 1, E = N->getNumOperands(); I != E; ++I) + Elts.push_back(N->getOperand(I)); - return DIType(MDNode::get(VMContext, Elts)); + return DIType(MDNode::get(Context, Elts)); +} + +DIType DIBuilder::createArtificialType(DIType Ty) { + if (Ty.isArtificial()) + return Ty; + return createTypeWithFlags(VMContext, Ty, DIType::FlagArtificial); } -/// createObjectPointerType - Create a new type with both the object pointer -/// and artificial flags set. DIType DIBuilder::createObjectPointerType(DIType Ty) { if (Ty.isObjectPointer()) return Ty; - - SmallVector<Value *, 9> Elts; - MDNode *N = Ty; - assert (N && "Unexpected input DIType!"); - for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) - Elts.push_back(N->getOperand(i)); - - unsigned CurFlags = Ty.getFlags(); - CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial); - - // Flags are stored at this slot. - // FIXME: Add an enum for this magic value. - Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags); - - return DIType(MDNode::get(VMContext, Elts)); + unsigned Flags = DIType::FlagObjectPointer | DIType::FlagArtificial; + return createTypeWithFlags(VMContext, Ty, Flags); } -/// retainType - Retain DIType in a module even if it is not referenced -/// through debug info anchors. -void DIBuilder::retainType(DIType T) { - AllRetainTypes.push_back(TrackingVH<MDNode>(T)); -} +void DIBuilder::retainType(DIType T) { AllRetainTypes.emplace_back(T); } -/// createUnspecifiedParameter - Create unspeicified type descriptor -/// for the subroutine type. -DIDescriptor DIBuilder::createUnspecifiedParameter() { - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters) - }; - return DIDescriptor(MDNode::get(VMContext, Elts)); +DIBasicType DIBuilder::createUnspecifiedParameter() { + return DIBasicType(); } -/// createForwardDecl - Create a temporary forward-declared type that -/// can be RAUW'd if the full type is seen. DICompositeType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits, StringRef UniqueIdentifier) { // Create a temporary MDNode. - Value *Elts[] = { - GetTagConstant(VMContext, Tag), - F.getFileNode(), - DIScope(getNonCompileUnitScope(Scope)).getRef(), - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), Line), - ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl), - nullptr, - DIArray(), - ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang), - nullptr, - nullptr, //TemplateParams - UniqueIdentifier.empty() ? nullptr - : MDString::get(VMContext, UniqueIdentifier) - }; + Metadata *Elts[] = { + HeaderBuilder::get(Tag) + .concat(Name) + .concat(Line) + .concat(SizeInBits) + .concat(AlignInBits) + .concat(0) // Offset + .concat(DIDescriptor::FlagFwdDecl) + .concat(RuntimeLang) + .get(VMContext), + F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr, + DIArray(), nullptr, + nullptr, // TemplateParams + UniqueIdentifier.empty() ? nullptr + : MDString::get(VMContext, UniqueIdentifier)}; MDNode *Node = MDNode::get(VMContext, Elts); DICompositeType RetTy(Node); assert(RetTy.isCompositeType() && @@ -917,123 +810,108 @@ DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope, return RetTy; } -/// createForwardDecl - Create a temporary forward-declared type that -/// can be RAUW'd if the full type is seen. DICompositeType DIBuilder::createReplaceableForwardDecl( unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits, StringRef UniqueIdentifier) { // Create a temporary MDNode. - Value *Elts[] = { - GetTagConstant(VMContext, Tag), - F.getFileNode(), - DIScope(getNonCompileUnitScope(Scope)).getRef(), - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), Line), - ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), - ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset - ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl), - nullptr, - DIArray(), - ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang), - nullptr, - nullptr, //TemplateParams - UniqueIdentifier.empty() ? nullptr - : MDString::get(VMContext, UniqueIdentifier) - }; - MDNode *Node = MDNode::getTemporary(VMContext, Elts); - DICompositeType RetTy(Node); + Metadata *Elts[] = { + HeaderBuilder::get(Tag) + .concat(Name) + .concat(Line) + .concat(SizeInBits) + .concat(AlignInBits) + .concat(0) // Offset + .concat(DIDescriptor::FlagFwdDecl) + .concat(RuntimeLang) + .get(VMContext), + F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr, + DIArray(), nullptr, + nullptr, // TemplateParams + UniqueIdentifier.empty() ? nullptr + : MDString::get(VMContext, UniqueIdentifier)}; + DICompositeType RetTy(MDNode::getTemporary(VMContext, Elts)); assert(RetTy.isCompositeType() && - "createForwardDecl result should be a DIType"); + "createReplaceableForwardDecl result should be a DIType"); if (!UniqueIdentifier.empty()) retainType(RetTy); return RetTy; } -/// getOrCreateArray - Get a DIArray, create one if required. -DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) { +DIArray DIBuilder::getOrCreateArray(ArrayRef<Metadata *> Elements) { return DIArray(MDNode::get(VMContext, Elements)); } -/// getOrCreateSubrange - Create a descriptor for a value range. This -/// implicitly uniques the values returned. +DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef<Metadata *> Elements) { + SmallVector<llvm::Metadata *, 16> Elts; + for (unsigned i = 0, e = Elements.size(); i != e; ++i) { + if (Elements[i] && isa<MDNode>(Elements[i])) + Elts.push_back(DIType(cast<MDNode>(Elements[i])).getRef()); + else + Elts.push_back(Elements[i]); + } + return DITypeArray(MDNode::get(VMContext, Elts)); +} + DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) { - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type), - ConstantInt::get(Type::getInt64Ty(VMContext), Lo), - ConstantInt::get(Type::getInt64Ty(VMContext), Count) - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subrange_type) + .concat(Lo) + .concat(Count) + .get(VMContext)}; return DISubrange(MDNode::get(VMContext, Elts)); } -/// \brief Create a new descriptor for the specified global. -DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name, - StringRef LinkageName, - DIFile F, unsigned LineNumber, - DITypeRef Ty, bool isLocalToUnit, - Value *Val) { - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_variable), - Constant::getNullValue(Type::getInt32Ty(VMContext)), - nullptr, // TheCU, - MDString::get(VMContext, Name), - MDString::get(VMContext, Name), - MDString::get(VMContext, LinkageName), - F, - ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), - Ty, - ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit), - ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/ - Val, - DIDescriptor() - }; - MDNode *Node = MDNode::get(VMContext, Elts); - AllGVs.push_back(Node); - return DIGlobalVariable(Node); -} - -/// \brief Create a new descriptor for the specified global. -DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name, DIFile F, - unsigned LineNumber, - DITypeRef Ty, - bool isLocalToUnit, - Value *Val) { - return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit, - Val); -} - -/// createStaticVariable - Create a new descriptor for the specified static -/// variable. -DIGlobalVariable DIBuilder::createStaticVariable(DIDescriptor Context, - StringRef Name, - StringRef LinkageName, - DIFile F, unsigned LineNumber, - DITypeRef Ty, - bool isLocalToUnit, - Value *Val, MDNode *Decl) { - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_variable), - Constant::getNullValue(Type::getInt32Ty(VMContext)), - getNonCompileUnitScope(Context), - MDString::get(VMContext, Name), - MDString::get(VMContext, Name), - MDString::get(VMContext, LinkageName), - F, - ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), - Ty, - ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit), - ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/ - Val, - DIDescriptor(Decl) - }; - MDNode *Node = MDNode::get(VMContext, Elts); - AllGVs.push_back(Node); - return DIGlobalVariable(Node); +static DIGlobalVariable createGlobalVariableHelper( + LLVMContext &VMContext, DIDescriptor Context, StringRef Name, + StringRef LinkageName, DIFile F, unsigned LineNumber, DITypeRef Ty, + bool isLocalToUnit, Constant *Val, MDNode *Decl, bool isDefinition, + std::function<MDNode *(ArrayRef<Metadata *>)> CreateFunc) { + + MDNode *TheCtx = getNonCompileUnitScope(Context); + if (DIScope(TheCtx).isCompositeType()) { + assert(!DICompositeType(TheCtx).getIdentifier() && + "Context of a global variable should not be a type with identifier"); + } + + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_variable) + .concat(Name) + .concat(Name) + .concat(LinkageName) + .concat(LineNumber) + .concat(isLocalToUnit) + .concat(isDefinition) + .get(VMContext), + TheCtx, F, Ty, getConstantOrNull(Val), + DIDescriptor(Decl)}; + + return DIGlobalVariable(CreateFunc(Elts)); +} + +DIGlobalVariable DIBuilder::createGlobalVariable( + DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F, + unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val, + MDNode *Decl) { + return createGlobalVariableHelper( + VMContext, Context, Name, LinkageName, F, LineNumber, Ty, isLocalToUnit, + Val, Decl, true, [&](ArrayRef<Metadata *> Elts) -> MDNode *{ + MDNode *Node = MDNode::get(VMContext, Elts); + AllGVs.push_back(Node); + return Node; + }); +} + +DIGlobalVariable DIBuilder::createTempGlobalVariableFwdDecl( + DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F, + unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val, + MDNode *Decl) { + return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F, + LineNumber, Ty, isLocalToUnit, Val, Decl, + false, [&](ArrayRef<Metadata *> Elts) { + return MDNode::getTemporary(VMContext, Elts); + }); } -/// createVariable - Create a new descriptor for the specified variable. DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNo, DITypeRef Ty, @@ -1042,24 +920,20 @@ DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope, DIDescriptor Context(getNonCompileUnitScope(Scope)); assert((!Context || Context.isScope()) && "createLocalVariable should be called with a valid Context"); - Value *Elts[] = { - GetTagConstant(VMContext, Tag), - getNonCompileUnitScope(Scope), - MDString::get(VMContext, Name), - File, - ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))), - Ty, - ConstantInt::get(Type::getInt32Ty(VMContext), Flags), - Constant::getNullValue(Type::getInt32Ty(VMContext)) - }; + Metadata *Elts[] = {HeaderBuilder::get(Tag) + .concat(Name) + .concat(LineNo | (ArgNo << 24)) + .concat(Flags) + .get(VMContext), + getNonCompileUnitScope(Scope), File, Ty}; MDNode *Node = MDNode::get(VMContext, Elts); if (AlwaysPreserve) { // The optimizer may remove local variable. If there is an interest // to preserve variable info in such situation then stash it in a // named mdnode. DISubprogram Fn(getDISubprogram(Scope)); - NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn); - FnLocals->addOperand(Node); + assert(Fn && "Missing subprogram for local variable"); + PreservedVariables[Fn].emplace_back(Node); } DIVariable RetVar(Node); assert(RetVar.isVariable() && @@ -1067,33 +941,20 @@ DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope, return RetVar; } -/// createComplexVariable - Create a new descriptor for the specified variable -/// which has a complex address expression for its address. -DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope, - StringRef Name, DIFile F, - unsigned LineNo, - DITypeRef Ty, - ArrayRef<Value *> Addr, - unsigned ArgNo) { - assert(Addr.size() > 0 && "complex address is empty"); - Value *Elts[] = { - GetTagConstant(VMContext, Tag), - getNonCompileUnitScope(Scope), - MDString::get(VMContext, Name), - F, - ConstantInt::get(Type::getInt32Ty(VMContext), - (LineNo | (ArgNo << 24))), - Ty, - Constant::getNullValue(Type::getInt32Ty(VMContext)), - Constant::getNullValue(Type::getInt32Ty(VMContext)), - MDNode::get(VMContext, Addr) - }; - return DIVariable(MDNode::get(VMContext, Elts)); +DIExpression DIBuilder::createExpression(ArrayRef<int64_t> Addr) { + auto Header = HeaderBuilder::get(DW_TAG_expression); + for (int64_t I : Addr) + Header.concat(I); + Metadata *Elts[] = {Header.get(VMContext)}; + return DIExpression(MDNode::get(VMContext, Elts)); +} + +DIExpression DIBuilder::createPieceExpression(unsigned OffsetInBytes, + unsigned SizeInBytes) { + int64_t Addr[] = {dwarf::DW_OP_piece, OffsetInBytes, SizeInBytes}; + return createExpression(Addr); } -/// createFunction - Create a new descriptor for the specified function. -/// FIXME: this is added for dragonegg. Once we update dragonegg -/// to call resolve function, this will be removed. DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name, StringRef LinkageName, DIFile File, unsigned LineNo, DICompositeType Ty, @@ -1109,7 +970,38 @@ DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name, Flags, isOptimized, Fn, TParams, Decl); } -/// createFunction - Create a new descriptor for the specified function. +static DISubprogram createFunctionHelper( + LLVMContext &VMContext, DIDescriptor Context, StringRef Name, + StringRef LinkageName, DIFile File, unsigned LineNo, DICompositeType Ty, + bool isLocalToUnit, bool isDefinition, unsigned ScopeLine, unsigned Flags, + bool isOptimized, Function *Fn, MDNode *TParams, MDNode *Decl, MDNode *Vars, + std::function<MDNode *(ArrayRef<Metadata *>)> CreateFunc) { + assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type && + "function types should be subroutines"); + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram) + .concat(Name) + .concat(Name) + .concat(LinkageName) + .concat(LineNo) + .concat(isLocalToUnit) + .concat(isDefinition) + .concat(0) + .concat(0) + .concat(Flags) + .concat(isOptimized) + .concat(ScopeLine) + .get(VMContext), + File.getFileNode(), + DIScope(getNonCompileUnitScope(Context)).getRef(), Ty, + nullptr, getConstantOrNull(Fn), TParams, Decl, Vars}; + + DISubprogram S(CreateFunc(Elts)); + assert(S.isSubprogram() && + "createFunction should return a valid DISubprogram"); + return S; +} + + DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile File, unsigned LineNo, DICompositeType Ty, @@ -1117,43 +1009,36 @@ DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name, unsigned ScopeLine, unsigned Flags, bool isOptimized, Function *Fn, MDNode *TParams, MDNode *Decl) { - assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type && - "function types should be subroutines"); - Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) }; - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_subprogram), - File.getFileNode(), - DIScope(getNonCompileUnitScope(Context)).getRef(), - MDString::get(VMContext, Name), - MDString::get(VMContext, Name), - MDString::get(VMContext, LinkageName), - ConstantInt::get(Type::getInt32Ty(VMContext), LineNo), - Ty, - ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit), - ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), - ConstantInt::get(Type::getInt32Ty(VMContext), 0), - nullptr, - ConstantInt::get(Type::getInt32Ty(VMContext), Flags), - ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized), - Fn, - TParams, - Decl, - MDNode::getTemporary(VMContext, TElts), - ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine) - }; - MDNode *Node = MDNode::get(VMContext, Elts); + return createFunctionHelper(VMContext, Context, Name, LinkageName, File, + LineNo, Ty, isLocalToUnit, isDefinition, + ScopeLine, Flags, isOptimized, Fn, TParams, Decl, + MDNode::getTemporary(VMContext, None), + [&](ArrayRef<Metadata *> Elts) -> MDNode *{ + MDNode *Node = MDNode::get(VMContext, Elts); + // Create a named metadata so that we + // do not lose this mdnode. + if (isDefinition) + AllSubprograms.push_back(Node); + return Node; + }); +} - // Create a named metadata so that we do not lose this mdnode. - if (isDefinition) - AllSubprograms.push_back(Node); - DISubprogram S(Node); - assert(S.isSubprogram() && - "createFunction should return a valid DISubprogram"); - return S; +DISubprogram +DIBuilder::createTempFunctionFwdDecl(DIDescriptor Context, StringRef Name, + StringRef LinkageName, DIFile File, + unsigned LineNo, DICompositeType Ty, + bool isLocalToUnit, bool isDefinition, + unsigned ScopeLine, unsigned Flags, + bool isOptimized, Function *Fn, + MDNode *TParams, MDNode *Decl) { + return createFunctionHelper(VMContext, Context, Name, LinkageName, File, + LineNo, Ty, isLocalToUnit, isDefinition, + ScopeLine, Flags, isOptimized, Fn, TParams, Decl, + nullptr, [&](ArrayRef<Metadata *> Elts) { + return MDNode::getTemporary(VMContext, Elts); + }); } -/// createMethod - Create a new descriptor for the specified C++ method. DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F, unsigned LineNo, DICompositeType Ty, @@ -1167,29 +1052,23 @@ DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name, assert(getNonCompileUnitScope(Context) && "Methods should have both a Context and a context that isn't " "the compile unit."); - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_subprogram), - F.getFileNode(), - DIScope(Context).getRef(), - MDString::get(VMContext, Name), - MDString::get(VMContext, Name), - MDString::get(VMContext, LinkageName), - ConstantInt::get(Type::getInt32Ty(VMContext), LineNo), - Ty, - ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit), - ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition), - ConstantInt::get(Type::getInt32Ty(VMContext), VK), - ConstantInt::get(Type::getInt32Ty(VMContext), VIndex), - VTableHolder.getRef(), - ConstantInt::get(Type::getInt32Ty(VMContext), Flags), - ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized), - Fn, - TParam, - Constant::getNullValue(Type::getInt32Ty(VMContext)), - nullptr, - // FIXME: Do we want to use different scope/lines? - ConstantInt::get(Type::getInt32Ty(VMContext), LineNo) - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram) + .concat(Name) + .concat(Name) + .concat(LinkageName) + .concat(LineNo) + .concat(isLocalToUnit) + .concat(isDefinition) + .concat(VK) + .concat(VIndex) + .concat(Flags) + .concat(isOptimized) + .concat(LineNo) + // FIXME: Do we want to use different scope/lines? + .get(VMContext), + F.getFileNode(), DIScope(Context).getRef(), Ty, + VTableHolder.getRef(), getConstantOrNull(Fn), TParam, + nullptr, nullptr}; MDNode *Node = MDNode::get(VMContext, Elts); if (isDefinition) AllSubprograms.push_back(Node); @@ -1198,32 +1077,26 @@ DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name, return S; } -/// createNameSpace - This creates new descriptor for a namespace -/// with the specified parent scope. DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNo) { - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_namespace), - File.getFileNode(), - getNonCompileUnitScope(Scope), - MDString::get(VMContext, Name), - ConstantInt::get(Type::getInt32Ty(VMContext), LineNo) - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_namespace) + .concat(Name) + .concat(LineNo) + .get(VMContext), + File.getFileNode(), getNonCompileUnitScope(Scope)}; DINameSpace R(MDNode::get(VMContext, Elts)); assert(R.Verify() && "createNameSpace should return a verifiable DINameSpace"); return R; } -/// createLexicalBlockFile - This creates a new MDNode that encapsulates -/// an existing scope with a new filename. DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope, - DIFile File) { - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block), - File.getFileNode(), - Scope - }; + DIFile File, + unsigned Discriminator) { + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block) + .concat(Discriminator) + .get(VMContext), + File.getFileNode(), Scope}; DILexicalBlockFile R(MDNode::get(VMContext, Elts)); assert( R.Verify() && @@ -1232,8 +1105,7 @@ DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope, } DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File, - unsigned Line, unsigned Col, - unsigned Discriminator) { + unsigned Line, unsigned Col) { // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing. // I believe the right way is to have a self-referential element in the node. // Also: why do we bother with line/column - they're not used and the @@ -1243,44 +1115,52 @@ DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File, // Defeat MDNode uniquing for lexical blocks by using unique id. static unsigned int unique_id = 0; - Value *Elts[] = { - GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block), - File.getFileNode(), - getNonCompileUnitScope(Scope), - ConstantInt::get(Type::getInt32Ty(VMContext), Line), - ConstantInt::get(Type::getInt32Ty(VMContext), Col), - ConstantInt::get(Type::getInt32Ty(VMContext), Discriminator), - ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++) - }; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block) + .concat(Line) + .concat(Col) + .concat(unique_id++) + .get(VMContext), + File.getFileNode(), getNonCompileUnitScope(Scope)}; DILexicalBlock R(MDNode::get(VMContext, Elts)); assert(R.Verify() && "createLexicalBlock should return a verifiable DILexicalBlock"); return R; } -/// insertDeclare - Insert a new llvm.dbg.declare intrinsic call. +static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) { + assert(V && "no value passed to dbg intrinsic"); + return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V)); +} + Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo, + DIExpression Expr, Instruction *InsertBefore) { - assert(Storage && "no storage passed to dbg.declare"); assert(VarInfo.isVariable() && "empty or invalid DIVariable passed to dbg.declare"); if (!DeclareFn) DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare); - Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo }; + trackIfUnresolved(VarInfo); + trackIfUnresolved(Expr); + Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage), + MetadataAsValue::get(VMContext, VarInfo), + MetadataAsValue::get(VMContext, Expr)}; return CallInst::Create(DeclareFn, Args, "", InsertBefore); } -/// insertDeclare - Insert a new llvm.dbg.declare intrinsic call. Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo, + DIExpression Expr, BasicBlock *InsertAtEnd) { - assert(Storage && "no storage passed to dbg.declare"); assert(VarInfo.isVariable() && "empty or invalid DIVariable passed to dbg.declare"); if (!DeclareFn) DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare); - Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo }; + trackIfUnresolved(VarInfo); + trackIfUnresolved(Expr); + Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage), + MetadataAsValue::get(VMContext, VarInfo), + MetadataAsValue::get(VMContext, Expr)}; // If this block already has a terminator then insert this intrinsic // before the terminator. @@ -1290,9 +1170,9 @@ Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo, return CallInst::Create(DeclareFn, Args, "", InsertAtEnd); } -/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call. Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset, DIVariable VarInfo, + DIExpression Expr, Instruction *InsertBefore) { assert(V && "no value passed to dbg.value"); assert(VarInfo.isVariable() && @@ -1300,15 +1180,18 @@ Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset, if (!ValueFn) ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value); - Value *Args[] = { MDNode::get(V->getContext(), V), - ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset), - VarInfo }; + trackIfUnresolved(VarInfo); + trackIfUnresolved(Expr); + Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V), + ConstantInt::get(Type::getInt64Ty(VMContext), Offset), + MetadataAsValue::get(VMContext, VarInfo), + MetadataAsValue::get(VMContext, Expr)}; return CallInst::Create(ValueFn, Args, "", InsertBefore); } -/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call. Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset, DIVariable VarInfo, + DIExpression Expr, BasicBlock *InsertAtEnd) { assert(V && "no value passed to dbg.value"); assert(VarInfo.isVariable() && @@ -1316,8 +1199,43 @@ Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset, if (!ValueFn) ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value); - Value *Args[] = { MDNode::get(V->getContext(), V), - ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset), - VarInfo }; + trackIfUnresolved(VarInfo); + trackIfUnresolved(Expr); + Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V), + ConstantInt::get(Type::getInt64Ty(VMContext), Offset), + MetadataAsValue::get(VMContext, VarInfo), + MetadataAsValue::get(VMContext, Expr)}; return CallInst::Create(ValueFn, Args, "", InsertAtEnd); } + +void DIBuilder::replaceVTableHolder(DICompositeType &T, DICompositeType VTableHolder) { + T.setContainingType(VTableHolder); + + // If this didn't create a self-reference, just return. + if (T != VTableHolder) + return; + + // Look for unresolved operands. T has dropped RAUW support and is already + // marked resolved, orphaning any cycles underneath it. + assert(T->isResolved() && "Expected self-reference to be resolved"); + for (const MDOperand &O : T->operands()) + if (auto *N = dyn_cast_or_null<MDNode>(O)) + trackIfUnresolved(N); +} + +void DIBuilder::replaceArrays(DICompositeType &T, DIArray Elements, + DIArray TParams) { + T.setArrays(Elements, TParams); + + // If T isn't resolved, there's no problem. + if (!T->isResolved()) + return; + + // If "T" is resolved, it may be due to a self-reference cycle. Track the + // arrays explicitly if they're unresolved, or else the cycles will be + // orphaned. + if (Elements) + trackIfUnresolved(Elements); + if (TParams) + trackIfUnresolved(TParams); +} diff --git a/contrib/llvm/lib/IR/DataLayout.cpp b/contrib/llvm/lib/IR/DataLayout.cpp index dea05fb..cde3937 100644 --- a/contrib/llvm/lib/IR/DataLayout.cpp +++ b/contrib/llvm/lib/IR/DataLayout.cpp @@ -55,7 +55,7 @@ StructLayout::StructLayout(StructType *ST, const DataLayout &DL) { // Add padding if necessary to align the data element properly. if ((StructSize & (TyAlign-1)) != 0) - StructSize = DataLayout::RoundUpAlignment(StructSize, TyAlign); + StructSize = RoundUpToAlignment(StructSize, TyAlign); // Keep track of maximum alignment constraint. StructAlignment = std::max(TyAlign, StructAlignment); @@ -70,7 +70,7 @@ StructLayout::StructLayout(StructType *ST, const DataLayout &DL) { // Add padding to the end of the struct so that it could be put in an array // and all array elements would be aligned correctly. if ((StructSize & (StructAlignment-1)) != 0) - StructSize = DataLayout::RoundUpAlignment(StructSize, StructAlignment); + StructSize = RoundUpToAlignment(StructSize, StructAlignment); } @@ -179,7 +179,7 @@ void DataLayout::reset(StringRef Desc) { clear(); LayoutMap = nullptr; - LittleEndian = false; + BigEndian = false; StackNaturalAlign = 0; ManglingMode = MM_None; @@ -197,8 +197,10 @@ void DataLayout::reset(StringRef Desc) { static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) { assert(!Str.empty() && "parse error, string can't be empty here"); std::pair<StringRef, StringRef> Split = Str.split(Separator); - assert((!Split.second.empty() || Split.first == Str) && - "a trailing separator is not allowed"); + if (Split.second.empty() && Split.first != Str) + report_fatal_error("Trailing separator in datalayout string"); + if (!Split.second.empty() && Split.first.empty()) + report_fatal_error("Expected token before separator in datalayout string"); return Split; } @@ -213,7 +215,8 @@ static unsigned getInt(StringRef R) { /// Convert bits into bytes. Assert if not a byte width multiple. static unsigned inBytes(unsigned Bits) { - assert(Bits % 8 == 0 && "number of bits must be a byte width multiple"); + if (Bits % 8) + report_fatal_error("number of bits must be a byte width multiple"); return Bits / 8; } @@ -239,22 +242,28 @@ void DataLayout::parseSpecifier(StringRef Desc) { // FIXME: remove this on LLVM 4.0. break; case 'E': - LittleEndian = false; + BigEndian = true; break; case 'e': - LittleEndian = true; + BigEndian = false; break; case 'p': { // Address space. unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok); - assert(AddrSpace < 1 << 24 && - "Invalid address space, must be a 24bit integer"); + if (!isUInt<24>(AddrSpace)) + report_fatal_error("Invalid address space, must be a 24bit integer"); // Size. + if (Rest.empty()) + report_fatal_error( + "Missing size specification for pointer in datalayout string"); Split = split(Rest, ':'); unsigned PointerMemSize = inBytes(getInt(Tok)); // ABI alignment. + if (Rest.empty()) + report_fatal_error( + "Missing alignment specification for pointer in datalayout string"); Split = split(Rest, ':'); unsigned PointerABIAlign = inBytes(getInt(Tok)); @@ -285,10 +294,14 @@ void DataLayout::parseSpecifier(StringRef Desc) { // Bit size. unsigned Size = Tok.empty() ? 0 : getInt(Tok); - assert((AlignType != AGGREGATE_ALIGN || Size == 0) && - "These specifications don't have a size"); + if (AlignType == AGGREGATE_ALIGN && Size != 0) + report_fatal_error( + "Sized aggregate specification in datalayout string"); // ABI alignment. + if (Rest.empty()) + report_fatal_error( + "Missing alignment specification in datalayout string"); Split = split(Rest, ':'); unsigned ABIAlign = inBytes(getInt(Tok)); @@ -306,7 +319,9 @@ void DataLayout::parseSpecifier(StringRef Desc) { case 'n': // Native integer types. for (;;) { unsigned Width = getInt(Tok); - assert(Width != 0 && "width must be non-zero"); + if (Width == 0) + report_fatal_error( + "Zero width native integer type in datalayout string"); LegalIntWidths.push_back(Width); if (Rest.empty()) break; @@ -318,11 +333,15 @@ void DataLayout::parseSpecifier(StringRef Desc) { break; } case 'm': - assert(Tok.empty()); - assert(Rest.size() == 1); + if (!Tok.empty()) + report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string"); + if (Rest.empty()) + report_fatal_error("Expected mangling specifier in datalayout string"); + if (Rest.size() > 1) + report_fatal_error("Unknown mangling specifier in datalayout string"); switch(Rest[0]) { default: - llvm_unreachable("Unknown mangling in datalayout string"); + report_fatal_error("Unknown mangling in datalayout string"); case 'e': ManglingMode = MM_ELF; break; @@ -338,13 +357,17 @@ void DataLayout::parseSpecifier(StringRef Desc) { } break; default: - llvm_unreachable("Unknown specifier in datalayout string"); + report_fatal_error("Unknown specifier in datalayout string"); break; } } } DataLayout::DataLayout(const Module *M) : LayoutMap(nullptr) { + init(M); +} + +void DataLayout::init(const Module *M) { const DataLayout *Other = M->getDataLayout(); if (Other) *this = *Other; @@ -353,7 +376,7 @@ DataLayout::DataLayout(const Module *M) : LayoutMap(nullptr) { } bool DataLayout::operator==(const DataLayout &Other) const { - bool Ret = LittleEndian == Other.LittleEndian && + bool Ret = BigEndian == Other.BigEndian && StackNaturalAlign == Other.StackNaturalAlign && ManglingMode == Other.ManglingMode && LegalIntWidths == Other.LegalIntWidths && @@ -522,7 +545,7 @@ std::string DataLayout::getStringRepresentation() const { std::string Result; raw_string_ostream OS(Result); - OS << (LittleEndian ? "e" : "E"); + OS << (BigEndian ? "E" : "e"); switch (ManglingMode) { case MM_None: @@ -637,7 +660,7 @@ unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const { ? getPointerABIAlignment(0) : getPointerPrefAlignment(0)); case Type::PointerTyID: { - unsigned AS = dyn_cast<PointerType>(Ty)->getAddressSpace(); + unsigned AS = cast<PointerType>(Ty)->getAddressSpace(); return (abi_or_pref ? getPointerABIAlignment(AS) : getPointerPrefAlignment(AS)); @@ -796,17 +819,17 @@ unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const { } DataLayoutPass::DataLayoutPass() : ImmutablePass(ID), DL("") { - report_fatal_error("Bad DataLayoutPass ctor used. Tool did not specify a " - "DataLayout to use?"); + initializeDataLayoutPassPass(*PassRegistry::getPassRegistry()); } DataLayoutPass::~DataLayoutPass() {} -DataLayoutPass::DataLayoutPass(const DataLayout &DL) - : ImmutablePass(ID), DL(DL) { - initializeDataLayoutPassPass(*PassRegistry::getPassRegistry()); +bool DataLayoutPass::doInitialization(Module &M) { + DL.init(&M); + return false; } -DataLayoutPass::DataLayoutPass(const Module *M) : ImmutablePass(ID), DL(M) { - initializeDataLayoutPassPass(*PassRegistry::getPassRegistry()); +bool DataLayoutPass::doFinalization(Module &M) { + DL.reset(""); + return false; } diff --git a/contrib/llvm/lib/IR/DebugInfo.cpp b/contrib/llvm/lib/IR/DebugInfo.cpp index 5e39b24..290dbe2 100644 --- a/contrib/llvm/lib/IR/DebugInfo.cpp +++ b/contrib/llvm/lib/IR/DebugInfo.cpp @@ -19,6 +19,7 @@ #include "llvm/ADT/SmallString.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/DIBuilder.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" @@ -46,13 +47,12 @@ bool DIDescriptor::Verify() const { DILexicalBlockFile(DbgNode).Verify() || DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() || DIObjCProperty(DbgNode).Verify() || - DIUnspecifiedParameter(DbgNode).Verify() || DITemplateTypeParameter(DbgNode).Verify() || DITemplateValueParameter(DbgNode).Verify() || - DIImportedEntity(DbgNode).Verify()); + DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify()); } -static Value *getField(const MDNode *DbgNode, unsigned Elt) { +static Metadata *getField(const MDNode *DbgNode, unsigned Elt) { if (!DbgNode || Elt >= DbgNode->getNumOperands()) return nullptr; return DbgNode->getOperand(Elt); @@ -73,25 +73,17 @@ StringRef DIDescriptor::getStringField(unsigned Elt) const { } uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const { - if (!DbgNode) - return 0; - - if (Elt < DbgNode->getNumOperands()) - if (ConstantInt *CI = - dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt))) + if (auto *C = getConstantField(Elt)) + if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) return CI->getZExtValue(); return 0; } int64_t DIDescriptor::getInt64Field(unsigned Elt) const { - if (!DbgNode) - return 0; - - if (Elt < DbgNode->getNumOperands()) - if (ConstantInt *CI = - dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt))) - return CI->getSExtValue(); + if (auto *C = getConstantField(Elt)) + if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) + return CI->getZExtValue(); return 0; } @@ -102,12 +94,7 @@ DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const { } GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const { - if (!DbgNode) - return nullptr; - - if (Elt < DbgNode->getNumOperands()) - return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt)); - return nullptr; + return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt)); } Constant *DIDescriptor::getConstantField(unsigned Elt) const { @@ -115,17 +102,14 @@ Constant *DIDescriptor::getConstantField(unsigned Elt) const { return nullptr; if (Elt < DbgNode->getNumOperands()) - return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt)); + if (auto *C = + dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt))) + return C->getValue(); return nullptr; } Function *DIDescriptor::getFunctionField(unsigned Elt) const { - if (!DbgNode) - return nullptr; - - if (Elt < DbgNode->getNumOperands()) - return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt)); - return nullptr; + return dyn_cast_or_null<Function>(getConstantField(Elt)); } void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) { @@ -134,29 +118,57 @@ void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) { if (Elt < DbgNode->getNumOperands()) { MDNode *Node = const_cast<MDNode *>(DbgNode); - Node->replaceOperandWith(Elt, F); + Node->replaceOperandWith(Elt, F ? ConstantAsMetadata::get(F) : nullptr); } } -uint64_t DIVariable::getAddrElement(unsigned Idx) const { - DIDescriptor ComplexExpr = getDescriptorField(8); - if (Idx < ComplexExpr->getNumOperands()) - if (auto *CI = dyn_cast_or_null<ConstantInt>(ComplexExpr->getOperand(Idx))) - return CI->getZExtValue(); +static unsigned DIVariableInlinedAtIndex = 4; +MDNode *DIVariable::getInlinedAt() const { + return getNodeField(DbgNode, DIVariableInlinedAtIndex); +} - assert(false && "non-existing complex address element requested"); - return 0; +/// \brief Return the size reported by the variable's type. +unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) { + DIType Ty = getType().resolve(Map); + // Follow derived types until we reach a type that + // reports back a size. + while (Ty.isDerivedType() && !Ty.getSizeInBits()) { + DIDerivedType DT(&*Ty); + Ty = DT.getTypeDerivedFrom().resolve(Map); + } + assert(Ty.getSizeInBits() && "type with size 0"); + return Ty.getSizeInBits(); +} + +uint64_t DIExpression::getElement(unsigned Idx) const { + unsigned I = Idx + 1; + assert(I < getNumHeaderFields() && + "non-existing complex address element requested"); + return getHeaderFieldAs<int64_t>(I); } -/// getInlinedAt - If this variable is inlined then return inline location. -MDNode *DIVariable::getInlinedAt() const { return getNodeField(DbgNode, 7); } +bool DIExpression::isVariablePiece() const { + return getNumElements() && getElement(0) == dwarf::DW_OP_piece; +} + +uint64_t DIExpression::getPieceOffset() const { + assert(isVariablePiece()); + return getElement(1); +} + +uint64_t DIExpression::getPieceSize() const { + assert(isVariablePiece()); + return getElement(2); +} //===----------------------------------------------------------------------===// // Predicates //===----------------------------------------------------------------------===// -/// isBasicType - Return true if the specified tag is legal for -/// DIBasicType. +bool DIDescriptor::isSubroutineType() const { + return DbgNode && getTag() == dwarf::DW_TAG_subroutine_type; +} + bool DIDescriptor::isBasicType() const { if (!DbgNode) return false; @@ -169,7 +181,6 @@ bool DIDescriptor::isBasicType() const { } } -/// isDerivedType - Return true if the specified tag is legal for DIDerivedType. bool DIDescriptor::isDerivedType() const { if (!DbgNode) return false; @@ -192,8 +203,6 @@ bool DIDescriptor::isDerivedType() const { } } -/// isCompositeType - Return true if the specified tag is legal for -/// DICompositeType. bool DIDescriptor::isCompositeType() const { if (!DbgNode) return false; @@ -210,7 +219,6 @@ bool DIDescriptor::isCompositeType() const { } } -/// isVariable - Return true if the specified tag is legal for DIVariable. bool DIDescriptor::isVariable() const { if (!DbgNode) return false; @@ -223,32 +231,19 @@ bool DIDescriptor::isVariable() const { } } -/// isType - Return true if the specified tag is legal for DIType. bool DIDescriptor::isType() const { return isBasicType() || isCompositeType() || isDerivedType(); } -/// isSubprogram - Return true if the specified tag is legal for -/// DISubprogram. bool DIDescriptor::isSubprogram() const { return DbgNode && getTag() == dwarf::DW_TAG_subprogram; } -/// isGlobalVariable - Return true if the specified tag is legal for -/// DIGlobalVariable. bool DIDescriptor::isGlobalVariable() const { return DbgNode && (getTag() == dwarf::DW_TAG_variable || getTag() == dwarf::DW_TAG_constant); } -/// isUnspecifiedParmeter - Return true if the specified tag is -/// DW_TAG_unspecified_parameters. -bool DIDescriptor::isUnspecifiedParameter() const { - return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters; -} - -/// isScope - Return true if the specified tag is one of the scope -/// related tag. bool DIDescriptor::isScope() const { if (!DbgNode) return false; @@ -265,83 +260,67 @@ bool DIDescriptor::isScope() const { return isType(); } -/// isTemplateTypeParameter - Return true if the specified tag is -/// DW_TAG_template_type_parameter. bool DIDescriptor::isTemplateTypeParameter() const { return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter; } -/// isTemplateValueParameter - Return true if the specified tag is -/// DW_TAG_template_value_parameter. bool DIDescriptor::isTemplateValueParameter() const { return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter || getTag() == dwarf::DW_TAG_GNU_template_template_param || getTag() == dwarf::DW_TAG_GNU_template_parameter_pack); } -/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit. bool DIDescriptor::isCompileUnit() const { return DbgNode && getTag() == dwarf::DW_TAG_compile_unit; } -/// isFile - Return true if the specified tag is DW_TAG_file_type. bool DIDescriptor::isFile() const { return DbgNode && getTag() == dwarf::DW_TAG_file_type; } -/// isNameSpace - Return true if the specified tag is DW_TAG_namespace. bool DIDescriptor::isNameSpace() const { return DbgNode && getTag() == dwarf::DW_TAG_namespace; } -/// isLexicalBlockFile - Return true if the specified descriptor is a -/// lexical block with an extra file. bool DIDescriptor::isLexicalBlockFile() const { return DbgNode && getTag() == dwarf::DW_TAG_lexical_block && - (DbgNode->getNumOperands() == 3); + DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2; } -/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block. bool DIDescriptor::isLexicalBlock() const { + // FIXME: There are always exactly 4 header fields in DILexicalBlock, but + // something relies on this returning true for DILexicalBlockFile. return DbgNode && getTag() == dwarf::DW_TAG_lexical_block && - (DbgNode->getNumOperands() > 3); + DbgNode->getNumOperands() == 3 && + (getNumHeaderFields() == 2 || getNumHeaderFields() == 4); } -/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type. bool DIDescriptor::isSubrange() const { return DbgNode && getTag() == dwarf::DW_TAG_subrange_type; } -/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator. bool DIDescriptor::isEnumerator() const { return DbgNode && getTag() == dwarf::DW_TAG_enumerator; } -/// isObjCProperty - Return true if the specified tag is DW_TAG_APPLE_property. bool DIDescriptor::isObjCProperty() const { return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property; } -/// \brief Return true if the specified tag is DW_TAG_imported_module or -/// DW_TAG_imported_declaration. bool DIDescriptor::isImportedEntity() const { return DbgNode && (getTag() == dwarf::DW_TAG_imported_module || getTag() == dwarf::DW_TAG_imported_declaration); } +bool DIDescriptor::isExpression() const { + return DbgNode && (getTag() == dwarf::DW_TAG_expression); +} + //===----------------------------------------------------------------------===// // Simple Descriptor Constructors and other Methods //===----------------------------------------------------------------------===// -unsigned DIArray::getNumElements() const { - if (!DbgNode) - return 0; - return DbgNode->getNumOperands(); -} - -/// replaceAllUsesWith - Replace all uses of the MDNode used by this -/// type with the one in the passed descriptor. -void DIType::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) { +void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) { assert(DbgNode && "Trying to replace an unverified type!"); @@ -352,33 +331,26 @@ void DIType::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) { // itself. const MDNode *DN = D; if (DbgNode == DN) { - SmallVector<Value*, 10> Ops(DbgNode->getNumOperands()); + SmallVector<Metadata *, 10> Ops(DbgNode->getNumOperands()); for (size_t i = 0; i != Ops.size(); ++i) Ops[i] = DbgNode->getOperand(i); DN = MDNode::get(VMContext, Ops); } - MDNode *Node = const_cast<MDNode *>(DbgNode); - const Value *V = cast_or_null<Value>(DN); - Node->replaceAllUsesWith(const_cast<Value *>(V)); + auto *Node = cast<MDNodeFwdDecl>(const_cast<MDNode *>(DbgNode)); + Node->replaceAllUsesWith(const_cast<MDNode *>(DN)); MDNode::deleteTemporary(Node); - DbgNode = D; + DbgNode = DN; } -/// replaceAllUsesWith - Replace all uses of the MDNode used by this -/// type with the one in D. -void DIType::replaceAllUsesWith(MDNode *D) { - +void DIDescriptor::replaceAllUsesWith(MDNode *D) { assert(DbgNode && "Trying to replace an unverified type!"); assert(DbgNode != D && "This replacement should always happen"); - MDNode *Node = const_cast<MDNode *>(DbgNode); - const MDNode *DN = D; - const Value *V = cast_or_null<Value>(DN); - Node->replaceAllUsesWith(const_cast<Value *>(V)); + auto *Node = cast<MDNodeFwdDecl>(const_cast<MDNode *>(DbgNode)); + Node->replaceAllUsesWith(D); MDNode::deleteTemporary(Node); } -/// Verify - Verify that a compile unit is well formed. bool DICompileUnit::Verify() const { if (!isCompileUnit()) return false; @@ -388,65 +360,66 @@ bool DICompileUnit::Verify() const { if (getFilename().empty()) return false; - return DbgNode->getNumOperands() == 14; + return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8; } -/// Verify - Verify that an ObjC property is well formed. bool DIObjCProperty::Verify() const { if (!isObjCProperty()) return false; // Don't worry about the rest of the strings for now. - return DbgNode->getNumOperands() == 8; + return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6; } -/// Check if a field at position Elt of a MDNode is a MDNode. +/// \brief Check if a field at position Elt of a MDNode is a MDNode. +/// /// We currently allow an empty string and an integer. /// But we don't allow a non-empty string in a MDNode field. static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) { // FIXME: This function should return true, if the field is null or the field // is indeed a MDNode: return !Fld || isa<MDNode>(Fld). - Value *Fld = getField(DbgNode, Elt); + Metadata *Fld = getField(DbgNode, Elt); if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty()) return false; return true; } -/// Check if a field at position Elt of a MDNode is a MDString. +/// \brief Check if a field at position Elt of a MDNode is a MDString. static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) { - Value *Fld = getField(DbgNode, Elt); + Metadata *Fld = getField(DbgNode, Elt); return !Fld || isa<MDString>(Fld); } -/// Check if a value can be a reference to a type. -static bool isTypeRef(const Value *Val) { - return !Val || - (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) || - (isa<MDNode>(Val) && DIType(cast<MDNode>(Val)).isType()); +/// \brief Check if a value can be a reference to a type. +static bool isTypeRef(const Metadata *MD) { + if (!MD) + return true; + if (auto *S = dyn_cast<MDString>(MD)) + return !S->getString().empty(); + if (auto *N = dyn_cast<MDNode>(MD)) + return DIType(N).isType(); + return false; } -/// Check if a field at position Elt of a MDNode can be a reference to a type. +/// \brief Check if referenced field might be a type. static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) { - Value *Fld = getField(DbgNode, Elt); - return isTypeRef(Fld); + return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt))); } -/// Check if a value can be a ScopeRef. -static bool isScopeRef(const Value *Val) { - return !Val || - (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) || - // Not checking for Val->isScope() here, because it would work - // only for lexical scopes and not all subclasses of DIScope. - isa<MDNode>(Val); +/// \brief Check if a value can be a ScopeRef. +static bool isScopeRef(const Metadata *MD) { + if (!MD) + return true; + if (auto *S = dyn_cast<MDString>(MD)) + return !S->getString().empty(); + return isa<MDNode>(MD); } -/// Check if a field at position Elt of a MDNode can be a ScopeRef. +/// \brief Check if a field at position Elt of a MDNode can be a ScopeRef. static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) { - Value *Fld = getField(DbgNode, Elt); - return isScopeRef(Fld); + return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt))); } -/// Verify - Verify that a type descriptor is well formed. bool DIType::Verify() const { if (!isType()) return false; @@ -467,6 +440,7 @@ bool DIType::Verify() const { Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend && getFilename().empty()) return false; + // DIType is abstract, it should be a BasicType, a DerivedType or // a CompositeType. if (isBasicType()) @@ -479,89 +453,112 @@ bool DIType::Verify() const { return false; } -/// Verify - Verify that a basic type descriptor is well formed. bool DIBasicType::Verify() const { - return isBasicType() && DbgNode->getNumOperands() == 10; + return isBasicType() && DbgNode->getNumOperands() == 3 && + getNumHeaderFields() == 8; } -/// Verify - Verify that a derived type descriptor is well formed. bool DIDerivedType::Verify() const { - // Make sure DerivedFrom @ field 9 is TypeRef. - if (!fieldIsTypeRef(DbgNode, 9)) + // Make sure DerivedFrom @ field 3 is TypeRef. + if (!fieldIsTypeRef(DbgNode, 3)) return false; if (getTag() == dwarf::DW_TAG_ptr_to_member_type) - // Make sure ClassType @ field 10 is a TypeRef. - if (!fieldIsTypeRef(DbgNode, 10)) + // Make sure ClassType @ field 4 is a TypeRef. + if (!fieldIsTypeRef(DbgNode, 4)) return false; - return isDerivedType() && DbgNode->getNumOperands() >= 10 && - DbgNode->getNumOperands() <= 14; + return isDerivedType() && DbgNode->getNumOperands() >= 4 && + DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 && + getNumHeaderFields() <= 8; } -/// Verify - Verify that a composite type descriptor is well formed. bool DICompositeType::Verify() const { if (!isCompositeType()) return false; - // Make sure DerivedFrom @ field 9 and ContainingType @ field 12 are TypeRef. - if (!fieldIsTypeRef(DbgNode, 9)) + // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef. + if (!fieldIsTypeRef(DbgNode, 3)) return false; - if (!fieldIsTypeRef(DbgNode, 12)) + if (!fieldIsTypeRef(DbgNode, 5)) return false; - // Make sure the type identifier at field 14 is MDString, it can be null. - if (!fieldIsMDString(DbgNode, 14)) + // Make sure the type identifier at field 7 is MDString, it can be null. + if (!fieldIsMDString(DbgNode, 7)) return false; // A subroutine type can't be both & and &&. if (isLValueReference() && isRValueReference()) return false; - return DbgNode->getNumOperands() == 15; + return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8; } -/// Verify - Verify that a subprogram descriptor is well formed. bool DISubprogram::Verify() const { if (!isSubprogram()) return false; - // Make sure context @ field 2 is a ScopeRef and type @ field 7 is a MDNode. + // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode. if (!fieldIsScopeRef(DbgNode, 2)) return false; - if (!fieldIsMDNode(DbgNode, 7)) + if (!fieldIsMDNode(DbgNode, 3)) return false; - // Containing type @ field 12. - if (!fieldIsTypeRef(DbgNode, 12)) + // Containing type @ field 4. + if (!fieldIsTypeRef(DbgNode, 4)) return false; // A subprogram can't be both & and &&. if (isLValueReference() && isRValueReference()) return false; - return DbgNode->getNumOperands() == 20; + // If a DISubprogram has an llvm::Function*, then scope chains from all + // instructions within the function should lead to this DISubprogram. + if (auto *F = getFunction()) { + for (auto &BB : *F) { + for (auto &I : BB) { + DebugLoc DL = I.getDebugLoc(); + if (DL.isUnknown()) + continue; + + MDNode *Scope = nullptr; + MDNode *IA = nullptr; + // walk the inlined-at scopes + while ((IA = DL.getInlinedAt())) + DL = DebugLoc::getFromDILocation(IA); + DL.getScopeAndInlinedAt(Scope, IA); + assert(!IA); + while (!DIDescriptor(Scope).isSubprogram()) { + DILexicalBlockFile D(Scope); + Scope = D.isLexicalBlockFile() + ? D.getScope() + : DebugLoc::getFromDILexicalBlock(Scope).getScope(); + } + if (!DISubprogram(Scope).describes(F)) + return false; + } + } + } + return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12; } -/// Verify - Verify that a global variable descriptor is well formed. bool DIGlobalVariable::Verify() const { if (!isGlobalVariable()) return false; if (getDisplayName().empty()) return false; - // Make sure context @ field 2 is an MDNode. - if (!fieldIsMDNode(DbgNode, 2)) + // Make sure context @ field 1 is an MDNode. + if (!fieldIsMDNode(DbgNode, 1)) return false; - // Make sure that type @ field 8 is a DITypeRef. - if (!fieldIsTypeRef(DbgNode, 8)) + // Make sure that type @ field 3 is a DITypeRef. + if (!fieldIsTypeRef(DbgNode, 3)) return false; - // Make sure StaticDataMemberDeclaration @ field 12 is MDNode. - if (!fieldIsMDNode(DbgNode, 12)) + // Make sure StaticDataMemberDeclaration @ field 5 is MDNode. + if (!fieldIsMDNode(DbgNode, 5)) return false; - return DbgNode->getNumOperands() == 13; + return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7; } -/// Verify - Verify that a variable descriptor is well formed. bool DIVariable::Verify() const { if (!isVariable()) return false; @@ -569,127 +566,120 @@ bool DIVariable::Verify() const { // Make sure context @ field 1 is an MDNode. if (!fieldIsMDNode(DbgNode, 1)) return false; - // Make sure that type @ field 5 is a DITypeRef. - if (!fieldIsTypeRef(DbgNode, 5)) + // Make sure that type @ field 3 is a DITypeRef. + if (!fieldIsTypeRef(DbgNode, 3)) return false; - // Variable without a complex expression. - if (DbgNode->getNumOperands() == 8) + // Check the number of header fields, which is common between complex and + // simple variables. + if (getNumHeaderFields() != 4) + return false; + + // Variable without an inline location. + if (DbgNode->getNumOperands() == 4) return true; - // Make sure the complex expression is an MDNode. - return (DbgNode->getNumOperands() == 9 && fieldIsMDNode(DbgNode, 8)); + // Variable with an inline location. + return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5; } -/// Verify - Verify that a location descriptor is well formed. -bool DILocation::Verify() const { +bool DIExpression::Verify() const { + // Empty DIExpressions may be represented as a nullptr. if (!DbgNode) - return false; + return true; - return DbgNode->getNumOperands() == 4; + return isExpression() && DbgNode->getNumOperands() == 1; +} + +bool DILocation::Verify() const { + return DbgNode && isa<MDLocation>(DbgNode); } -/// Verify - Verify that a namespace descriptor is well formed. bool DINameSpace::Verify() const { if (!isNameSpace()) return false; - return DbgNode->getNumOperands() == 5; + return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3; } -/// \brief Retrieve the MDNode for the directory/file pair. MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); } -/// \brief Verify that the file descriptor is well formed. bool DIFile::Verify() const { return isFile() && DbgNode->getNumOperands() == 2; } -/// \brief Verify that the enumerator descriptor is well formed. bool DIEnumerator::Verify() const { - return isEnumerator() && DbgNode->getNumOperands() == 3; + return isEnumerator() && DbgNode->getNumOperands() == 1 && + getNumHeaderFields() == 3; } -/// \brief Verify that the subrange descriptor is well formed. bool DISubrange::Verify() const { - return isSubrange() && DbgNode->getNumOperands() == 3; + return isSubrange() && DbgNode->getNumOperands() == 1 && + getNumHeaderFields() == 3; } -/// \brief Verify that the lexical block descriptor is well formed. bool DILexicalBlock::Verify() const { - return isLexicalBlock() && DbgNode->getNumOperands() == 7; + return isLexicalBlock() && DbgNode->getNumOperands() == 3 && + getNumHeaderFields() == 4; } -/// \brief Verify that the file-scoped lexical block descriptor is well formed. bool DILexicalBlockFile::Verify() const { - return isLexicalBlockFile() && DbgNode->getNumOperands() == 3; -} - -/// \brief Verify that an unspecified parameter descriptor is well formed. -bool DIUnspecifiedParameter::Verify() const { - return isUnspecifiedParameter() && DbgNode->getNumOperands() == 1; + return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 && + getNumHeaderFields() == 2; } -/// \brief Verify that the template type parameter descriptor is well formed. bool DITemplateTypeParameter::Verify() const { - return isTemplateTypeParameter() && DbgNode->getNumOperands() == 7; + return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 && + getNumHeaderFields() == 4; } -/// \brief Verify that the template value parameter descriptor is well formed. bool DITemplateValueParameter::Verify() const { - return isTemplateValueParameter() && DbgNode->getNumOperands() == 8; + return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 && + getNumHeaderFields() == 4; } -/// \brief Verify that the imported module descriptor is well formed. bool DIImportedEntity::Verify() const { - return isImportedEntity() && - (DbgNode->getNumOperands() == 4 || DbgNode->getNumOperands() == 5); + return isImportedEntity() && DbgNode->getNumOperands() == 3 && + getNumHeaderFields() == 3; } -/// getObjCProperty - Return property node, if this ivar is associated with one. MDNode *DIDerivedType::getObjCProperty() const { - return getNodeField(DbgNode, 10); + return getNodeField(DbgNode, 4); } MDString *DICompositeType::getIdentifier() const { - return cast_or_null<MDString>(getField(DbgNode, 14)); + return cast_or_null<MDString>(getField(DbgNode, 7)); } #ifndef NDEBUG static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) { for (unsigned i = 0; i != LHS->getNumOperands(); ++i) { // Skip the 'empty' list (that's a single i32 0, rather than truly empty). - if (i == 0 && isa<ConstantInt>(LHS->getOperand(i))) + if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i))) continue; const MDNode *E = cast<MDNode>(LHS->getOperand(i)); bool found = false; for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j) - found = E == RHS->getOperand(j); + found = (E == cast<MDNode>(RHS->getOperand(j))); assert(found && "Losing a member during member list replacement"); } } #endif -/// \brief Set the array of member DITypes. -void DICompositeType::setTypeArray(DIArray Elements, DIArray TParams) { - assert((!TParams || DbgNode->getNumOperands() == 15) && - "If you're setting the template parameters this should include a slot " - "for that!"); - TrackingVH<MDNode> N(*this); +void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) { + TrackingMDNodeRef N(*this); if (Elements) { #ifndef NDEBUG // Check that the new list of members contains all the old members as well. - if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(10))) + if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4))) VerifySubsetOf(El, Elements); #endif - N->replaceOperandWith(10, Elements); + N->replaceOperandWith(4, Elements); } if (TParams) - N->replaceOperandWith(13, TParams); + N->replaceOperandWith(6, TParams); DbgNode = N; } -/// Generate a reference to this DIType. Uses the type identifier instead -/// of the actual MDNode if possible, to help type uniquing. DIScopeRef DIScope::getRef() const { if (!isCompositeType()) return DIScopeRef(*this); @@ -699,15 +689,12 @@ DIScopeRef DIScope::getRef() const { return DIScopeRef(DTy.getIdentifier()); } -/// \brief Set the containing type. void DICompositeType::setContainingType(DICompositeType ContainingType) { - TrackingVH<MDNode> N(*this); - N->replaceOperandWith(12, ContainingType.getRef()); + TrackingMDNodeRef N(*this); + N->replaceOperandWith(5, ContainingType.getRef()); DbgNode = N; } -/// isInlinedFnArgument - Return true if this variable provides debugging -/// information for an inlined function arguments. bool DIVariable::isInlinedFnArgument(const Function *CurFn) { assert(CurFn && "Invalid function"); if (!getContext().isSubprogram()) @@ -717,8 +704,6 @@ bool DIVariable::isInlinedFnArgument(const Function *CurFn) { return !DISubprogram(getContext()).describes(CurFn); } -/// describes - Return true if this subprogram provides debugging -/// information for the function F. bool DISubprogram::describes(const Function *F) { assert(F && "Invalid function"); if (F == getFunction()) @@ -731,27 +716,18 @@ bool DISubprogram::describes(const Function *F) { return false; } -unsigned DISubprogram::isOptimized() const { - assert(DbgNode && "Invalid subprogram descriptor!"); - if (DbgNode->getNumOperands() == 15) - return getUnsignedField(14); - return 0; -} - MDNode *DISubprogram::getVariablesNodes() const { - return getNodeField(DbgNode, 18); + return getNodeField(DbgNode, 8); } DIArray DISubprogram::getVariables() const { - return DIArray(getNodeField(DbgNode, 18)); + return DIArray(getNodeField(DbgNode, 8)); } -Value *DITemplateValueParameter::getValue() const { - return getField(DbgNode, 4); +Metadata *DITemplateValueParameter::getValue() const { + return DbgNode->getOperand(3); } -// If the current node has a parent scope then return that, -// else return an empty scope. DIScopeRef DIScope::getContext() const { if (isType()) @@ -773,7 +749,6 @@ DIScopeRef DIScope::getContext() const { return DIScopeRef(nullptr); } -// If the scope node has a name, return that, else return an empty string. StringRef DIScope::getName() const { if (isType()) return DIType(DbgNode).getName(); @@ -800,128 +775,103 @@ StringRef DIScope::getDirectory() const { } DIArray DICompileUnit::getEnumTypes() const { - if (!DbgNode || DbgNode->getNumOperands() < 13) + if (!DbgNode || DbgNode->getNumOperands() < 7) return DIArray(); - return DIArray(getNodeField(DbgNode, 7)); + return DIArray(getNodeField(DbgNode, 2)); } DIArray DICompileUnit::getRetainedTypes() const { - if (!DbgNode || DbgNode->getNumOperands() < 13) + if (!DbgNode || DbgNode->getNumOperands() < 7) return DIArray(); - return DIArray(getNodeField(DbgNode, 8)); + return DIArray(getNodeField(DbgNode, 3)); } DIArray DICompileUnit::getSubprograms() const { - if (!DbgNode || DbgNode->getNumOperands() < 13) + if (!DbgNode || DbgNode->getNumOperands() < 7) return DIArray(); - return DIArray(getNodeField(DbgNode, 9)); + return DIArray(getNodeField(DbgNode, 4)); } DIArray DICompileUnit::getGlobalVariables() const { - if (!DbgNode || DbgNode->getNumOperands() < 13) + if (!DbgNode || DbgNode->getNumOperands() < 7) return DIArray(); - return DIArray(getNodeField(DbgNode, 10)); + return DIArray(getNodeField(DbgNode, 5)); } DIArray DICompileUnit::getImportedEntities() const { - if (!DbgNode || DbgNode->getNumOperands() < 13) + if (!DbgNode || DbgNode->getNumOperands() < 7) return DIArray(); - return DIArray(getNodeField(DbgNode, 11)); + return DIArray(getNodeField(DbgNode, 6)); } -/// copyWithNewScope - Return a copy of this location, replacing the -/// current scope with the given one. -DILocation DILocation::copyWithNewScope(LLVMContext &Ctx, - DILexicalBlock NewScope) { - SmallVector<Value *, 10> Elts; - assert(Verify()); - for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) { - if (I != 2) - Elts.push_back(DbgNode->getOperand(I)); - else - Elts.push_back(NewScope); - } - MDNode *NewDIL = MDNode::get(Ctx, Elts); - return DILocation(NewDIL); -} +void DICompileUnit::replaceSubprograms(DIArray Subprograms) { + assert(Verify() && "Expected compile unit"); + if (Subprograms == getSubprograms()) + return; -/// computeNewDiscriminator - Generate a new discriminator value for this -/// file and line location. -unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) { - std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber()); - return ++Ctx.pImpl->DiscriminatorTable[Key]; + const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms); } -/// fixupSubprogramName - Replace contains special characters used -/// in a typical Objective-C names with '.' in a given string. -static void fixupSubprogramName(DISubprogram Fn, SmallVectorImpl<char> &Out) { - StringRef FName = - Fn.getFunction() ? Fn.getFunction()->getName() : Fn.getName(); - FName = Function::getRealLinkageName(FName); - - StringRef Prefix("llvm.dbg.lv."); - Out.reserve(FName.size() + Prefix.size()); - Out.append(Prefix.begin(), Prefix.end()); - - bool isObjCLike = false; - for (size_t i = 0, e = FName.size(); i < e; ++i) { - char C = FName[i]; - if (C == '[') - isObjCLike = true; - - if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' || - C == '+' || C == '(' || C == ')')) - Out.push_back('.'); - else - Out.push_back(C); - } +void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) { + assert(Verify() && "Expected compile unit"); + if (GlobalVariables == getGlobalVariables()) + return; + + const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables); } -/// getFnSpecificMDNode - Return a NameMDNode, if available, that is -/// suitable to hold function specific information. -NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) { - SmallString<32> Name; - fixupSubprogramName(Fn, Name); - return M.getNamedMetadata(Name.str()); +DILocation DILocation::copyWithNewScope(LLVMContext &Ctx, + DILexicalBlockFile NewScope) { + assert(Verify()); + assert(NewScope && "Expected valid scope"); + + const auto *Old = cast<MDLocation>(DbgNode); + return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(), + NewScope, Old->getInlinedAt())); } -/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable -/// to hold function specific information. -NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) { - SmallString<32> Name; - fixupSubprogramName(Fn, Name); - return M.getOrInsertNamedMetadata(Name.str()); +unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) { + std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber()); + return ++Ctx.pImpl->DiscriminatorTable[Key]; } -/// createInlinedVariable - Create a new inlined variable based on current -/// variable. -/// @param DV Current Variable. -/// @param InlinedScope Location at current variable is inlined. DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope, LLVMContext &VMContext) { - SmallVector<Value *, 16> Elts; - // Insert inlined scope as 7th element. - for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i) - i == 7 ? Elts.push_back(InlinedScope) : Elts.push_back(DV->getOperand(i)); - return DIVariable(MDNode::get(VMContext, Elts)); + assert(DIVariable(DV).Verify() && "Expected a DIVariable"); + if (!InlinedScope) + return cleanseInlinedVariable(DV, VMContext); + + // Insert inlined scope. + SmallVector<Metadata *, 8> Elts; + for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I) + Elts.push_back(DV->getOperand(I)); + Elts.push_back(InlinedScope); + + DIVariable Inlined(MDNode::get(VMContext, Elts)); + assert(Inlined.Verify() && "Expected to create a DIVariable"); + return Inlined; } -/// cleanseInlinedVariable - Remove inlined scope from the variable. DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) { - SmallVector<Value *, 16> Elts; - // Insert inlined scope as 7th element. - for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i) - i == 7 ? Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))) - : Elts.push_back(DV->getOperand(i)); - return DIVariable(MDNode::get(VMContext, Elts)); + assert(DIVariable(DV).Verify() && "Expected a DIVariable"); + if (!DIVariable(DV).getInlinedAt()) + return DIVariable(DV); + + // Remove inlined scope. + SmallVector<Metadata *, 8> Elts; + for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I) + Elts.push_back(DV->getOperand(I)); + + DIVariable Cleansed(MDNode::get(VMContext, Elts)); + assert(Cleansed.Verify() && "Expected to create a DIVariable"); + return Cleansed; } -/// getDISubprogram - Find subprogram that is enclosing this scope. DISubprogram llvm::getDISubprogram(const MDNode *Scope) { DIDescriptor D(Scope); if (D.isSubprogram()) @@ -936,7 +886,23 @@ DISubprogram llvm::getDISubprogram(const MDNode *Scope) { return DISubprogram(); } -/// getDICompositeType - Find underlying composite type. +DISubprogram llvm::getDISubprogram(const Function *F) { + // We look for the first instr that has a debug annotation leading back to F. + for (auto &BB : *F) { + auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) { + return !Inst.getDebugLoc().isUnknown(); + }); + if (Inst == BB.end()) + continue; + DebugLoc DLoc = Inst->getDebugLoc(); + const MDNode *Scope = DLoc.getScopeNode(); + DISubprogram Subprogram = getDISubprogram(Scope); + return Subprogram.describes(F) ? Subprogram : DISubprogram(); + } + + return DISubprogram(); +} + DICompositeType llvm::getDICompositeType(DIType T) { if (T.isCompositeType()) return DICompositeType(T); @@ -953,7 +919,6 @@ DICompositeType llvm::getDICompositeType(DIType T) { return DICompositeType(); } -/// Update DITypeIdentifierMap by going through retained types of each CU. DITypeIdentifierMap llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) { DITypeIdentifierMap Map; @@ -1002,7 +967,6 @@ void DebugInfoFinder::InitializeTypeMap(const Module &M) { } } -/// processModule - Process entire module and collect debug info. void DebugInfoFinder::processModule(const Module &M) { InitializeTypeMap(M); if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { @@ -1041,7 +1005,6 @@ void DebugInfoFinder::processModule(const Module &M) { } } -/// processLocation - Process DILocation. void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) { if (!Loc) return; @@ -1050,7 +1013,6 @@ void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) { processLocation(M, Loc.getOrigLocation()); } -/// processType - Process DIType. void DebugInfoFinder::processType(DIType DT) { if (!addType(DT)) return; @@ -1058,7 +1020,13 @@ void DebugInfoFinder::processType(DIType DT) { if (DT.isCompositeType()) { DICompositeType DCT(DT); processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); - DIArray DA = DCT.getTypeArray(); + if (DT.isSubroutineType()) { + DITypeArray DTA = DISubroutineType(DT).getTypeArray(); + for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i) + processType(DTA.getElement(i).resolve(TypeIdentifierMap)); + return; + } + DIArray DA = DCT.getElements(); for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) { DIDescriptor D = DA.getElement(i); if (D.isType()) @@ -1100,7 +1068,6 @@ void DebugInfoFinder::processScope(DIScope Scope) { } } -/// processSubprogram - Process DISubprogram. void DebugInfoFinder::processSubprogram(DISubprogram SP) { if (!addSubprogram(SP)) return; @@ -1121,7 +1088,6 @@ void DebugInfoFinder::processSubprogram(DISubprogram SP) { } } -/// processDeclare - Process DbgDeclareInst. void DebugInfoFinder::processDeclare(const Module &M, const DbgDeclareInst *DDI) { MDNode *N = dyn_cast<MDNode>(DDI->getVariable()); @@ -1133,7 +1099,7 @@ void DebugInfoFinder::processDeclare(const Module &M, if (!DV.isVariable()) return; - if (!NodesSeen.insert(DV)) + if (!NodesSeen.insert(DV).second) return; processScope(DIVariable(N).getContext()); processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); @@ -1149,53 +1115,49 @@ void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { if (!DV.isVariable()) return; - if (!NodesSeen.insert(DV)) + if (!NodesSeen.insert(DV).second) return; processScope(DIVariable(N).getContext()); processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); } -/// addType - Add type into Tys. bool DebugInfoFinder::addType(DIType DT) { if (!DT) return false; - if (!NodesSeen.insert(DT)) + if (!NodesSeen.insert(DT).second) return false; TYs.push_back(DT); return true; } -/// addCompileUnit - Add compile unit into CUs. bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) { if (!CU) return false; - if (!NodesSeen.insert(CU)) + if (!NodesSeen.insert(CU).second) return false; CUs.push_back(CU); return true; } -/// addGlobalVariable - Add global variable into GVs. bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) { if (!DIG) return false; - if (!NodesSeen.insert(DIG)) + if (!NodesSeen.insert(DIG).second) return false; GVs.push_back(DIG); return true; } -// addSubprogram - Add subprgoram into SPs. bool DebugInfoFinder::addSubprogram(DISubprogram SP) { if (!SP) return false; - if (!NodesSeen.insert(SP)) + if (!NodesSeen.insert(SP).second) return false; SPs.push_back(SP); @@ -1209,7 +1171,7 @@ bool DebugInfoFinder::addScope(DIScope Scope) { // as null for now. if (Scope->getNumOperands() == 0) return false; - if (!NodesSeen.insert(Scope)) + if (!NodesSeen.insert(Scope).second) return false; Scopes.push_back(Scope); return true; @@ -1219,13 +1181,11 @@ bool DebugInfoFinder::addScope(DIScope Scope) { // DIDescriptor: dump routines for all descriptors. //===----------------------------------------------------------------------===// -/// dump - Print descriptor to dbgs() with a newline. void DIDescriptor::dump() const { print(dbgs()); dbgs() << '\n'; } -/// print - Print descriptor. void DIDescriptor::print(raw_ostream &OS) const { if (!DbgNode) return; @@ -1259,6 +1219,8 @@ void DIDescriptor::print(raw_ostream &OS) const { DINameSpace(DbgNode).printInternal(OS); } else if (this->isScope()) { DIScope(DbgNode).printInternal(OS); + } else if (this->isExpression()) { + DIExpression(DbgNode).printInternal(OS); } } @@ -1311,6 +1273,8 @@ void DIType::printInternal(raw_ostream &OS) const { OS << " [private]"; else if (isProtected()) OS << " [protected]"; + else if (isPublic()) + OS << " [public]"; if (isArtificial()) OS << " [artificial]"; @@ -1341,7 +1305,7 @@ void DIDerivedType::printInternal(raw_ostream &OS) const { void DICompositeType::printInternal(raw_ostream &OS) const { DIType::printInternal(OS); - DIArray A = getTypeArray(); + DIArray A = getElements(); OS << " [" << A.getNumElements() << " elements]"; } @@ -1370,6 +1334,8 @@ void DISubprogram::printInternal(raw_ostream &OS) const { OS << " [private]"; else if (isProtected()) OS << " [protected]"; + else if (isPublic()) + OS << " [public]"; if (isLValueReference()) OS << " [reference]"; @@ -1406,6 +1372,33 @@ void DIVariable::printInternal(raw_ostream &OS) const { OS << " [line " << getLineNumber() << ']'; } +void DIExpression::printInternal(raw_ostream &OS) const { + for (unsigned I = 0; I < getNumElements(); ++I) { + uint64_t OpCode = getElement(I); + OS << " [" << OperationEncodingString(OpCode); + switch (OpCode) { + case DW_OP_plus: { + OS << " " << getElement(++I); + break; + } + case DW_OP_piece: { + unsigned Offset = getElement(++I); + unsigned Size = getElement(++I); + OS << " offset=" << Offset << ", size=" << Size; + break; + } + case DW_OP_deref: + // No arguments. + break; + default: + // Else bail out early. This may be a line table entry. + OS << "Unknown]"; + return; + } + OS << "]"; + } +} + void DIObjCProperty::printInternal(raw_ostream &OS) const { StringRef Name = getObjCPropertyName(); if (!Name.empty()) @@ -1449,30 +1442,22 @@ void DIVariable::printExtendedName(raw_ostream &OS) const { } } -/// Specialize constructor to make sure it has the correct type. -template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) { +template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) { assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode"); } -template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) { +template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) { assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode"); } -/// Specialize getFieldAs to handle fields that are references to DIScopes. template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const { - return DIScopeRef(getField(DbgNode, Elt)); + return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); } -/// Specialize getFieldAs to handle fields that are references to DITypes. template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const { - return DITypeRef(getField(DbgNode, Elt)); + return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); } -/// Strip debug info in the module if it exists. -/// To do this, we remove all calls to the debugger intrinsics and any named -/// metadata for debugging. We also remove debug locations for instructions. -/// Return true if module is modified. bool llvm::StripDebugInfo(Module &M) { - bool Changed = false; // Remove all of the calls to the debugger intrinsics, and remove them from @@ -1519,12 +1504,11 @@ bool llvm::StripDebugInfo(Module &M) { return Changed; } -/// Return Debug Info Metadata Version by checking module flags. unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { - Value *Val = M.getModuleFlag("Debug Info Version"); - if (!Val) - return 0; - return cast<ConstantInt>(Val)->getZExtValue(); + if (auto *Val = mdconst::extract_or_null<ConstantInt>( + M.getModuleFlag("Debug Info Version"))) + return Val->getZExtValue(); + return 0; } llvm::DenseMap<const llvm::Function *, llvm::DISubprogram> diff --git a/contrib/llvm/lib/IR/DebugLoc.cpp b/contrib/llvm/lib/IR/DebugLoc.cpp index e8bdcce..075f295 100644 --- a/contrib/llvm/lib/IR/DebugLoc.cpp +++ b/contrib/llvm/lib/IR/DebugLoc.cpp @@ -17,129 +17,60 @@ using namespace llvm; // DebugLoc Implementation //===----------------------------------------------------------------------===// -MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const { - if (ScopeIdx == 0) return nullptr; - - if (ScopeIdx > 0) { - // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at - // position specified. - assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() && - "Invalid ScopeIdx!"); - return Ctx.pImpl->ScopeRecords[ScopeIdx-1].get(); - } - - // Otherwise, the index is in the ScopeInlinedAtRecords array. - assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() && - "Invalid ScopeIdx"); - return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get(); -} +unsigned DebugLoc::getLine() const { return DILocation(Loc).getLineNumber(); } +unsigned DebugLoc::getCol() const { return DILocation(Loc).getColumnNumber(); } + +MDNode *DebugLoc::getScope() const { return DILocation(Loc).getScope(); } -MDNode *DebugLoc::getInlinedAt(const LLVMContext &Ctx) const { - // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at - // position specified. Zero is invalid. - if (ScopeIdx >= 0) return nullptr; - - // Otherwise, the index is in the ScopeInlinedAtRecords array. - assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() && - "Invalid ScopeIdx"); - return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get(); +MDNode *DebugLoc::getInlinedAt() const { + return DILocation(Loc).getOrigLocation(); } /// Return both the Scope and the InlinedAt values. -void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA, - const LLVMContext &Ctx) const { - if (ScopeIdx == 0) { - Scope = IA = nullptr; - return; - } - - if (ScopeIdx > 0) { - // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at - // position specified. - assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() && - "Invalid ScopeIdx!"); - Scope = Ctx.pImpl->ScopeRecords[ScopeIdx-1].get(); - IA = nullptr; - return; - } - - // Otherwise, the index is in the ScopeInlinedAtRecords array. - assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() && - "Invalid ScopeIdx"); - Scope = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get(); - IA = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get(); +void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA) const { + Scope = getScope(); + IA = getInlinedAt(); } -MDNode *DebugLoc::getScopeNode(const LLVMContext &Ctx) const { - if (MDNode *InlinedAt = getInlinedAt(Ctx)) - return DebugLoc::getFromDILocation(InlinedAt).getScopeNode(Ctx); - return getScope(Ctx); +MDNode *DebugLoc::getScopeNode() const { + if (MDNode *InlinedAt = getInlinedAt()) + return DebugLoc::getFromDILocation(InlinedAt).getScopeNode(); + return getScope(); } -DebugLoc DebugLoc::getFnDebugLoc(const LLVMContext &Ctx) const { - const MDNode *Scope = getScopeNode(Ctx); +DebugLoc DebugLoc::getFnDebugLoc() const { + const MDNode *Scope = getScopeNode(); DISubprogram SP = getDISubprogram(Scope); - if (SP.isSubprogram()) { - // Check for number of operands since the compatibility is - // cheap here. FIXME: Name the magic constant. - if (SP->getNumOperands() > 19) - return DebugLoc::get(SP.getScopeLineNumber(), 0, SP); - else - return DebugLoc::get(SP.getLineNumber(), 0, SP); - } + if (SP.isSubprogram()) + return DebugLoc::get(SP.getScopeLineNumber(), 0, SP); return DebugLoc(); } DebugLoc DebugLoc::get(unsigned Line, unsigned Col, MDNode *Scope, MDNode *InlinedAt) { - DebugLoc Result; - // If no scope is available, this is an unknown location. - if (!Scope) return Result; + if (!Scope) + return DebugLoc(); // Saturate line and col to "unknown". + // FIXME: Allow 16-bits for columns. if (Col > 255) Col = 0; if (Line >= (1 << 24)) Line = 0; - Result.LineCol = Line | (Col << 24); - - LLVMContext &Ctx = Scope->getContext(); - - // If there is no inlined-at location, use the ScopeRecords array. - if (!InlinedAt) - Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0); - else - Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope, - InlinedAt, 0); - return Result; + return getFromDILocation( + MDLocation::get(Scope->getContext(), Line, Col, Scope, InlinedAt)); } /// getAsMDNode - This method converts the compressed DebugLoc node into a /// DILocation-compatible MDNode. -MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const { - if (isUnknown()) return nullptr; - - MDNode *Scope, *IA; - getScopeAndInlinedAt(Scope, IA, Ctx); - assert(Scope && "If scope is null, this should be isUnknown()"); - - LLVMContext &Ctx2 = Scope->getContext(); - Type *Int32 = Type::getInt32Ty(Ctx2); - Value *Elts[] = { - ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()), - Scope, IA - }; - return MDNode::get(Ctx2, Elts); -} +MDNode *DebugLoc::getAsMDNode() const { return Loc; } /// getFromDILocation - Translate the DILocation quad into a DebugLoc. DebugLoc DebugLoc::getFromDILocation(MDNode *N) { - DILocation Loc(N); - MDNode *Scope = Loc.getScope(); - if (!Scope) return DebugLoc(); - return get(Loc.getLineNumber(), Loc.getColumnNumber(), Scope, - Loc.getOrigLocation()); + DebugLoc Loc; + Loc.Loc.reset(N); + return Loc; } /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc. @@ -151,26 +82,26 @@ DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) { nullptr); } -void DebugLoc::dump(const LLVMContext &Ctx) const { +void DebugLoc::dump() const { #ifndef NDEBUG if (!isUnknown()) { dbgs() << getLine(); if (getCol() != 0) dbgs() << ',' << getCol(); - DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx)); + DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt()); if (!InlinedAtDL.isUnknown()) { dbgs() << " @ "; - InlinedAtDL.dump(Ctx); + InlinedAtDL.dump(); } else dbgs() << "\n"; } #endif } -void DebugLoc::print(const LLVMContext &Ctx, raw_ostream &OS) const { +void DebugLoc::print(raw_ostream &OS) const { if (!isUnknown()) { // Print source line info. - DIScope Scope(getScope(Ctx)); + DIScope Scope(getScope()); assert((!Scope || Scope.isScope()) && "Scope of a DebugLoc should be null or a DIScope."); if (Scope) @@ -180,179 +111,11 @@ void DebugLoc::print(const LLVMContext &Ctx, raw_ostream &OS) const { OS << ':' << getLine(); if (getCol() != 0) OS << ':' << getCol(); - DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx)); + DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt()); if (!InlinedAtDL.isUnknown()) { OS << " @[ "; - InlinedAtDL.print(Ctx, OS); + InlinedAtDL.print(OS); OS << " ]"; } } } - -//===----------------------------------------------------------------------===// -// DenseMap specialization -//===----------------------------------------------------------------------===// - -unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) { - return static_cast<unsigned>(hash_combine(Key.LineCol, Key.ScopeIdx)); -} - -//===----------------------------------------------------------------------===// -// LLVMContextImpl Implementation -//===----------------------------------------------------------------------===// - -int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope, - int ExistingIdx) { - // If we already have an entry for this scope, return it. - int &Idx = ScopeRecordIdx[Scope]; - if (Idx) return Idx; - - // If we don't have an entry, but ExistingIdx is specified, use it. - if (ExistingIdx) - return Idx = ExistingIdx; - - // Otherwise add a new entry. - - // Start out ScopeRecords with a minimal reasonable size to avoid - // excessive reallocation starting out. - if (ScopeRecords.empty()) - ScopeRecords.reserve(128); - - // Index is biased by 1 for index. - Idx = ScopeRecords.size()+1; - ScopeRecords.push_back(DebugRecVH(Scope, this, Idx)); - return Idx; -} - -int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA, - int ExistingIdx) { - // If we already have an entry, return it. - int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)]; - if (Idx) return Idx; - - // If we don't have an entry, but ExistingIdx is specified, use it. - if (ExistingIdx) - return Idx = ExistingIdx; - - // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid - // excessive reallocation starting out. - if (ScopeInlinedAtRecords.empty()) - ScopeInlinedAtRecords.reserve(128); - - // Index is biased by 1 and negated. - Idx = -ScopeInlinedAtRecords.size()-1; - ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx), - DebugRecVH(IA, this, Idx))); - return Idx; -} - - -//===----------------------------------------------------------------------===// -// DebugRecVH Implementation -//===----------------------------------------------------------------------===// - -/// deleted - The MDNode this is pointing to got deleted, so this pointer needs -/// to drop to null and we need remove our entry from the DenseMap. -void DebugRecVH::deleted() { - // If this is a non-canonical reference, just drop the value to null, we know - // it doesn't have a map entry. - if (Idx == 0) { - setValPtr(nullptr); - return; - } - - MDNode *Cur = get(); - - // If the index is positive, it is an entry in ScopeRecords. - if (Idx > 0) { - assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!"); - Ctx->ScopeRecordIdx.erase(Cur); - // Reset this VH to null and we're done. - setValPtr(nullptr); - Idx = 0; - return; - } - - // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it - // is the scope or the inlined-at record entry. - assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size()); - std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1]; - assert((this == &Entry.first || this == &Entry.second) && - "Mapping out of date!"); - - MDNode *OldScope = Entry.first.get(); - MDNode *OldInlinedAt = Entry.second.get(); - assert(OldScope && OldInlinedAt && - "Entry should be non-canonical if either val dropped to null"); - - // Otherwise, we do have an entry in it, nuke it and we're done. - assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&& - "Mapping out of date"); - Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt)); - - // Reset this VH to null. Drop both 'Idx' values to null to indicate that - // we're in non-canonical form now. - setValPtr(nullptr); - Entry.first.Idx = Entry.second.Idx = 0; -} - -void DebugRecVH::allUsesReplacedWith(Value *NewVa) { - // If being replaced with a non-mdnode value (e.g. undef) handle this as if - // the mdnode got deleted. - MDNode *NewVal = dyn_cast<MDNode>(NewVa); - if (!NewVal) return deleted(); - - // If this is a non-canonical reference, just change it, we know it already - // doesn't have a map entry. - if (Idx == 0) { - setValPtr(NewVa); - return; - } - - MDNode *OldVal = get(); - assert(OldVal != NewVa && "Node replaced with self?"); - - // If the index is positive, it is an entry in ScopeRecords. - if (Idx > 0) { - assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!"); - Ctx->ScopeRecordIdx.erase(OldVal); - setValPtr(NewVal); - - int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx); - - // If NewVal already has an entry, this becomes a non-canonical reference, - // just drop Idx to 0 to signify this. - if (NewEntry != Idx) - Idx = 0; - return; - } - - // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it - // is the scope or the inlined-at record entry. - assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size()); - std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1]; - assert((this == &Entry.first || this == &Entry.second) && - "Mapping out of date!"); - - MDNode *OldScope = Entry.first.get(); - MDNode *OldInlinedAt = Entry.second.get(); - assert(OldScope && OldInlinedAt && - "Entry should be non-canonical if either val dropped to null"); - - // Otherwise, we do have an entry in it, nuke it and we're done. - assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&& - "Mapping out of date"); - Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt)); - - // Reset this VH to the new value. - setValPtr(NewVal); - - int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(), - Entry.second.get(), Idx); - // If NewVal already has an entry, this becomes a non-canonical reference, - // just drop Idx to 0 to signify this. - if (NewIdx != Idx) { - std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1]; - Entry.first.Idx = Entry.second.Idx = 0; - } -} diff --git a/contrib/llvm/lib/IR/DiagnosticInfo.cpp b/contrib/llvm/lib/IR/DiagnosticInfo.cpp index 37cce2b..cfb699a 100644 --- a/contrib/llvm/lib/IR/DiagnosticInfo.cpp +++ b/contrib/llvm/lib/IR/DiagnosticInfo.cpp @@ -98,7 +98,8 @@ DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I, Instr(&I) { if (const MDNode *SrcLoc = I.getMetadata("srcloc")) { if (SrcLoc->getNumOperands() != 0) - if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0))) + if (const auto *CI = + mdconst::dyn_extract<ConstantInt>(SrcLoc->getOperand(0))) LocCookie = CI->getZExtValue(); } } diff --git a/contrib/llvm/lib/IR/DiagnosticPrinter.cpp b/contrib/llvm/lib/IR/DiagnosticPrinter.cpp index 5e16026..f25fc20 100644 --- a/contrib/llvm/lib/IR/DiagnosticPrinter.cpp +++ b/contrib/llvm/lib/IR/DiagnosticPrinter.cpp @@ -7,7 +7,7 @@ // //===----------------------------------------------------------------------===// // -// This file defines the a diagnostic printer relying on raw_ostream. +// This file defines a diagnostic printer relying on raw_ostream. // //===----------------------------------------------------------------------===// diff --git a/contrib/llvm/lib/IR/Dominators.cpp b/contrib/llvm/lib/IR/Dominators.cpp index d6649d6..9b6ff1e 100644 --- a/contrib/llvm/lib/IR/Dominators.cpp +++ b/contrib/llvm/lib/IR/Dominators.cpp @@ -20,6 +20,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/IR/CFG.h" #include "llvm/IR/Instructions.h" +#include "llvm/IR/PassManager.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/Debug.h" @@ -298,10 +299,45 @@ void DominatorTree::verifyDomTree() const { } //===----------------------------------------------------------------------===// +// DominatorTreeAnalysis and related pass implementations +//===----------------------------------------------------------------------===// +// +// This implements the DominatorTreeAnalysis which is used with the new pass +// manager. It also implements some methods from utility passes. +// +//===----------------------------------------------------------------------===// + +DominatorTree DominatorTreeAnalysis::run(Function &F) { + DominatorTree DT; + DT.recalculate(F); + return DT; +} + +char DominatorTreeAnalysis::PassID; + +DominatorTreePrinterPass::DominatorTreePrinterPass(raw_ostream &OS) : OS(OS) {} + +PreservedAnalyses DominatorTreePrinterPass::run(Function &F, + FunctionAnalysisManager *AM) { + OS << "DominatorTree for function: " << F.getName() << "\n"; + AM->getResult<DominatorTreeAnalysis>(F).print(OS); + + return PreservedAnalyses::all(); +} + +PreservedAnalyses DominatorTreeVerifierPass::run(Function &F, + FunctionAnalysisManager *AM) { + AM->getResult<DominatorTreeAnalysis>(F).verifyDomTree(); + + return PreservedAnalyses::all(); +} + +//===----------------------------------------------------------------------===// // DominatorTreeWrapperPass Implementation //===----------------------------------------------------------------------===// // -// The implementation details of the wrapper pass that holds a DominatorTree. +// The implementation details of the wrapper pass that holds a DominatorTree +// suitable for use with the legacy pass manager. // //===----------------------------------------------------------------------===// diff --git a/contrib/llvm/lib/IR/Function.cpp b/contrib/llvm/lib/IR/Function.cpp index de59b26..bfd0ca6 100644 --- a/contrib/llvm/lib/IR/Function.cpp +++ b/contrib/llvm/lib/IR/Function.cpp @@ -23,7 +23,6 @@ #include "llvm/IR/InstIterator.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/LeakDetector.h" #include "llvm/IR/Module.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/RWMutex.h" @@ -46,20 +45,13 @@ Argument::Argument(Type *Ty, const Twine &Name, Function *Par) : Value(Ty, Value::ArgumentVal) { Parent = nullptr; - // Make sure that we get added to a function - LeakDetector::addGarbageObject(this); - if (Par) Par->getArgumentList().push_back(this); setName(Name); } void Argument::setParent(Function *parent) { - if (getParent()) - LeakDetector::addGarbageObject(this); Parent = parent; - if (getParent()) - LeakDetector::removeGarbageObject(this); } /// getArgNo - Return the index of this formal argument in its containing @@ -166,6 +158,20 @@ bool Argument::hasReturnedAttr() const { hasAttribute(getArgNo()+1, Attribute::Returned); } +/// hasZExtAttr - Return true if this argument has the zext attribute on it in +/// its containing function. +bool Argument::hasZExtAttr() const { + return getParent()->getAttributes(). + hasAttribute(getArgNo()+1, Attribute::ZExt); +} + +/// hasSExtAttr Return true if this argument has the sext attribute on it in its +/// containing function. +bool Argument::hasSExtAttr() const { + return getParent()->getAttributes(). + hasAttribute(getArgNo()+1, Attribute::SExt); +} + /// Return true if this argument has the readonly or readnone attribute on it /// in its containing function. bool Argument::onlyReadsMemory() const { @@ -199,6 +205,12 @@ void Argument::removeAttr(AttributeSet AS) { // Helper Methods in Function //===----------------------------------------------------------------------===// +bool Function::isMaterializable() const { + return getGlobalObjectSubClassData(); +} + +void Function::setIsMaterializable(bool V) { setGlobalObjectSubClassData(V); } + LLVMContext &Function::getContext() const { return getType()->getContext(); } @@ -227,21 +239,19 @@ void Function::eraseFromParent() { // Function Implementation //===----------------------------------------------------------------------===// -Function::Function(FunctionType *Ty, LinkageTypes Linkage, - const Twine &name, Module *ParentModule) - : GlobalObject(PointerType::getUnqual(Ty), - Value::FunctionVal, nullptr, 0, Linkage, name) { +Function::Function(FunctionType *Ty, LinkageTypes Linkage, const Twine &name, + Module *ParentModule) + : GlobalObject(PointerType::getUnqual(Ty), Value::FunctionVal, nullptr, 0, + Linkage, name) { assert(FunctionType::isValidReturnType(getReturnType()) && "invalid return type"); + setIsMaterializable(false); SymTab = new ValueSymbolTable(); // If the function has arguments, mark them as lazily built. if (Ty->getNumParams()) setValueSubclassData(1); // Set the "has lazy arguments" bit. - // Make sure that we get added to a function - LeakDetector::addGarbageObject(this); - if (ParentModule) ParentModule->getFunctionList().push_back(this); @@ -277,7 +287,7 @@ void Function::BuildLazyArguments() const { // Clear the lazy arguments bit. unsigned SDC = getSubclassDataFromValue(); - const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1); + const_cast<Function*>(this)->setValueSubclassData(SDC &= ~(1<<0)); } size_t Function::arg_size() const { @@ -288,11 +298,7 @@ bool Function::arg_empty() const { } void Function::setParent(Module *parent) { - if (getParent()) - LeakDetector::addGarbageObject(this); Parent = parent; - if (getParent()) - LeakDetector::removeGarbageObject(this); } // dropAllReferences() - This function causes all the subinstructions to "let @@ -304,6 +310,8 @@ void Function::setParent(Module *parent) { // delete. // void Function::dropAllReferences() { + setIsMaterializable(false); + for (iterator I = begin(), E = end(); I != E; ++I) I->dropAllReferences(); @@ -312,8 +320,9 @@ void Function::dropAllReferences() { while (!BasicBlocks.empty()) BasicBlocks.begin()->eraseFromParent(); - // Prefix data is stored in a side table. + // Prefix and prologue data are stored in a side table. setPrefixData(nullptr); + setPrologueData(nullptr); } void Function::addAttribute(unsigned i, Attribute::AttrKind attr) { @@ -393,6 +402,10 @@ void Function::copyAttributesFrom(const GlobalValue *Src) { setPrefixData(SrcF->getPrefixData()); else setPrefixData(nullptr); + if (SrcF->hasPrologueData()) + setPrologueData(SrcF->getPrologueData()); + else + setPrologueData(nullptr); } /// getIntrinsicID - This method returns the ID number of the specified @@ -432,6 +445,42 @@ unsigned Function::lookupIntrinsicID() const { return 0; } +/// Returns a stable mangling for the type specified for use in the name +/// mangling scheme used by 'any' types in intrinsic signatures. The mangling +/// of named types is simply their name. Manglings for unnamed types consist +/// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions) +/// combined with the mangling of their component types. A vararg function +/// type will have a suffix of 'vararg'. Since function types can contain +/// other function types, we close a function type mangling with suffix 'f' +/// which can't be confused with it's prefix. This ensures we don't have +/// collisions between two unrelated function types. Otherwise, you might +/// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.) +static std::string getMangledTypeStr(Type* Ty) { + std::string Result; + if (PointerType* PTyp = dyn_cast<PointerType>(Ty)) { + Result += "p" + llvm::utostr(PTyp->getAddressSpace()) + + getMangledTypeStr(PTyp->getElementType()); + } else if (ArrayType* ATyp = dyn_cast<ArrayType>(Ty)) { + Result += "a" + llvm::utostr(ATyp->getNumElements()) + + getMangledTypeStr(ATyp->getElementType()); + } else if (StructType* STyp = dyn_cast<StructType>(Ty)) { + if (!STyp->isLiteral()) + Result += STyp->getName(); + else + llvm_unreachable("TODO: implement literal types"); + } else if (FunctionType* FT = dyn_cast<FunctionType>(Ty)) { + Result += "f_" + getMangledTypeStr(FT->getReturnType()); + for (size_t i = 0; i < FT->getNumParams(); i++) + Result += getMangledTypeStr(FT->getParamType(i)); + if (FT->isVarArg()) + Result += "vararg"; + // Ensure nested function types are distinguishable. + Result += "f"; + } else if (Ty) + Result += EVT::getEVT(Ty).getEVTString(); + return Result; +} + std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) { assert(id < num_intrinsics && "Invalid intrinsic ID!"); static const char * const Table[] = { @@ -444,12 +493,7 @@ std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) { return Table[id]; std::string Result(Table[id]); for (unsigned i = 0; i < Tys.size(); ++i) { - if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) { - Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) + - EVT::getEVT(PTyp->getElementType()).getEVTString(); - } - else if (Tys[i]) - Result += "." + EVT::getEVT(Tys[i]).getEVTString(); + Result += "." + getMangledTypeStr(Tys[i]); } return Result; } @@ -479,19 +523,22 @@ enum IIT_Info { IIT_ARG = 15, // Values from 16+ are only encodable with the inefficient encoding. - IIT_MMX = 16, - IIT_METADATA = 17, - IIT_EMPTYSTRUCT = 18, - IIT_STRUCT2 = 19, - IIT_STRUCT3 = 20, - IIT_STRUCT4 = 21, - IIT_STRUCT5 = 22, - IIT_EXTEND_ARG = 23, - IIT_TRUNC_ARG = 24, - IIT_ANYPTR = 25, - IIT_V1 = 26, - IIT_VARARG = 27, - IIT_HALF_VEC_ARG = 28 + IIT_V64 = 16, + IIT_MMX = 17, + IIT_METADATA = 18, + IIT_EMPTYSTRUCT = 19, + IIT_STRUCT2 = 20, + IIT_STRUCT3 = 21, + IIT_STRUCT4 = 22, + IIT_STRUCT5 = 23, + IIT_EXTEND_ARG = 24, + IIT_TRUNC_ARG = 25, + IIT_ANYPTR = 26, + IIT_V1 = 27, + IIT_VARARG = 28, + IIT_HALF_VEC_ARG = 29, + IIT_SAME_VEC_WIDTH_ARG = 30, + IIT_PTR_TO_ARG = 31 }; @@ -562,6 +609,10 @@ static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos, OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32)); DecodeIITType(NextElt, Infos, OutputTable); return; + case IIT_V64: + OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 64)); + DecodeIITType(NextElt, Infos, OutputTable); + return; case IIT_PTR: OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0)); DecodeIITType(NextElt, Infos, OutputTable); @@ -595,6 +646,18 @@ static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos, ArgInfo)); return; } + case IIT_SAME_VEC_WIDTH_ARG: { + unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); + OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument, + ArgInfo)); + return; + } + case IIT_PTR_TO_ARG: { + unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); + OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument, + ArgInfo)); + return; + } case IIT_EMPTYSTRUCT: OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0)); return; @@ -678,7 +741,7 @@ static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos, assert(D.Struct_NumElements <= 5 && "Can't handle this yet"); for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) Elts[i] = DecodeFixedType(Infos, Tys, Context); - return StructType::get(Context, ArrayRef<Type*>(Elts,D.Struct_NumElements)); + return StructType::get(Context, makeArrayRef(Elts,D.Struct_NumElements)); } case IITDescriptor::Argument: @@ -702,7 +765,19 @@ static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos, case IITDescriptor::HalfVecArgument: return VectorType::getHalfElementsVectorType(cast<VectorType>( Tys[D.getArgumentNumber()])); + case IITDescriptor::SameVecWidthArgument: { + Type *EltTy = DecodeFixedType(Infos, Tys, Context); + Type *Ty = Tys[D.getArgumentNumber()]; + if (VectorType *VTy = dyn_cast<VectorType>(Ty)) { + return VectorType::get(EltTy, VTy->getNumElements()); + } + llvm_unreachable("unhandled"); + } + case IITDescriptor::PtrToArgument: { + Type *Ty = Tys[D.getArgumentNumber()]; + return PointerType::getUnqual(Ty); } + } llvm_unreachable("unhandled"); } @@ -720,6 +795,12 @@ FunctionType *Intrinsic::getType(LLVMContext &Context, while (!TableRef.empty()) ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context)); + // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg + // If we see void type as the type of the last argument, it is vararg intrinsic + if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) { + ArgTys.pop_back(); + return FunctionType::get(ResultTy, ArgTys, true); + } return FunctionType::get(ResultTy, ArgTys, false); } @@ -815,11 +896,40 @@ void Function::setPrefixData(Constant *PrefixData) { PDHolder->setOperand(0, PrefixData); else PDHolder = ReturnInst::Create(getContext(), PrefixData); - SCData |= 2; + SCData |= (1<<1); } else { delete PDHolder; PDMap.erase(this); - SCData &= ~2; + SCData &= ~(1<<1); } setValueSubclassData(SCData); } + +Constant *Function::getPrologueData() const { + assert(hasPrologueData()); + const LLVMContextImpl::PrologueDataMapTy &SOMap = + getContext().pImpl->PrologueDataMap; + assert(SOMap.find(this) != SOMap.end()); + return cast<Constant>(SOMap.find(this)->second->getReturnValue()); +} + +void Function::setPrologueData(Constant *PrologueData) { + if (!PrologueData && !hasPrologueData()) + return; + + unsigned PDData = getSubclassDataFromValue(); + LLVMContextImpl::PrologueDataMapTy &PDMap = getContext().pImpl->PrologueDataMap; + ReturnInst *&PDHolder = PDMap[this]; + if (PrologueData) { + if (PDHolder) + PDHolder->setOperand(0, PrologueData); + else + PDHolder = ReturnInst::Create(getContext(), PrologueData); + PDData |= (1<<2); + } else { + delete PDHolder; + PDMap.erase(this); + PDData &= ~(1<<2); + } + setValueSubclassData(PDData); +} diff --git a/contrib/llvm/lib/IR/GCOV.cpp b/contrib/llvm/lib/IR/GCOV.cpp index 1667401..245c500 100644 --- a/contrib/llvm/lib/IR/GCOV.cpp +++ b/contrib/llvm/lib/IR/GCOV.cpp @@ -298,7 +298,8 @@ uint64_t GCOVFunction::getExitCount() const { /// dump - Dump GCOVFunction content to dbgs() for debugging purposes. void GCOVFunction::dump() const { - dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n"; + dbgs() << "===== " << Name << " (" << Ident << ") @ " << Filename << ":" + << LineNumber << "\n"; for (const auto &Block : Blocks) Block->dump(); } @@ -517,11 +518,11 @@ FileInfo::openCoveragePath(StringRef CoveragePath) { if (Options.NoOutput) return llvm::make_unique<raw_null_ostream>(); - std::string ErrorInfo; - auto OS = llvm::make_unique<raw_fd_ostream>(CoveragePath.str().c_str(), - ErrorInfo, sys::fs::F_Text); - if (!ErrorInfo.empty()) { - errs() << ErrorInfo << "\n"; + std::error_code EC; + auto OS = llvm::make_unique<raw_fd_ostream>(CoveragePath.str(), EC, + sys::fs::F_Text); + if (EC) { + errs() << EC.message() << "\n"; return llvm::make_unique<raw_null_ostream>(); } return std::move(OS); diff --git a/contrib/llvm/lib/IR/Globals.cpp b/contrib/llvm/lib/IR/Globals.cpp index 244e3e4..54197d9 100644 --- a/contrib/llvm/lib/IR/Globals.cpp +++ b/contrib/llvm/lib/IR/Globals.cpp @@ -18,7 +18,6 @@ #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/GlobalAlias.h" #include "llvm/IR/GlobalVariable.h" -#include "llvm/IR/LeakDetector.h" #include "llvm/IR/Module.h" #include "llvm/IR/Operator.h" #include "llvm/Support/ErrorHandling.h" @@ -29,13 +28,15 @@ using namespace llvm; //===----------------------------------------------------------------------===// bool GlobalValue::isMaterializable() const { - return getParent() && getParent()->isMaterializable(this); + if (const Function *F = dyn_cast<Function>(this)) + return F->isMaterializable(); + return false; } bool GlobalValue::isDematerializable() const { return getParent() && getParent()->isDematerializable(this); } -bool GlobalValue::Materialize(std::string *ErrInfo) { - return getParent()->Materialize(this, ErrInfo); +std::error_code GlobalValue::materialize() { + return getParent()->materialize(this); } void GlobalValue::Dematerialize() { getParent()->Dematerialize(this); @@ -77,10 +78,24 @@ void GlobalObject::setAlignment(unsigned Align) { assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); assert(Align <= MaximumAlignment && "Alignment is greater than MaximumAlignment!"); - setGlobalValueSubClassData(Log2_32(Align) + 1); + unsigned AlignmentData = Log2_32(Align) + 1; + unsigned OldData = getGlobalValueSubClassData(); + setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData); assert(getAlignment() == Align && "Alignment representation error!"); } +unsigned GlobalObject::getGlobalObjectSubClassData() const { + unsigned ValueData = getGlobalValueSubClassData(); + return ValueData >> AlignmentBits; +} + +void GlobalObject::setGlobalObjectSubClassData(unsigned Val) { + unsigned OldData = getGlobalValueSubClassData(); + setGlobalValueSubClassData((OldData & AlignmentMask) | + (Val << AlignmentBits)); + assert(getGlobalObjectSubClassData() == Val && "representation error"); +} + void GlobalObject::copyAttributesFrom(const GlobalValue *Src) { const auto *GV = cast<GlobalObject>(Src); GlobalValue::copyAttributesFrom(GV); @@ -117,7 +132,7 @@ bool GlobalValue::isDeclaration() const { // Functions are definitions if they have a body. if (const Function *F = dyn_cast<Function>(this)) - return F->empty(); + return F->empty() && !F->isMaterializable(); // Aliases are always definitions. assert(isa<GlobalAlias>(this)); @@ -143,8 +158,6 @@ GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link, "Initializer should be the same type as the GlobalVariable!"); Op<0>() = InitVal; } - - LeakDetector::addGarbageObject(this); } GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, @@ -164,8 +177,6 @@ GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, Op<0>() = InitVal; } - LeakDetector::addGarbageObject(this); - if (Before) Before->getParent()->getGlobalList().insert(Before, this); else @@ -173,11 +184,7 @@ GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, } void GlobalVariable::setParent(Module *parent) { - if (getParent()) - LeakDetector::addGarbageObject(this); Parent = parent; - if (getParent()) - LeakDetector::removeGarbageObject(this); } void GlobalVariable::removeFromParent() { @@ -230,6 +237,7 @@ void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) { GlobalObject::copyAttributesFrom(Src); const GlobalVariable *SrcVar = cast<GlobalVariable>(Src); setThreadLocalMode(SrcVar->getThreadLocalMode()); + setExternallyInitialized(SrcVar->isExternallyInitialized()); } @@ -242,7 +250,6 @@ GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link, Module *ParentModule) : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) { - LeakDetector::addGarbageObject(this); Op<0>() = Aliasee; if (ParentModule) @@ -279,11 +286,7 @@ GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) { } void GlobalAlias::setParent(Module *parent) { - if (getParent()) - LeakDetector::addGarbageObject(this); Parent = parent; - if (getParent()) - LeakDetector::removeGarbageObject(this); } void GlobalAlias::removeFromParent() { diff --git a/contrib/llvm/lib/IR/IRBuilder.cpp b/contrib/llvm/lib/IR/IRBuilder.cpp index 435e54f..ef1f226 100644 --- a/contrib/llvm/lib/IR/IRBuilder.cpp +++ b/contrib/llvm/lib/IR/IRBuilder.cpp @@ -53,8 +53,9 @@ Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) { } static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops, - IRBuilderBase *Builder) { - CallInst *CI = CallInst::Create(Callee, Ops, ""); + IRBuilderBase *Builder, + const Twine& Name="") { + CallInst *CI = CallInst::Create(Callee, Ops, Name); Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI); Builder->SetInstDebugLocation(CI); return CI; @@ -62,7 +63,8 @@ static CallInst *createCallHelper(Value *Callee, ArrayRef<Value *> Ops, CallInst *IRBuilderBase:: CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align, - bool isVolatile, MDNode *TBAATag) { + bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag, + MDNode *NoAliasTag) { Ptr = getCastedInt8PtrValue(Ptr); Value *Ops[] = { Ptr, Val, Size, getInt32(Align), getInt1(isVolatile) }; Type *Tys[] = { Ptr->getType(), Size->getType() }; @@ -74,13 +76,20 @@ CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align, // Set the TBAA info if present. if (TBAATag) CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); - + + if (ScopeTag) + CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); + + if (NoAliasTag) + CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); + return CI; } CallInst *IRBuilderBase:: CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align, - bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag) { + bool isVolatile, MDNode *TBAATag, MDNode *TBAAStructTag, + MDNode *ScopeTag, MDNode *NoAliasTag) { Dst = getCastedInt8PtrValue(Dst); Src = getCastedInt8PtrValue(Src); @@ -98,13 +107,20 @@ CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align, // Set the TBAA Struct info if present. if (TBAAStructTag) CI->setMetadata(LLVMContext::MD_tbaa_struct, TBAAStructTag); - + + if (ScopeTag) + CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); + + if (NoAliasTag) + CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); + return CI; } CallInst *IRBuilderBase:: CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align, - bool isVolatile, MDNode *TBAATag) { + bool isVolatile, MDNode *TBAATag, MDNode *ScopeTag, + MDNode *NoAliasTag) { Dst = getCastedInt8PtrValue(Dst); Src = getCastedInt8PtrValue(Src); @@ -118,7 +134,13 @@ CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align, // Set the TBAA info if present. if (TBAATag) CI->setMetadata(LLVMContext::MD_tbaa, TBAATag); - + + if (ScopeTag) + CI->setMetadata(LLVMContext::MD_alias_scope, ScopeTag); + + if (NoAliasTag) + CI->setMetadata(LLVMContext::MD_noalias, NoAliasTag); + return CI; } @@ -151,3 +173,126 @@ CallInst *IRBuilderBase::CreateLifetimeEnd(Value *Ptr, ConstantInt *Size) { Value *TheFn = Intrinsic::getDeclaration(M, Intrinsic::lifetime_end); return createCallHelper(TheFn, Ops, this); } + +CallInst *IRBuilderBase::CreateAssumption(Value *Cond) { + assert(Cond->getType() == getInt1Ty() && + "an assumption condition must be of type i1"); + + Value *Ops[] = { Cond }; + Module *M = BB->getParent()->getParent(); + Value *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume); + return createCallHelper(FnAssume, Ops, this); +} + +/// Create a call to a Masked Load intrinsic. +/// Ptr - the base pointer for the load +/// Align - alignment of the source location +/// Mask - an vector of booleans which indicates what vector lanes should +/// be accessed in memory +/// PassThru - a pass-through value that is used to fill the masked-off lanes +/// of the result +/// Name - name of the result variable +CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align, + Value *Mask, Value *PassThru, + const Twine &Name) { + assert(Ptr->getType()->isPointerTy() && "Ptr must be of pointer type"); + // DataTy is the overloaded type + Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType(); + assert(DataTy->isVectorTy() && "Ptr should point to a vector"); + if (!PassThru) + PassThru = UndefValue::get(DataTy); + Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru}; + return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, DataTy, Name); +} + +/// Create a call to a Masked Store intrinsic. +/// Val - the data to be stored, +/// Ptr - the base pointer for the store +/// Align - alignment of the destination location +/// Mask - an vector of booleans which indicates what vector lanes should +/// be accessed in memory +CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr, + unsigned Align, Value *Mask) { + Value *Ops[] = { Val, Ptr, getInt32(Align), Mask }; + // Type of the data to be stored - the only one overloaded type + return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, Val->getType()); +} + +/// Create a call to a Masked intrinsic, with given intrinsic Id, +/// an array of operands - Ops, and one overloaded type - DataTy +CallInst *IRBuilderBase::CreateMaskedIntrinsic(unsigned Id, + ArrayRef<Value *> Ops, + Type *DataTy, + const Twine &Name) { + Module *M = BB->getParent()->getParent(); + Type *OverloadedTypes[] = { DataTy }; + Value *TheFn = Intrinsic::getDeclaration(M, (Intrinsic::ID)Id, OverloadedTypes); + return createCallHelper(TheFn, Ops, this, Name); +} + +CallInst *IRBuilderBase::CreateGCStatepoint(Value *ActualCallee, + ArrayRef<Value*> CallArgs, + ArrayRef<Value*> DeoptArgs, + ArrayRef<Value*> GCArgs, + const Twine& Name) { + // Extract out the type of the callee. + PointerType *FuncPtrType = cast<PointerType>(ActualCallee->getType()); + assert(isa<FunctionType>(FuncPtrType->getElementType()) && + "actual callee must be a callable value"); + + + Module *M = BB->getParent()->getParent(); + // Fill in the one generic type'd argument (the function is also vararg) + Type *ArgTypes[] = { FuncPtrType }; + Function *FnStatepoint = + Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint, + ArgTypes); + + std::vector<llvm::Value *> args; + args.push_back(ActualCallee); + args.push_back(getInt32(CallArgs.size())); + args.push_back(getInt32(0 /*unused*/)); + args.insert(args.end(), CallArgs.begin(), CallArgs.end()); + args.push_back(getInt32(DeoptArgs.size())); + args.insert(args.end(), DeoptArgs.begin(), DeoptArgs.end()); + args.insert(args.end(), GCArgs.begin(), GCArgs.end()); + + return createCallHelper(FnStatepoint, args, this, Name); +} + +CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint, + Type *ResultType, + const Twine &Name) { + Intrinsic::ID ID; + if (ResultType->isIntegerTy()) { + ID = Intrinsic::experimental_gc_result_int; + } else if (ResultType->isFloatingPointTy()) { + ID = Intrinsic::experimental_gc_result_float; + } else if (ResultType->isPointerTy()) { + ID = Intrinsic::experimental_gc_result_ptr; + } else { + llvm_unreachable("unimplemented result type for gc.result"); + } + Module *M = BB->getParent()->getParent(); + Type *Types[] = {ResultType}; + Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types); + + Value *Args[] = {Statepoint}; + return createCallHelper(FnGCResult, Args, this, Name); +} + +CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint, + int BaseOffset, + int DerivedOffset, + Type *ResultType, + const Twine &Name) { + Module *M = BB->getParent()->getParent(); + Type *Types[] = {ResultType}; + Value *FnGCRelocate = + Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types); + + Value *Args[] = {Statepoint, + getInt32(BaseOffset), + getInt32(DerivedOffset)}; + return createCallHelper(FnGCRelocate, Args, this, Name); +} diff --git a/contrib/llvm/lib/IR/IRPrintingPasses.cpp b/contrib/llvm/lib/IR/IRPrintingPasses.cpp index c8a1747..91ccfbb 100644 --- a/contrib/llvm/lib/IR/IRPrintingPasses.cpp +++ b/contrib/llvm/lib/IR/IRPrintingPasses.cpp @@ -24,8 +24,8 @@ PrintModulePass::PrintModulePass() : OS(dbgs()) {} PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner) : OS(OS), Banner(Banner) {} -PreservedAnalyses PrintModulePass::run(Module *M) { - OS << Banner << *M; +PreservedAnalyses PrintModulePass::run(Module &M) { + OS << Banner << M; return PreservedAnalyses::all(); } @@ -33,8 +33,8 @@ PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {} PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner) : OS(OS), Banner(Banner) {} -PreservedAnalyses PrintFunctionPass::run(Function *F) { - OS << Banner << static_cast<Value &>(*F); +PreservedAnalyses PrintFunctionPass::run(Function &F) { + OS << Banner << static_cast<Value &>(F); return PreservedAnalyses::all(); } @@ -50,7 +50,7 @@ public: : ModulePass(ID), P(OS, Banner) {} bool runOnModule(Module &M) override { - P.run(&M); + P.run(M); return false; } @@ -70,7 +70,7 @@ public: // This pass just prints a banner followed by the function as it's processed. bool runOnFunction(Function &F) override { - P.run(&F); + P.run(F); return false; } diff --git a/contrib/llvm/lib/IR/InlineAsm.cpp b/contrib/llvm/lib/IR/InlineAsm.cpp index a3e1da3..16d874f3 100644 --- a/contrib/llvm/lib/IR/InlineAsm.cpp +++ b/contrib/llvm/lib/IR/InlineAsm.cpp @@ -91,6 +91,10 @@ bool InlineAsm::ConstraintInfo::Parse(StringRef Str, if (*I == '~') { Type = isClobber; ++I; + + // '{' must immediately follow '~'. + if (I != E && *I != '{') + return true; } else if (*I == '=') { ++I; Type = isOutput; diff --git a/contrib/llvm/lib/IR/Instruction.cpp b/contrib/llvm/lib/IR/Instruction.cpp index 86421c4..92c6e9f 100644 --- a/contrib/llvm/lib/IR/Instruction.cpp +++ b/contrib/llvm/lib/IR/Instruction.cpp @@ -15,7 +15,6 @@ #include "llvm/IR/CallSite.h" #include "llvm/IR/Constants.h" #include "llvm/IR/Instructions.h" -#include "llvm/IR/LeakDetector.h" #include "llvm/IR/Module.h" #include "llvm/IR/Operator.h" #include "llvm/IR/Type.h" @@ -24,8 +23,6 @@ using namespace llvm; Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, Instruction *InsertBefore) : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { - // Make sure that we get added to a basicblock - LeakDetector::addGarbageObject(this); // If requested, insert this instruction into a basic block... if (InsertBefore) { @@ -42,8 +39,6 @@ const DataLayout *Instruction::getDataLayout() const { Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd) : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { - // Make sure that we get added to a basicblock - LeakDetector::addGarbageObject(this); // append this instruction into the basic block assert(InsertAtEnd && "Basic block to append to may not be NULL!"); @@ -60,12 +55,6 @@ Instruction::~Instruction() { void Instruction::setParent(BasicBlock *P) { - if (getParent()) { - if (!P) LeakDetector::addGarbageObject(this); - } else { - if (P) LeakDetector::removeGarbageObject(this); - } - Parent = P; } @@ -143,6 +132,11 @@ void Instruction::setFastMathFlags(FastMathFlags FMF) { cast<FPMathOperator>(this)->setFastMathFlags(FMF); } +void Instruction::copyFastMathFlags(FastMathFlags FMF) { + assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op"); + cast<FPMathOperator>(this)->copyFastMathFlags(FMF); +} + /// Determine whether the unsafe-algebra flag is set. bool Instruction::hasUnsafeAlgebra() const { assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); @@ -175,7 +169,7 @@ bool Instruction::hasAllowReciprocal() const { /// Convenience function for getting all the fast-math flags, which must be an /// operator which supports these flags. See LangRef.html for the meaning of -/// these flats. +/// these flags. FastMathFlags Instruction::getFastMathFlags() const { assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); return cast<FPMathOperator>(this)->getFastMathFlags(); @@ -183,7 +177,7 @@ FastMathFlags Instruction::getFastMathFlags() const { /// Copy I's fast-math flags void Instruction::copyFastMathFlags(const Instruction *I) { - setFastMathFlags(I->getFastMathFlags()); + copyFastMathFlags(I->getFastMathFlags()); } @@ -438,6 +432,21 @@ bool Instruction::mayWriteToMemory() const { } } +bool Instruction::isAtomic() const { + switch (getOpcode()) { + default: + return false; + case Instruction::AtomicCmpXchg: + case Instruction::AtomicRMW: + case Instruction::Fence: + return true; + case Instruction::Load: + return cast<LoadInst>(this)->getOrdering() != NotAtomic; + case Instruction::Store: + return cast<StoreInst>(this)->getOrdering() != NotAtomic; + } +} + bool Instruction::mayThrow() const { if (const CallInst *CI = dyn_cast<CallInst>(this)) return !CI->doesNotThrow(); @@ -528,7 +537,7 @@ Instruction *Instruction::clone() const { // Otherwise, enumerate and copy over metadata from the old instruction to the // new one. - SmallVector<std::pair<unsigned, MDNode*>, 4> TheMDs; + SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs; getAllMetadataOtherThanDebugLoc(TheMDs); for (const auto &MD : TheMDs) New->setMetadata(MD.first, MD.second); diff --git a/contrib/llvm/lib/IR/Instructions.cpp b/contrib/llvm/lib/IR/Instructions.cpp index 9553252..132800e 100644 --- a/contrib/llvm/lib/IR/Instructions.cpp +++ b/contrib/llvm/lib/IR/Instructions.cpp @@ -364,8 +364,9 @@ bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const { /// IsConstantOne - Return true only if val is constant int 1 static bool IsConstantOne(Value *val) { - assert(val && "IsConstantOne does not work with NULL val"); - return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne(); + assert(val && "IsConstantOne does not work with nullptr val"); + const ConstantInt *CVal = dyn_cast<ConstantInt>(val); + return CVal && CVal->isOne(); } static Instruction *createMalloc(Instruction *InsertBefore, @@ -418,7 +419,7 @@ static Instruction *createMalloc(Instruction *InsertBefore, Value *MallocFunc = MallocF; if (!MallocFunc) // prototype malloc as "void *malloc(size_t)" - MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL); + MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr); PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); CallInst *MCall = nullptr; Instruction *Result = nullptr; @@ -491,7 +492,7 @@ static Instruction* createFree(Value* Source, Instruction *InsertBefore, Type *VoidTy = Type::getVoidTy(M->getContext()); Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); // prototype free as "void free(void*)" - Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL); + Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr); CallInst* Result = nullptr; Value *PtrCast = Source; if (InsertBefore) { @@ -795,11 +796,8 @@ void BranchInst::swapSuccessors() { return; // The first operand is the name. Fetch them backwards and build a new one. - Value *Ops[] = { - ProfileData->getOperand(0), - ProfileData->getOperand(2), - ProfileData->getOperand(1) - }; + Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2), + ProfileData->getOperand(1)}; setMetadata(LLVMContext::MD_prof, MDNode::get(ProfileData->getContext(), Ops)); } @@ -2030,6 +2028,39 @@ bool BinaryOperator::isExact() const { return cast<PossiblyExactOperator>(this)->isExact(); } +void BinaryOperator::copyIRFlags(const Value *V) { + // Copy the wrapping flags. + if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { + setHasNoSignedWrap(OB->hasNoSignedWrap()); + setHasNoUnsignedWrap(OB->hasNoUnsignedWrap()); + } + + // Copy the exact flag. + if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) + setIsExact(PE->isExact()); + + // Copy the fast-math flags. + if (auto *FP = dyn_cast<FPMathOperator>(V)) + copyFastMathFlags(FP->getFastMathFlags()); +} + +void BinaryOperator::andIRFlags(const Value *V) { + if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { + setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap()); + setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap()); + } + + if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) + setIsExact(isExact() & PE->isExact()); + + if (auto *FP = dyn_cast<FPMathOperator>(V)) { + FastMathFlags FM = getFastMathFlags(); + FM &= FP->getFastMathFlags(); + copyFastMathFlags(FM); + } +} + + //===----------------------------------------------------------------------===// // FPMathOperator Class //===----------------------------------------------------------------------===// @@ -2039,10 +2070,10 @@ bool BinaryOperator::isExact() const { /// default precision. float FPMathOperator::getFPAccuracy() const { const MDNode *MD = - cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); + cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); if (!MD) return 0.0; - ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0)); + ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0)); return Accuracy->getValueAPF().convertToFloat(); } @@ -2525,6 +2556,17 @@ CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); } +CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty, + const Twine &Name, + Instruction *InsertBefore) { + if (S->getType()->isPointerTy() && Ty->isIntegerTy()) + return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); + if (S->getType()->isIntegerTy() && Ty->isPointerTy()) + return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore); + + return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); +} + CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, bool isSigned, const Twine &Name, Instruction *InsertBefore) { @@ -2682,6 +2724,18 @@ bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { return true; } +bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, + const DataLayout *DL) { + if (auto *PtrTy = dyn_cast<PointerType>(SrcTy)) + if (auto *IntTy = dyn_cast<IntegerType>(DestTy)) + return DL && IntTy->getBitWidth() == DL->getPointerTypeSizeInBits(PtrTy); + if (auto *PtrTy = dyn_cast<PointerType>(DestTy)) + if (auto *IntTy = dyn_cast<IntegerType>(SrcTy)) + return DL && IntTy->getBitWidth() == DL->getPointerTypeSizeInBits(PtrTy); + + return isBitCastable(SrcTy, DestTy); +} + // Provide a way to get a "cast" where the cast opcode is inferred from the // types and size of the operand. This, basically, is a parallel of the // logic in the castIsValid function below. This axiom should hold: diff --git a/contrib/llvm/lib/IR/IntrinsicInst.cpp b/contrib/llvm/lib/IR/IntrinsicInst.cpp index 5725284..b9b5a29 100644 --- a/contrib/llvm/lib/IR/IntrinsicInst.cpp +++ b/contrib/llvm/lib/IR/IntrinsicInst.cpp @@ -49,15 +49,25 @@ Value *DbgInfoIntrinsic::StripCast(Value *C) { return dyn_cast<GlobalVariable>(C); } +static Value *getValueImpl(Value *Op) { + auto *MD = cast<MetadataAsValue>(Op)->getMetadata(); + if (auto *V = dyn_cast<ValueAsMetadata>(MD)) + return V->getValue(); + + // When the value goes to null, it gets replaced by an empty MDNode. + assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode"); + return nullptr; +} + //===----------------------------------------------------------------------===// /// DbgDeclareInst - This represents the llvm.dbg.declare instruction. /// Value *DbgDeclareInst::getAddress() const { - if (MDNode* MD = cast_or_null<MDNode>(getArgOperand(0))) - return MD->getOperand(0); - else + if (!getArgOperand(0)) return nullptr; + + return getValueImpl(getArgOperand(0)); } //===----------------------------------------------------------------------===// @@ -65,9 +75,7 @@ Value *DbgDeclareInst::getAddress() const { /// const Value *DbgValueInst::getValue() const { - return cast<MDNode>(getArgOperand(0))->getOperand(0); + return const_cast<DbgValueInst *>(this)->getValue(); } -Value *DbgValueInst::getValue() { - return cast<MDNode>(getArgOperand(0))->getOperand(0); -} +Value *DbgValueInst::getValue() { return getValueImpl(getArgOperand(0)); } diff --git a/contrib/llvm/lib/IR/LLVMContext.cpp b/contrib/llvm/lib/IR/LLVMContext.cpp index de825f0..b6d95c4 100644 --- a/contrib/llvm/lib/IR/LLVMContext.cpp +++ b/contrib/llvm/lib/IR/LLVMContext.cpp @@ -66,6 +66,33 @@ LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { unsigned InvariantLdId = getMDKindID("invariant.load"); assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted"); (void)InvariantLdId; + + // Create the 'alias.scope' metadata kind. + unsigned AliasScopeID = getMDKindID("alias.scope"); + assert(AliasScopeID == MD_alias_scope && "alias.scope kind id drifted"); + (void)AliasScopeID; + + // Create the 'noalias' metadata kind. + unsigned NoAliasID = getMDKindID("noalias"); + assert(NoAliasID == MD_noalias && "noalias kind id drifted"); + (void)NoAliasID; + + // Create the 'nontemporal' metadata kind. + unsigned NonTemporalID = getMDKindID("nontemporal"); + assert(NonTemporalID == MD_nontemporal && "nontemporal kind id drifted"); + (void)NonTemporalID; + + // Create the 'llvm.mem.parallel_loop_access' metadata kind. + unsigned MemParallelLoopAccessID = getMDKindID("llvm.mem.parallel_loop_access"); + assert(MemParallelLoopAccessID == MD_mem_parallel_loop_access && + "mem_parallel_loop_access kind id drifted"); + (void)MemParallelLoopAccessID; + + + // Create the 'nonnull' metadata kind. + unsigned NonNullID = getMDKindID("nonnull"); + assert(NonNullID == MD_nonnull && "nonnull kind id drifted"); + (void)NonNullID; } LLVMContext::~LLVMContext() { delete pImpl; } @@ -102,9 +129,11 @@ void *LLVMContext::getInlineAsmDiagnosticContext() const { } void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler, - void *DiagnosticContext) { + void *DiagnosticContext, + bool RespectFilters) { pImpl->DiagnosticHandler = DiagnosticHandler; pImpl->DiagnosticContext = DiagnosticContext; + pImpl->RespectDiagnosticFilters = RespectFilters; } LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const { @@ -135,13 +164,7 @@ void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr)); } -void LLVMContext::diagnose(const DiagnosticInfo &DI) { - // If there is a report handler, use it. - if (pImpl->DiagnosticHandler) { - pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext); - return; - } - +static bool isDiagnosticEnabled(const DiagnosticInfo &DI) { // Optimization remarks are selective. They need to check whether the regexp // pattern, passed via one of the -pass-remarks* flags, matches the name of // the pass that is emitting the diagnostic. If there is no match, ignore the @@ -149,19 +172,32 @@ void LLVMContext::diagnose(const DiagnosticInfo &DI) { switch (DI.getKind()) { case llvm::DK_OptimizationRemark: if (!cast<DiagnosticInfoOptimizationRemark>(DI).isEnabled()) - return; + return false; break; case llvm::DK_OptimizationRemarkMissed: if (!cast<DiagnosticInfoOptimizationRemarkMissed>(DI).isEnabled()) - return; + return false; break; case llvm::DK_OptimizationRemarkAnalysis: if (!cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI).isEnabled()) - return; + return false; break; default: break; } + return true; +} + +void LLVMContext::diagnose(const DiagnosticInfo &DI) { + // If there is a report handler, use it. + if (pImpl->DiagnosticHandler) { + if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) + pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext); + return; + } + + if (!isDiagnosticEnabled(DI)) + return; // Otherwise, print the message with a prefix based on the severity. std::string MsgStorage; @@ -193,33 +229,16 @@ void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { // Metadata Kind Uniquing //===----------------------------------------------------------------------===// -#ifndef NDEBUG -/// isValidName - Return true if Name is a valid custom metadata handler name. -static bool isValidName(StringRef MDName) { - if (MDName.empty()) - return false; - - if (!std::isalpha(static_cast<unsigned char>(MDName[0]))) - return false; - - for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E; - ++I) { - if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' && - *I != '-' && *I != '.') - return false; - } - return true; -} -#endif - /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. unsigned LLVMContext::getMDKindID(StringRef Name) const { - assert(isValidName(Name) && "Invalid MDNode name"); + assert(!std::isdigit(Name.front()) && + "Named metadata may not start with a digit"); // If this is new, assign it its ID. - return - pImpl->CustomMDKindNames.GetOrCreateValue( - Name, pImpl->CustomMDKindNames.size()).second; + return pImpl->CustomMDKindNames.insert(std::make_pair( + Name, + pImpl->CustomMDKindNames.size())) + .first->second; } /// getHandlerNames - Populate client supplied smallvector using custome diff --git a/contrib/llvm/lib/IR/LLVMContextImpl.cpp b/contrib/llvm/lib/IR/LLVMContextImpl.cpp index 4c2791f..01a0e6c 100644 --- a/contrib/llvm/lib/IR/LLVMContextImpl.cpp +++ b/contrib/llvm/lib/IR/LLVMContextImpl.cpp @@ -40,6 +40,7 @@ LLVMContextImpl::LLVMContextImpl(LLVMContext &C) InlineAsmDiagContext = nullptr; DiagnosticHandler = nullptr; DiagnosticContext = nullptr; + RespectDiagnosticFilters = false; YieldCallback = nullptr; YieldOpaqueHandle = nullptr; NamedStructTypesUniqueID = 0; @@ -71,11 +72,34 @@ LLVMContextImpl::~LLVMContextImpl() { // the container. Avoid iterators during this operation: while (!OwnedModules.empty()) delete *OwnedModules.begin(); - + + // Drop references for MDNodes. Do this before Values get deleted to avoid + // unnecessary RAUW when nodes are still unresolved. + for (auto *I : DistinctMDNodes) + I->dropAllReferences(); + for (auto *I : MDTuples) + I->dropAllReferences(); + for (auto *I : MDLocations) + I->dropAllReferences(); + + // Also drop references that come from the Value bridges. + for (auto &Pair : ValuesAsMetadata) + Pair.second->dropUsers(); + for (auto &Pair : MetadataAsValues) + Pair.second->dropUse(); + + // Destroy MDNodes. + for (UniquableMDNode *I : DistinctMDNodes) + I->deleteAsSubclass(); + for (MDTuple *I : MDTuples) + delete I; + for (MDLocation *I : MDLocations) + delete I; + // Free the constants. This is important to do here to ensure that they are // freed before the LeakDetector is torn down. std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), - DropReferences()); + DropFirst()); std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), DropFirst()); std::for_each(StructConstants.map_begin(), StructConstants.map_end(), @@ -119,22 +143,23 @@ LLVMContextImpl::~LLVMContextImpl() { delete &*Elem; } - // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet - // and the NonUniquedMDNodes sets, so copy the values out first. - SmallVector<MDNode*, 8> MDNodes; - MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size()); - for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end(); - I != E; ++I) - MDNodes.push_back(&*I); - MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end()); - for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(), - E = MDNodes.end(); I != E; ++I) - (*I)->destroy(); - assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() && - "Destroying all MDNodes didn't empty the Context's sets."); + // Destroy MetadataAsValues. + { + SmallVector<MetadataAsValue *, 8> MDVs; + MDVs.reserve(MetadataAsValues.size()); + for (auto &Pair : MetadataAsValues) + MDVs.push_back(Pair.second); + MetadataAsValues.clear(); + for (auto *V : MDVs) + delete V; + } + + // Destroy ValuesAsMetadata. + for (auto &Pair : ValuesAsMetadata) + delete Pair.second; // Destroy MDStrings. - DeleteContainerSeconds(MDStringCache); + MDStringCache.clear(); } // ConstantsContext anchors diff --git a/contrib/llvm/lib/IR/LLVMContextImpl.h b/contrib/llvm/lib/IR/LLVMContextImpl.h index 808c239..6ebc567 100644 --- a/contrib/llvm/lib/IR/LLVMContextImpl.h +++ b/contrib/llvm/lib/IR/LLVMContextImpl.h @@ -12,8 +12,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_LLVMCONTEXT_IMPL_H -#define LLVM_LLVMCONTEXT_IMPL_H +#ifndef LLVM_LIB_IR_LLVMCONTEXTIMPL_H +#define LLVM_LIB_IR_LLVMCONTEXTIMPL_H #include "AttributeImpl.h" #include "ConstantsContext.h" @@ -22,6 +22,7 @@ #include "llvm/ADT/APInt.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/DenseSet.h" #include "llvm/ADT/FoldingSet.h" #include "llvm/ADT/Hashing.h" #include "llvm/ADT/SmallPtrSet.h" @@ -45,55 +46,32 @@ class Type; class Value; struct DenseMapAPIntKeyInfo { - struct KeyTy { - APInt val; - Type* type; - KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {} - bool operator==(const KeyTy& that) const { - return type == that.type && this->val == that.val; - } - bool operator!=(const KeyTy& that) const { - return !this->operator==(that); - } - friend hash_code hash_value(const KeyTy &Key) { - return hash_combine(Key.type, Key.val); - } - }; - static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), nullptr); } - static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), nullptr); } - static unsigned getHashValue(const KeyTy &Key) { + static inline APInt getEmptyKey() { + APInt V(nullptr, 0); + V.VAL = 0; + return V; + } + static inline APInt getTombstoneKey() { + APInt V(nullptr, 0); + V.VAL = 1; + return V; + } + static unsigned getHashValue(const APInt &Key) { return static_cast<unsigned>(hash_value(Key)); } - static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { - return LHS == RHS; + static bool isEqual(const APInt &LHS, const APInt &RHS) { + return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS; } }; struct DenseMapAPFloatKeyInfo { - struct KeyTy { - APFloat val; - KeyTy(const APFloat& V) : val(V){} - bool operator==(const KeyTy& that) const { - return this->val.bitwiseIsEqual(that.val); - } - bool operator!=(const KeyTy& that) const { - return !this->operator==(that); - } - friend hash_code hash_value(const KeyTy &Key) { - return hash_combine(Key.val); - } - }; - static inline KeyTy getEmptyKey() { - return KeyTy(APFloat(APFloat::Bogus,1)); - } - static inline KeyTy getTombstoneKey() { - return KeyTy(APFloat(APFloat::Bogus,2)); - } - static unsigned getHashValue(const KeyTy &Key) { + static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); } + static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); } + static unsigned getHashValue(const APFloat &Key) { return static_cast<unsigned>(hash_value(Key)); } - static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { - return LHS == RHS; + static bool isEqual(const APFloat &LHS, const APFloat &RHS) { + return LHS.bitwiseIsEqual(RHS); } }; @@ -103,9 +81,8 @@ struct AnonStructTypeKeyInfo { bool isPacked; KeyTy(const ArrayRef<Type*>& E, bool P) : ETypes(E), isPacked(P) {} - KeyTy(const StructType* ST) : - ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())), - isPacked(ST->isPacked()) {} + KeyTy(const StructType *ST) + : ETypes(ST->elements()), isPacked(ST->isPacked()) {} bool operator==(const KeyTy& that) const { if (isPacked != that.isPacked) return false; @@ -148,10 +125,9 @@ struct FunctionTypeKeyInfo { bool isVarArg; KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) : ReturnType(R), Params(P), isVarArg(V) {} - KeyTy(const FunctionType* FT) : - ReturnType(FT->getReturnType()), - Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())), - isVarArg(FT->isVarArg()) {} + KeyTy(const FunctionType *FT) + : ReturnType(FT->getReturnType()), Params(FT->params()), + isVarArg(FT->isVarArg()) {} bool operator==(const KeyTy& that) const { if (ReturnType != that.ReturnType) return false; @@ -190,49 +166,97 @@ struct FunctionTypeKeyInfo { } }; -// Provide a FoldingSetTrait::Equals specialization for MDNode that can use a -// shortcut to avoid comparing all operands. -template<> struct FoldingSetTrait<MDNode> : DefaultFoldingSetTrait<MDNode> { - static bool Equals(const MDNode &X, const FoldingSetNodeID &ID, - unsigned IDHash, FoldingSetNodeID &TempID) { - assert(!X.isNotUniqued() && "Non-uniqued MDNode in FoldingSet?"); - // First, check if the cached hashes match. If they don't we can skip the - // expensive operand walk. - if (X.Hash != IDHash) - return false; +/// \brief DenseMapInfo for MDTuple. +/// +/// Note that we don't need the is-function-local bit, since that's implicit in +/// the operands. +struct MDTupleInfo { + struct KeyTy { + ArrayRef<Metadata *> RawOps; + ArrayRef<MDOperand> Ops; + unsigned Hash; + + KeyTy(ArrayRef<Metadata *> Ops) + : RawOps(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {} + + KeyTy(MDTuple *N) + : Ops(N->op_begin(), N->op_end()), Hash(N->getHash()) {} - // If they match we have to compare the operands. - X.Profile(TempID); - return TempID == ID; + bool operator==(const MDTuple *RHS) const { + if (RHS == getEmptyKey() || RHS == getTombstoneKey()) + return false; + if (Hash != RHS->getHash()) + return false; + assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?"); + return RawOps.empty() ? compareOps(Ops, RHS) : compareOps(RawOps, RHS); + } + template <class T> + static bool compareOps(ArrayRef<T> Ops, const MDTuple *RHS) { + if (Ops.size() != RHS->getNumOperands()) + return false; + return std::equal(Ops.begin(), Ops.end(), RHS->op_begin()); + } + }; + static inline MDTuple *getEmptyKey() { + return DenseMapInfo<MDTuple *>::getEmptyKey(); + } + static inline MDTuple *getTombstoneKey() { + return DenseMapInfo<MDTuple *>::getTombstoneKey(); } - static unsigned ComputeHash(const MDNode &X, FoldingSetNodeID &) { - return X.Hash; // Return cached hash. + static unsigned getHashValue(const KeyTy &Key) { return Key.Hash; } + static unsigned getHashValue(const MDTuple *U) { + return U->getHash(); + } + static bool isEqual(const KeyTy &LHS, const MDTuple *RHS) { + return LHS == RHS; + } + static bool isEqual(const MDTuple *LHS, const MDTuple *RHS) { + return LHS == RHS; } }; -/// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps -/// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp. -class DebugRecVH : public CallbackVH { - /// Ctx - This is the LLVM Context being referenced. - LLVMContextImpl *Ctx; - - /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that - /// this reference lives in. If this is zero, then it represents a - /// non-canonical entry that has no DenseMap value. This can happen due to - /// RAUW. - int Idx; -public: - DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx) - : CallbackVH(n), Ctx(ctx), Idx(idx) {} - - MDNode *get() const { - return cast_or_null<MDNode>(getValPtr()); - } +/// \brief DenseMapInfo for MDLocation. +struct MDLocationInfo { + struct KeyTy { + unsigned Line; + unsigned Column; + Metadata *Scope; + Metadata *InlinedAt; - void deleted() override; - void allUsesReplacedWith(Value *VNew) override; + KeyTy(unsigned Line, unsigned Column, Metadata *Scope, Metadata *InlinedAt) + : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {} + + KeyTy(const MDLocation *L) + : Line(L->getLine()), Column(L->getColumn()), Scope(L->getScope()), + InlinedAt(L->getInlinedAt()) {} + + bool operator==(const MDLocation *RHS) const { + if (RHS == getEmptyKey() || RHS == getTombstoneKey()) + return false; + return Line == RHS->getLine() && Column == RHS->getColumn() && + Scope == RHS->getScope() && InlinedAt == RHS->getInlinedAt(); + } + }; + static inline MDLocation *getEmptyKey() { + return DenseMapInfo<MDLocation *>::getEmptyKey(); + } + static inline MDLocation *getTombstoneKey() { + return DenseMapInfo<MDLocation *>::getTombstoneKey(); + } + static unsigned getHashValue(const KeyTy &Key) { + return hash_combine(Key.Line, Key.Column, Key.Scope, Key.InlinedAt); + } + static unsigned getHashValue(const MDLocation *U) { + return getHashValue(KeyTy(U)); + } + static bool isEqual(const KeyTy &LHS, const MDLocation *RHS) { + return LHS == RHS; + } + static bool isEqual(const MDLocation *LHS, const MDLocation *RHS) { + return LHS == RHS; + } }; - + class LLVMContextImpl { public: /// OwnedModules - The set of modules instantiated in this context, and which @@ -244,41 +268,43 @@ public: LLVMContext::DiagnosticHandlerTy DiagnosticHandler; void *DiagnosticContext; + bool RespectDiagnosticFilters; LLVMContext::YieldCallbackTy YieldCallback; void *YieldOpaqueHandle; - typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt *, - DenseMapAPIntKeyInfo> IntMapTy; + typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy; IntMapTy IntConstants; - - typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, - DenseMapAPFloatKeyInfo> FPMapTy; + + typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy; FPMapTy FPConstants; FoldingSet<AttributeImpl> AttrsSet; FoldingSet<AttributeSetImpl> AttrsLists; FoldingSet<AttributeSetNode> AttrsSetNodes; - StringMap<Value*> MDStringCache; + StringMap<MDString> MDStringCache; + DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata; + DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues; - FoldingSet<MDNode> MDNodeSet; + DenseSet<MDTuple *, MDTupleInfo> MDTuples; + DenseSet<MDLocation *, MDLocationInfo> MDLocations; // MDNodes may be uniqued or not uniqued. When they're not uniqued, they // aren't in the MDNodeSet, but they're still shared between objects, so no // one object can destroy them. This set allows us to at least destroy them // on Context destruction. - SmallPtrSet<MDNode*, 1> NonUniquedMDNodes; - + SmallPtrSet<UniquableMDNode *, 1> DistinctMDNodes; + DenseMap<Type*, ConstantAggregateZero*> CAZConstants; - typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy; + typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy; ArrayConstantsTy ArrayConstants; - typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy; + typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy; StructConstantsTy StructConstants; - typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy; + typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy; VectorConstantsTy VectorConstants; DenseMap<PointerType*, ConstantPointerNull*> CPNConstants; @@ -289,17 +315,16 @@ public: DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *> BlockAddresses; - ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr> - ExprConstants; + ConstantUniqueMap<ConstantExpr> ExprConstants; + + ConstantUniqueMap<InlineAsm> InlineAsms; - ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType, - InlineAsm> InlineAsms; - ConstantInt *TheTrueVal; ConstantInt *TheFalseVal; LeakDetectorImpl<Value> LLVMObjects; - + LeakDetectorImpl<Metadata> LLVMMDObjects; + // Basic type instances. Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy; Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy; @@ -311,11 +336,11 @@ public: BumpPtrAllocator TypeAllocator; DenseMap<unsigned, IntegerType*> IntegerTypes; - - typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap; - FunctionTypeMap FunctionTypes; - typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap; - StructTypeMap AnonStructTypes; + + typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet; + FunctionTypeSet FunctionTypes; + typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet; + StructTypeSet AnonStructTypes; StringMap<StructType*> NamedStructTypes; unsigned NamedStructTypesUniqueID; @@ -333,32 +358,14 @@ public: /// CustomMDKindNames - Map to hold the metadata string to ID mapping. StringMap<unsigned> CustomMDKindNames; - - typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy; + + typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy; typedef SmallVector<MDPairTy, 2> MDMapTy; /// MetadataStore - Collection of per-instruction metadata used in this /// context. DenseMap<const Instruction *, MDMapTy> MetadataStore; - /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope - /// entry with no "inlined at" element. - DenseMap<MDNode*, int> ScopeRecordIdx; - - /// ScopeRecords - These are the actual mdnodes (in a value handle) for an - /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if - /// the MDNode is RAUW'd. - std::vector<DebugRecVH> ScopeRecords; - - /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an - /// scope/inlined-at pair. - DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx; - - /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles) - /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up - /// to date. - std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords; - /// DiscriminatorTable - This table maps file:line locations to an /// integer representing the next DWARF path discriminator to assign to /// instructions in different blocks at the same location. @@ -374,6 +381,12 @@ public: typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy; PrefixDataMapTy PrefixDataMap; + /// \brief Mapping from a function to its prologue data, which is stored as + /// the operand of an unparented ReturnInst so that the prologue data has a + /// Use. + typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy; + PrologueDataMapTy PrologueDataMap; + int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx); int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx); diff --git a/contrib/llvm/lib/IR/LeakDetector.cpp b/contrib/llvm/lib/IR/LeakDetector.cpp deleted file mode 100644 index 6f71627..0000000 --- a/contrib/llvm/lib/IR/LeakDetector.cpp +++ /dev/null @@ -1,69 +0,0 @@ -//===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements the LeakDetector class. -// -//===----------------------------------------------------------------------===// - -#include "llvm/IR/LeakDetector.h" -#include "LLVMContextImpl.h" -#include "llvm/ADT/SmallPtrSet.h" -#include "llvm/IR/Value.h" -#include "llvm/Support/Compiler.h" -#include "llvm/Support/ManagedStatic.h" -#include "llvm/Support/Mutex.h" -#include "llvm/Support/Threading.h" -using namespace llvm; - -static ManagedStatic<sys::SmartMutex<true> > ObjectsLock; -static ManagedStatic<LeakDetectorImpl<void> > Objects; - -static void clearGarbage(LLVMContext &Context) { - Objects->clear(); - Context.pImpl->LLVMObjects.clear(); -} - -void LeakDetector::addGarbageObjectImpl(void *Object) { - sys::SmartScopedLock<true> Lock(*ObjectsLock); - Objects->addGarbage(Object); -} - -void LeakDetector::addGarbageObjectImpl(const Value *Object) { - LLVMContextImpl *pImpl = Object->getContext().pImpl; - pImpl->LLVMObjects.addGarbage(Object); -} - -void LeakDetector::removeGarbageObjectImpl(void *Object) { - sys::SmartScopedLock<true> Lock(*ObjectsLock); - Objects->removeGarbage(Object); -} - -void LeakDetector::removeGarbageObjectImpl(const Value *Object) { - LLVMContextImpl *pImpl = Object->getContext().pImpl; - pImpl->LLVMObjects.removeGarbage(Object); -} - -void LeakDetector::checkForGarbageImpl(LLVMContext &Context, - const std::string &Message) { - LLVMContextImpl *pImpl = Context.pImpl; - sys::SmartScopedLock<true> Lock(*ObjectsLock); - - Objects->setName("GENERIC"); - pImpl->LLVMObjects.setName("LLVM"); - - // use non-short-circuit version so that both checks are performed - if (Objects->hasGarbage(Message) | - pImpl->LLVMObjects.hasGarbage(Message)) - errs() << "\nThis is probably because you removed an object, but didn't " - << "delete it. Please check your code for memory leaks.\n"; - - // Clear out results so we don't get duplicate warnings on - // next call... - clearGarbage(Context); -} diff --git a/contrib/llvm/lib/IR/LeaksContext.h b/contrib/llvm/lib/IR/LeaksContext.h index 52ac170..47704fa 100644 --- a/contrib/llvm/lib/IR/LeaksContext.h +++ b/contrib/llvm/lib/IR/LeaksContext.h @@ -12,10 +12,11 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_IR_LEAKSCONTEXT_H -#define LLVM_IR_LEAKSCONTEXT_H +#ifndef LLVM_LIB_IR_LEAKSCONTEXT_H +#define LLVM_LIB_IR_LEAKSCONTEXT_H #include "llvm/ADT/SmallPtrSet.h" +#include "llvm/IR/Metadata.h" #include "llvm/IR/Value.h" #include "llvm/Support/raw_ostream.h" @@ -31,6 +32,10 @@ struct PrinterTrait<Value> { static void print(const Value* P) { errs() << *P; } }; +template <> struct PrinterTrait<Metadata> { + static void print(const Metadata *P) { P->print(errs()); } +}; + template <typename T> struct LeakDetectorImpl { explicit LeakDetectorImpl(const char* const name = "") : @@ -95,4 +100,4 @@ private: } -#endif // LLVM_IR_LEAKSCONTEXT_H +#endif diff --git a/contrib/llvm/lib/IR/LegacyPassManager.cpp b/contrib/llvm/lib/IR/LegacyPassManager.cpp index d3f3482..fa8d50e 100644 --- a/contrib/llvm/lib/IR/LegacyPassManager.cpp +++ b/contrib/llvm/lib/IR/LegacyPassManager.cpp @@ -227,10 +227,7 @@ public: Pass(PT_PassManager, ID), PMDataManager(), PMTopLevelManager(new FPPassManager()), wasRun(false) {} - /// add - Add a pass to the queue of passes to run. This passes ownership of - /// the Pass to the PassManager. When the PassManager is destroyed, the pass - /// will be destroyed as well, so there is no need to delete the pass. This - /// implies that all passes MUST be allocated with 'new'. + /// \copydoc FunctionPassManager::add() void add(Pass *P) { schedulePass(P); } @@ -398,10 +395,7 @@ public: Pass(PT_PassManager, ID), PMDataManager(), PMTopLevelManager(new MPPassManager()) {} - /// add - Add a pass to the queue of passes to run. This passes ownership of - /// the Pass to the PassManager. When the PassManager is destroyed, the pass - /// will be destroyed as well, so there is no need to delete the pass. This - /// implies that all passes MUST be allocated with 'new'. + /// \copydoc PassManager::add() void add(Pass *P) { schedulePass(P); } @@ -573,9 +567,8 @@ void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses, return; SmallPtrSet<Pass *, 8> &LU = DMI->second; - for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(), - E = LU.end(); I != E; ++I) { - LastUses.push_back(*I); + for (Pass *LUP : LU) { + LastUses.push_back(LUP); } } @@ -607,8 +600,7 @@ void PMTopLevelManager::schedulePass(Pass *P) { // If P is an analysis pass and it is available then do not // generate the analysis again. Stale analysis info should not be // available at this point. - const PassInfo *PI = - PassRegistry::getPassRegistry()->getPassInfo(P->getPassID()); + const PassInfo *PI = findAnalysisPassInfo(P->getPassID()); if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) { delete P; return; @@ -626,7 +618,7 @@ void PMTopLevelManager::schedulePass(Pass *P) { Pass *AnalysisPass = findAnalysisPass(*I); if (!AnalysisPass) { - const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I); + const PassInfo *PI = findAnalysisPassInfo(*I); if (!PI) { // Pass P is not in the global PassRegistry @@ -723,8 +715,7 @@ Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { return *I; // If Pass not found then check the interfaces implemented by Immutable Pass - const PassInfo *PassInf = - PassRegistry::getPassRegistry()->getPassInfo(PI); + const PassInfo *PassInf = findAnalysisPassInfo(PI); assert(PassInf && "Expected all immutable passes to be initialized"); const std::vector<const PassInfo*> &ImmPI = PassInf->getInterfacesImplemented(); @@ -738,6 +729,17 @@ Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { return nullptr; } +const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const { + const PassInfo *&PI = AnalysisPassInfos[AID]; + if (!PI) + PI = PassRegistry::getPassRegistry()->getPassInfo(AID); + else + assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) && + "The pass info pointer changed for an analysis ID!"); + + return PI; +} + // Print passes managed by this top level manager. void PMTopLevelManager::dumpPasses() const { @@ -766,8 +768,7 @@ void PMTopLevelManager::dumpArguments() const { dbgs() << "Pass Arguments: "; for (SmallVectorImpl<ImmutablePass *>::const_iterator I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) - if (const PassInfo *PI = - PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) { + if (const PassInfo *PI = findAnalysisPassInfo((*I)->getPassID())) { assert(PI && "Expected all immutable passes to be initialized"); if (!PI->isAnalysisGroup()) dbgs() << " -" << PI->getPassArgument(); @@ -831,7 +832,7 @@ void PMDataManager::recordAvailableAnalysis(Pass *P) { // This pass is the current implementation of all of the interfaces it // implements as well. - const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI); + const PassInfo *PInf = TPM->findAnalysisPassInfo(PI); if (!PInf) return; const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented(); for (unsigned i = 0, e = II.size(); i != e; ++i) @@ -964,7 +965,7 @@ void PMDataManager::freePass(Pass *P, StringRef Msg, } AnalysisID PI = P->getPassID(); - if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) { + if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) { // Remove the pass itself (if it is not already removed). AvailableAnalysis.erase(PI); @@ -1044,7 +1045,7 @@ void PMDataManager::add(Pass *P, bool ProcessAnalysis) { for (SmallVectorImpl<AnalysisID>::iterator I = ReqAnalysisNotAvailable.begin(), E = ReqAnalysisNotAvailable.end() ;I != E; ++I) { - const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I); + const PassInfo *PI = TPM->findAnalysisPassInfo(*I); Pass *AnalysisPass = PI->createPass(); this->addLowerLevelRequiredPass(P, AnalysisPass); } @@ -1149,7 +1150,7 @@ void PMDataManager::dumpPassArguments() const { PMD->dumpPassArguments(); else if (const PassInfo *PI = - PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) + TPM->findAnalysisPassInfo((*I)->getPassID())) if (!PI->isAnalysisGroup()) dbgs() << " -" << PI->getPassArgument(); } @@ -1225,7 +1226,7 @@ void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P, dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; for (unsigned i = 0; i != Set.size(); ++i) { if (i) dbgs() << ','; - const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]); + const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]); if (!PInf) { // Some preserved passes, such as AliasAnalysis, may not be initialized by // all drivers. @@ -1390,11 +1391,6 @@ FunctionPassManager::~FunctionPassManager() { delete FPM; } -/// add - Add a pass to the queue of passes to run. This passes -/// ownership of the Pass to the PassManager. When the -/// PassManager_X is destroyed, the pass will be destroyed as well, so -/// there is no need to delete the pass. (TODO delete passes.) -/// This implies that all passes MUST be allocated with 'new'. void FunctionPassManager::add(Pass *P) { FPM->add(P); } @@ -1404,11 +1400,8 @@ void FunctionPassManager::add(Pass *P) { /// so, return true. /// bool FunctionPassManager::run(Function &F) { - if (F.isMaterializable()) { - std::string errstr; - if (F.Materialize(&errstr)) - report_fatal_error("Error reading bitcode file: " + Twine(errstr)); - } + if (std::error_code EC = F.materialize()) + report_fatal_error("Error reading bitcode file: " + EC.message()); return FPM->run(F); } @@ -1673,8 +1666,8 @@ void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { OnTheFlyManagers[P] = FPP; } - const PassInfo * RequiredPassPI = - PassRegistry::getPassRegistry()->getPassInfo(RequiredPass->getPassID()); + const PassInfo *RequiredPassPI = + TPM->findAnalysisPassInfo(RequiredPass->getPassID()); Pass *FoundPass = nullptr; if (RequiredPassPI && RequiredPassPI->isAnalysis()) { @@ -1684,7 +1677,7 @@ void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { if (!FoundPass) { FoundPass = RequiredPass; // This should be guaranteed to add RequiredPass to the passmanager given - // that we checked for an avaiable analysis above. + // that we checked for an available analysis above. FPP->add(RequiredPass); } // Register P as the last user of FoundPass or RequiredPass. @@ -1753,10 +1746,6 @@ PassManager::~PassManager() { delete PM; } -/// add - Add a pass to the queue of passes to run. This passes ownership of -/// the Pass to the PassManager. When the PassManager is destroyed, the pass -/// will be destroyed as well, so there is no need to delete the pass. This -/// implies that all passes MUST be allocated with 'new'. void PassManager::add(Pass *P) { PM->add(P); } diff --git a/contrib/llvm/lib/IR/MDBuilder.cpp b/contrib/llvm/lib/IR/MDBuilder.cpp index 65cdf38..c7fcf7a 100644 --- a/contrib/llvm/lib/IR/MDBuilder.cpp +++ b/contrib/llvm/lib/IR/MDBuilder.cpp @@ -21,11 +21,16 @@ MDString *MDBuilder::createString(StringRef Str) { return MDString::get(Context, Str); } +ConstantAsMetadata *MDBuilder::createConstant(Constant *C) { + return ConstantAsMetadata::get(C); +} + MDNode *MDBuilder::createFPMath(float Accuracy) { if (Accuracy == 0.0) return nullptr; assert(Accuracy > 0.0 && "Invalid fpmath accuracy!"); - Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy); + auto *Op = + createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy)); return MDNode::get(Context, Op); } @@ -38,12 +43,12 @@ MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight, MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) { assert(Weights.size() >= 2 && "Need at least two branch weights!"); - SmallVector<Value *, 4> Vals(Weights.size() + 1); + SmallVector<Metadata *, 4> Vals(Weights.size() + 1); Vals[0] = createString("branch_weights"); Type *Int32Ty = Type::getInt32Ty(Context); for (unsigned i = 0, e = Weights.size(); i != e; ++i) - Vals[i + 1] = ConstantInt::get(Int32Ty, Weights[i]); + Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i])); return MDNode::get(Context, Vals); } @@ -56,14 +61,22 @@ MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) { // Return the range [Lo, Hi). Type *Ty = IntegerType::get(Context, Lo.getBitWidth()); - Value *Range[2] = {ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi)}; + Metadata *Range[2] = {createConstant(ConstantInt::get(Ty, Lo)), + createConstant(ConstantInt::get(Ty, Hi))}; return MDNode::get(Context, Range); } -MDNode *MDBuilder::createAnonymousTBAARoot() { +MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) { // To ensure uniqueness the root node is self-referential. - MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef<Value *>()); - MDNode *Root = MDNode::get(Context, Dummy); + MDNode *Dummy = MDNode::getTemporary(Context, None); + + SmallVector<Metadata *, 3> Args(1, Dummy); + if (Extra) + Args.push_back(Extra); + if (!Name.empty()) + Args.push_back(createString(Name)); + MDNode *Root = MDNode::get(Context, Args); + // At this point we have // !0 = metadata !{} <- dummy // !1 = metadata !{metadata !0} <- root @@ -85,22 +98,31 @@ MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent, bool isConstant) { if (isConstant) { Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1); - Value *Ops[3] = {createString(Name), Parent, Flags}; + Metadata *Ops[3] = {createString(Name), Parent, createConstant(Flags)}; return MDNode::get(Context, Ops); } else { - Value *Ops[2] = {createString(Name), Parent}; + Metadata *Ops[2] = {createString(Name), Parent}; return MDNode::get(Context, Ops); } } +MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) { + return MDNode::get(Context, createString(Name)); +} + +MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) { + Metadata *Ops[2] = {createString(Name), Domain}; + return MDNode::get(Context, Ops); +} + /// \brief Return metadata for a tbaa.struct node with the given /// struct field descriptions. MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) { - SmallVector<Value *, 4> Vals(Fields.size() * 3); + SmallVector<Metadata *, 4> Vals(Fields.size() * 3); Type *Int64 = Type::getInt64Ty(Context); for (unsigned i = 0, e = Fields.size(); i != e; ++i) { - Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset); - Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size); + Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset)); + Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size)); Vals[i * 3 + 2] = Fields[i].TBAA; } return MDNode::get(Context, Vals); @@ -110,12 +132,12 @@ MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) { /// with the given name, a list of pairs (offset, field type in the type DAG). MDNode *MDBuilder::createTBAAStructTypeNode( StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) { - SmallVector<Value *, 4> Ops(Fields.size() * 2 + 1); + SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1); Type *Int64 = Type::getInt64Ty(Context); Ops[0] = createString(Name); for (unsigned i = 0, e = Fields.size(); i != e; ++i) { Ops[i * 2 + 1] = Fields[i].first; - Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second); + Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second)); } return MDNode::get(Context, Ops); } @@ -125,7 +147,7 @@ MDNode *MDBuilder::createTBAAStructTypeNode( MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, uint64_t Offset) { ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset); - Value *Ops[3] = {createString(Name), Parent, Off}; + Metadata *Ops[3] = {createString(Name), Parent, createConstant(Off)}; return MDNode::get(Context, Ops); } @@ -134,6 +156,7 @@ MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, uint64_t Offset) { Type *Int64 = Type::getInt64Ty(Context); - Value *Ops[3] = {BaseType, AccessType, ConstantInt::get(Int64, Offset)}; + Metadata *Ops[3] = {BaseType, AccessType, + createConstant(ConstantInt::get(Int64, Offset))}; return MDNode::get(Context, Ops); } diff --git a/contrib/llvm/lib/IR/Mangler.cpp b/contrib/llvm/lib/IR/Mangler.cpp index 27d973b..5eeb797 100644 --- a/contrib/llvm/lib/IR/Mangler.cpp +++ b/contrib/llvm/lib/IR/Mangler.cpp @@ -22,23 +22,25 @@ using namespace llvm; static void getNameWithPrefixx(raw_ostream &OS, const Twine &GVName, Mangler::ManglerPrefixTy PrefixTy, - const DataLayout &DL, bool UseAt) { + const DataLayout &DL, char Prefix) { SmallString<256> TmpData; StringRef Name = GVName.toStringRef(TmpData); assert(!Name.empty() && "getNameWithPrefix requires non-empty name"); + // No need to do anything special if the global has the special "do not + // mangle" flag in the name. + if (Name[0] == '\1') { + OS << Name.substr(1); + return; + } + if (PrefixTy == Mangler::Private) OS << DL.getPrivateGlobalPrefix(); else if (PrefixTy == Mangler::LinkerPrivate) OS << DL.getLinkerPrivateGlobalPrefix(); - if (UseAt) { - OS << '@'; - } else { - char Prefix = DL.getGlobalPrefix(); - if (Prefix != '\0') - OS << Prefix; - } + if (Prefix != '\0') + OS << Prefix; // If this is a simple string that doesn't need escaping, just append it. OS << Name; @@ -46,7 +48,8 @@ static void getNameWithPrefixx(raw_ostream &OS, const Twine &GVName, void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName, ManglerPrefixTy PrefixTy) const { - return getNameWithPrefixx(OS, GVName, PrefixTy, *DL, false); + char Prefix = DL->getGlobalPrefix(); + return getNameWithPrefixx(OS, GVName, PrefixTy, *DL, Prefix); } void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, @@ -56,11 +59,21 @@ void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, return getNameWithPrefix(OS, GVName, PrefixTy); } -/// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require -/// a suffix on their name indicating the number of words of arguments they -/// take. -static void AddFastCallStdCallSuffix(raw_ostream &OS, const Function *F, - const DataLayout &TD) { +static bool hasByteCountSuffix(CallingConv::ID CC) { + switch (CC) { + case CallingConv::X86_FastCall: + case CallingConv::X86_StdCall: + case CallingConv::X86_VectorCall: + return true; + default: + return false; + } +} + +/// Microsoft fastcall and stdcall functions require a suffix on their name +/// indicating the number of words of arguments they take. +static void addByteCountSuffix(raw_ostream &OS, const Function *F, + const DataLayout &TD) { // Calculate arguments size total. unsigned ArgWords = 0; for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); @@ -69,8 +82,9 @@ static void AddFastCallStdCallSuffix(raw_ostream &OS, const Function *F, // 'Dereference' type in case of byval or inalloca parameter attribute. if (AI->hasByValOrInAllocaAttr()) Ty = cast<PointerType>(Ty)->getElementType(); - // Size should be aligned to DWORD boundary - ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4; + // Size should be aligned to pointer size. + unsigned PtrSize = TD.getPointerSize(); + ArgWords += RoundUpToAlignment(TD.getTypeAllocSize(Ty), PtrSize); } OS << '@' << ArgWords; @@ -99,41 +113,41 @@ void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, } StringRef Name = GV->getName(); - - // No need to do anything special if the global has the special "do not - // mangle" flag in the name. - if (Name[0] == '\1') { - OS << Name.substr(1); - return; - } - - bool UseAt = false; - const Function *MSFunc = nullptr; - CallingConv::ID CC; - if (DL->hasMicrosoftFastStdCallMangling()) { - if ((MSFunc = dyn_cast<Function>(GV))) { - CC = MSFunc->getCallingConv(); - // fastcall functions need to start with @ instead of _. - if (CC == CallingConv::X86_FastCall) - UseAt = true; - } + char Prefix = DL->getGlobalPrefix(); + + // Mangle functions with Microsoft calling conventions specially. Only do + // this mangling for x86_64 vectorcall and 32-bit x86. + const Function *MSFunc = dyn_cast<Function>(GV); + if (Name.startswith("\01")) + MSFunc = nullptr; // Don't mangle when \01 is present. + CallingConv::ID CC = + MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C; + if (!DL->hasMicrosoftFastStdCallMangling() && + CC != CallingConv::X86_VectorCall) + MSFunc = nullptr; + if (MSFunc) { + if (CC == CallingConv::X86_FastCall) + Prefix = '@'; // fastcall functions have an @ prefix instead of _. + else if (CC == CallingConv::X86_VectorCall) + Prefix = '\0'; // vectorcall functions have no prefix. } - getNameWithPrefixx(OS, Name, PrefixTy, *DL, UseAt); + getNameWithPrefixx(OS, Name, PrefixTy, *DL, Prefix); if (!MSFunc) return; - // If we are supposed to add a microsoft-style suffix for stdcall/fastcall, - // add it. - // fastcall and stdcall functions usually need @42 at the end to specify - // the argument info. + // If we are supposed to add a microsoft-style suffix for stdcall, fastcall, + // or vectorcall, add it. These functions have a suffix of @N where N is the + // cumulative byte size of all of the parameters to the function in decimal. + if (CC == CallingConv::X86_VectorCall) + OS << '@'; // vectorcall functions use a double @ suffix. FunctionType *FT = MSFunc->getFunctionType(); - if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) && + if (hasByteCountSuffix(CC) && // "Pure" variadic functions do not receive @0 suffix. (!FT->isVarArg() || FT->getNumParams() == 0 || (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr()))) - AddFastCallStdCallSuffix(OS, MSFunc, *DL); + addByteCountSuffix(OS, MSFunc, *DL); } void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, diff --git a/contrib/llvm/lib/IR/Metadata.cpp b/contrib/llvm/lib/IR/Metadata.cpp index 59137e4..2c6b332 100644 --- a/contrib/llvm/lib/IR/Metadata.cpp +++ b/contrib/llvm/lib/IR/Metadata.cpp @@ -22,396 +22,816 @@ #include "llvm/IR/ConstantRange.h" #include "llvm/IR/Instruction.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/LeakDetector.h" #include "llvm/IR/Module.h" #include "llvm/IR/ValueHandle.h" + using namespace llvm; -//===----------------------------------------------------------------------===// -// MDString implementation. -// +MetadataAsValue::MetadataAsValue(Type *Ty, Metadata *MD) + : Value(Ty, MetadataAsValueVal), MD(MD) { + track(); +} -void MDString::anchor() { } +MetadataAsValue::~MetadataAsValue() { + getType()->getContext().pImpl->MetadataAsValues.erase(MD); + untrack(); +} -MDString::MDString(LLVMContext &C) - : Value(Type::getMetadataTy(C), Value::MDStringVal) {} +/// \brief Canonicalize metadata arguments to intrinsics. +/// +/// To support bitcode upgrades (and assembly semantic sugar) for \a +/// MetadataAsValue, we need to canonicalize certain metadata. +/// +/// - nullptr is replaced by an empty MDNode. +/// - An MDNode with a single null operand is replaced by an empty MDNode. +/// - An MDNode whose only operand is a \a ConstantAsMetadata gets skipped. +/// +/// This maintains readability of bitcode from when metadata was a type of +/// value, and these bridges were unnecessary. +static Metadata *canonicalizeMetadataForValue(LLVMContext &Context, + Metadata *MD) { + if (!MD) + // !{} + return MDNode::get(Context, None); + + // Return early if this isn't a single-operand MDNode. + auto *N = dyn_cast<MDNode>(MD); + if (!N || N->getNumOperands() != 1) + return MD; + + if (!N->getOperand(0)) + // !{} + return MDNode::get(Context, None); + + if (auto *C = dyn_cast<ConstantAsMetadata>(N->getOperand(0))) + // Look through the MDNode. + return C; + + return MD; +} -MDString *MDString::get(LLVMContext &Context, StringRef Str) { - LLVMContextImpl *pImpl = Context.pImpl; - StringMapEntry<Value*> &Entry = - pImpl->MDStringCache.GetOrCreateValue(Str); - Value *&S = Entry.getValue(); - if (!S) S = new MDString(Context); - S->setValueName(&Entry); - return cast<MDString>(S); +MetadataAsValue *MetadataAsValue::get(LLVMContext &Context, Metadata *MD) { + MD = canonicalizeMetadataForValue(Context, MD); + auto *&Entry = Context.pImpl->MetadataAsValues[MD]; + if (!Entry) + Entry = new MetadataAsValue(Type::getMetadataTy(Context), MD); + return Entry; } -//===----------------------------------------------------------------------===// -// MDNodeOperand implementation. -// +MetadataAsValue *MetadataAsValue::getIfExists(LLVMContext &Context, + Metadata *MD) { + MD = canonicalizeMetadataForValue(Context, MD); + auto &Store = Context.pImpl->MetadataAsValues; + auto I = Store.find(MD); + return I == Store.end() ? nullptr : I->second; +} -// Use CallbackVH to hold MDNode operands. -namespace llvm { -class MDNodeOperand : public CallbackVH { - MDNode *getParent() { - MDNodeOperand *Cur = this; +void MetadataAsValue::handleChangedMetadata(Metadata *MD) { + LLVMContext &Context = getContext(); + MD = canonicalizeMetadataForValue(Context, MD); + auto &Store = Context.pImpl->MetadataAsValues; + + // Stop tracking the old metadata. + Store.erase(this->MD); + untrack(); + this->MD = nullptr; + + // Start tracking MD, or RAUW if necessary. + auto *&Entry = Store[MD]; + if (Entry) { + replaceAllUsesWith(Entry); + delete this; + return; + } + + this->MD = MD; + track(); + Entry = this; +} + +void MetadataAsValue::track() { + if (MD) + MetadataTracking::track(&MD, *MD, *this); +} + +void MetadataAsValue::untrack() { + if (MD) + MetadataTracking::untrack(MD); +} + +void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) { + bool WasInserted = + UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex))) + .second; + (void)WasInserted; + assert(WasInserted && "Expected to add a reference"); + + ++NextIndex; + assert(NextIndex != 0 && "Unexpected overflow"); +} + +void ReplaceableMetadataImpl::dropRef(void *Ref) { + bool WasErased = UseMap.erase(Ref); + (void)WasErased; + assert(WasErased && "Expected to drop a reference"); +} + +void ReplaceableMetadataImpl::moveRef(void *Ref, void *New, + const Metadata &MD) { + auto I = UseMap.find(Ref); + assert(I != UseMap.end() && "Expected to move a reference"); + auto OwnerAndIndex = I->second; + UseMap.erase(I); + bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second; + (void)WasInserted; + assert(WasInserted && "Expected to add a reference"); + + // Check that the references are direct if there's no owner. + (void)MD; + assert((OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) && + "Reference without owner must be direct"); + assert((OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) && + "Reference without owner must be direct"); +} + +void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) { + assert(!(MD && isa<MDNodeFwdDecl>(MD)) && "Expected non-temp node"); + + if (UseMap.empty()) + return; + + // Copy out uses since UseMap will get touched below. + typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy; + SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end()); + std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { + return L.second.second < R.second.second; + }); + for (const auto &Pair : Uses) { + // Check that this Ref hasn't disappeared after RAUW (when updating a + // previous Ref). + if (!UseMap.count(Pair.first)) + continue; + + OwnerTy Owner = Pair.second.first; + if (!Owner) { + // Update unowned tracking references directly. + Metadata *&Ref = *static_cast<Metadata **>(Pair.first); + Ref = MD; + if (MD) + MetadataTracking::track(Ref); + UseMap.erase(Pair.first); + continue; + } + + // Check for MetadataAsValue. + if (Owner.is<MetadataAsValue *>()) { + Owner.get<MetadataAsValue *>()->handleChangedMetadata(MD); + continue; + } + + // There's a Metadata owner -- dispatch. + Metadata *OwnerMD = Owner.get<Metadata *>(); + switch (OwnerMD->getMetadataID()) { +#define HANDLE_METADATA_LEAF(CLASS) \ + case Metadata::CLASS##Kind: \ + cast<CLASS>(OwnerMD)->handleChangedOperand(Pair.first, MD); \ + continue; +#include "llvm/IR/Metadata.def" + default: + llvm_unreachable("Invalid metadata subclass"); + } + } + assert(UseMap.empty() && "Expected all uses to be replaced"); +} + +void ReplaceableMetadataImpl::resolveAllUses(bool ResolveUsers) { + if (UseMap.empty()) + return; + + if (!ResolveUsers) { + UseMap.clear(); + return; + } + + // Copy out uses since UseMap could get touched below. + typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy; + SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end()); + std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { + return L.second.second < R.second.second; + }); + UseMap.clear(); + for (const auto &Pair : Uses) { + auto Owner = Pair.second.first; + if (!Owner) + continue; + if (Owner.is<MetadataAsValue *>()) + continue; - while (Cur->getValPtrInt() != 1) - --Cur; + // Resolve UniquableMDNodes that point at this. + auto *OwnerMD = dyn_cast<UniquableMDNode>(Owner.get<Metadata *>()); + if (!OwnerMD) + continue; + if (OwnerMD->isResolved()) + continue; + OwnerMD->decrementUnresolvedOperandCount(); + } +} - assert(Cur->getValPtrInt() == 1 && - "Couldn't find the beginning of the operand list!"); - return reinterpret_cast<MDNode*>(Cur) - 1; +static Function *getLocalFunction(Value *V) { + assert(V && "Expected value"); + if (auto *A = dyn_cast<Argument>(V)) + return A->getParent(); + if (BasicBlock *BB = cast<Instruction>(V)->getParent()) + return BB->getParent(); + return nullptr; +} + +ValueAsMetadata *ValueAsMetadata::get(Value *V) { + assert(V && "Unexpected null Value"); + + auto &Context = V->getContext(); + auto *&Entry = Context.pImpl->ValuesAsMetadata[V]; + if (!Entry) { + assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) && + "Expected constant or function-local value"); + assert(!V->NameAndIsUsedByMD.getInt() && + "Expected this to be the only metadata use"); + V->NameAndIsUsedByMD.setInt(true); + if (auto *C = dyn_cast<Constant>(V)) + Entry = new ConstantAsMetadata(C); + else + Entry = new LocalAsMetadata(V); } -public: - MDNodeOperand(Value *V) : CallbackVH(V) {} - virtual ~MDNodeOperand(); + return Entry; +} + +ValueAsMetadata *ValueAsMetadata::getIfExists(Value *V) { + assert(V && "Unexpected null Value"); + return V->getContext().pImpl->ValuesAsMetadata.lookup(V); +} - void set(Value *V) { - unsigned IsFirst = this->getValPtrInt(); - this->setValPtr(V); - this->setAsFirstOperand(IsFirst); +void ValueAsMetadata::handleDeletion(Value *V) { + assert(V && "Expected valid value"); + + auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata; + auto I = Store.find(V); + if (I == Store.end()) + return; + + // Remove old entry from the map. + ValueAsMetadata *MD = I->second; + assert(MD && "Expected valid metadata"); + assert(MD->getValue() == V && "Expected valid mapping"); + Store.erase(I); + + // Delete the metadata. + MD->replaceAllUsesWith(nullptr); + delete MD; +} + +void ValueAsMetadata::handleRAUW(Value *From, Value *To) { + assert(From && "Expected valid value"); + assert(To && "Expected valid value"); + assert(From != To && "Expected changed value"); + assert(From->getType() == To->getType() && "Unexpected type change"); + + LLVMContext &Context = From->getType()->getContext(); + auto &Store = Context.pImpl->ValuesAsMetadata; + auto I = Store.find(From); + if (I == Store.end()) { + assert(!From->NameAndIsUsedByMD.getInt() && + "Expected From not to be used by metadata"); + return; + } + + // Remove old entry from the map. + assert(From->NameAndIsUsedByMD.getInt() && + "Expected From to be used by metadata"); + From->NameAndIsUsedByMD.setInt(false); + ValueAsMetadata *MD = I->second; + assert(MD && "Expected valid metadata"); + assert(MD->getValue() == From && "Expected valid mapping"); + Store.erase(I); + + if (isa<LocalAsMetadata>(MD)) { + if (auto *C = dyn_cast<Constant>(To)) { + // Local became a constant. + MD->replaceAllUsesWith(ConstantAsMetadata::get(C)); + delete MD; + return; + } + if (getLocalFunction(From) && getLocalFunction(To) && + getLocalFunction(From) != getLocalFunction(To)) { + // Function changed. + MD->replaceAllUsesWith(nullptr); + delete MD; + return; + } + } else if (!isa<Constant>(To)) { + // Changed to function-local value. + MD->replaceAllUsesWith(nullptr); + delete MD; + return; } - /// setAsFirstOperand - Accessor method to mark the operand as the first in - /// the list. - void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); } + auto *&Entry = Store[To]; + if (Entry) { + // The target already exists. + MD->replaceAllUsesWith(Entry); + delete MD; + return; + } - void deleted() override; - void allUsesReplacedWith(Value *NV) override; -}; -} // end namespace llvm. + // Update MD in place (and update the map entry). + assert(!To->NameAndIsUsedByMD.getInt() && + "Expected this to be the only metadata use"); + To->NameAndIsUsedByMD.setInt(true); + MD->V = To; + Entry = MD; +} -// Provide out-of-line definition to prevent weak vtable. -MDNodeOperand::~MDNodeOperand() {} +//===----------------------------------------------------------------------===// +// MDString implementation. +// -void MDNodeOperand::deleted() { - getParent()->replaceOperand(this, nullptr); +MDString *MDString::get(LLVMContext &Context, StringRef Str) { + auto &Store = Context.pImpl->MDStringCache; + auto I = Store.find(Str); + if (I != Store.end()) + return &I->second; + + auto *Entry = + StringMapEntry<MDString>::Create(Str, Store.getAllocator(), MDString()); + bool WasInserted = Store.insert(Entry); + (void)WasInserted; + assert(WasInserted && "Expected entry to be inserted"); + Entry->second.Entry = Entry; + return &Entry->second; } -void MDNodeOperand::allUsesReplacedWith(Value *NV) { - getParent()->replaceOperand(this, NV); +StringRef MDString::getString() const { + assert(Entry && "Expected to find string map entry"); + return Entry->first(); } //===----------------------------------------------------------------------===// // MDNode implementation. // -/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on -/// the end of the MDNode. -static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) { - // Use <= instead of < to permit a one-past-the-end address. - assert(Op <= N->getNumOperands() && "Invalid operand number"); - return reinterpret_cast<MDNodeOperand*>(N + 1) + Op; +void *MDNode::operator new(size_t Size, unsigned NumOps) { + void *Ptr = ::operator new(Size + NumOps * sizeof(MDOperand)); + MDOperand *O = static_cast<MDOperand *>(Ptr); + for (MDOperand *E = O + NumOps; O != E; ++O) + (void)new (O) MDOperand; + return O; } -void MDNode::replaceOperandWith(unsigned i, Value *Val) { - MDNodeOperand *Op = getOperandPtr(this, i); - replaceOperand(Op, Val); +void MDNode::operator delete(void *Mem) { + MDNode *N = static_cast<MDNode *>(Mem); + MDOperand *O = static_cast<MDOperand *>(Mem); + for (MDOperand *E = O - N->NumOperands; O != E; --O) + (O - 1)->~MDOperand(); + ::operator delete(O); } -MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal) -: Value(Type::getMetadataTy(C), Value::MDNodeVal) { - NumOperands = Vals.size(); +MDNode::MDNode(LLVMContext &Context, unsigned ID, ArrayRef<Metadata *> MDs) + : Metadata(ID), Context(Context), NumOperands(MDs.size()), + MDNodeSubclassData(0) { + for (unsigned I = 0, E = MDs.size(); I != E; ++I) + setOperand(I, MDs[I]); +} - if (isFunctionLocal) - setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit); +bool MDNode::isResolved() const { + if (isa<MDNodeFwdDecl>(this)) + return false; + return cast<UniquableMDNode>(this)->isResolved(); +} - // Initialize the operand list, which is co-allocated on the end of the node. - unsigned i = 0; - for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands; - Op != E; ++Op, ++i) { - new (Op) MDNodeOperand(Vals[i]); +static bool isOperandUnresolved(Metadata *Op) { + if (auto *N = dyn_cast_or_null<MDNode>(Op)) + return !N->isResolved(); + return false; +} - // Mark the first MDNodeOperand as being the first in the list of operands. - if (i == 0) - Op->setAsFirstOperand(1); - } +UniquableMDNode::UniquableMDNode(LLVMContext &C, unsigned ID, + ArrayRef<Metadata *> Vals, bool AllowRAUW) + : MDNode(C, ID, Vals) { + if (!AllowRAUW) + return; + + // Check whether any operands are unresolved, requiring re-uniquing. + unsigned NumUnresolved = 0; + for (const auto &Op : operands()) + NumUnresolved += unsigned(isOperandUnresolved(Op)); + + if (!NumUnresolved) + return; + + ReplaceableUses.reset(new ReplaceableMetadataImpl); + SubclassData32 = NumUnresolved; } -/// ~MDNode - Destroy MDNode. -MDNode::~MDNode() { - assert((getSubclassDataFromValue() & DestroyFlag) != 0 && - "Not being destroyed through destroy()?"); - LLVMContextImpl *pImpl = getType()->getContext().pImpl; - if (isNotUniqued()) { - pImpl->NonUniquedMDNodes.erase(this); - } else { - pImpl->MDNodeSet.RemoveNode(this); - } +void UniquableMDNode::resolve() { + assert(!isResolved() && "Expected this to be unresolved"); + + // Move the map, so that this immediately looks resolved. + auto Uses = std::move(ReplaceableUses); + SubclassData32 = 0; + assert(isResolved() && "Expected this to be resolved"); - // Destroy the operands. - for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands; - Op != E; ++Op) - Op->~MDNodeOperand(); + // Drop RAUW support. + Uses->resolveAllUses(); } -static const Function *getFunctionForValue(Value *V) { - if (!V) return nullptr; - if (Instruction *I = dyn_cast<Instruction>(V)) { - BasicBlock *BB = I->getParent(); - return BB ? BB->getParent() : nullptr; - } - if (Argument *A = dyn_cast<Argument>(V)) - return A->getParent(); - if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) - return BB->getParent(); - if (MDNode *MD = dyn_cast<MDNode>(V)) - return MD->getFunction(); - return nullptr; +void UniquableMDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) { + assert(SubclassData32 != 0 && "Expected unresolved operands"); + + // Check if an operand was resolved. + if (!isOperandUnresolved(Old)) { + if (isOperandUnresolved(New)) + // An operand was un-resolved! + ++SubclassData32; + } else if (!isOperandUnresolved(New)) + decrementUnresolvedOperandCount(); } -#ifndef NDEBUG -static const Function *assertLocalFunction(const MDNode *N) { - if (!N->isFunctionLocal()) return nullptr; - - // FIXME: This does not handle cyclic function local metadata. - const Function *F = nullptr, *NewF = nullptr; - for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { - if (Value *V = N->getOperand(i)) { - if (MDNode *MD = dyn_cast<MDNode>(V)) - NewF = assertLocalFunction(MD); - else - NewF = getFunctionForValue(V); - } - if (!F) - F = NewF; - else - assert((NewF == nullptr || F == NewF) && - "inconsistent function-local metadata"); +void UniquableMDNode::decrementUnresolvedOperandCount() { + if (!--SubclassData32) + // Last unresolved operand has just been resolved. + resolve(); +} + +void UniquableMDNode::resolveCycles() { + if (isResolved()) + return; + + // Resolve this node immediately. + resolve(); + + // Resolve all operands. + for (const auto &Op : operands()) { + if (!Op) + continue; + assert(!isa<MDNodeFwdDecl>(Op) && + "Expected all forward declarations to be resolved"); + if (auto *N = dyn_cast<UniquableMDNode>(Op)) + if (!N->isResolved()) + N->resolveCycles(); } - return F; } -#endif -// getFunction - If this metadata is function-local and recursively has a -// function-local operand, return the first such operand's parent function. -// Otherwise, return null. getFunction() should not be used for performance- -// critical code because it recursively visits all the MDNode's operands. -const Function *MDNode::getFunction() const { +void MDTuple::recalculateHash() { + setHash(hash_combine_range(op_begin(), op_end())); #ifndef NDEBUG - return assertLocalFunction(this); -#else - if (!isFunctionLocal()) return nullptr; - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - if (const Function *F = getFunctionForValue(getOperand(i))) - return F; - return nullptr; + { + SmallVector<Metadata *, 8> MDs(op_begin(), op_end()); + unsigned RawHash = hash_combine_range(MDs.begin(), MDs.end()); + assert(getHash() == RawHash && + "Expected hash of MDOperand to equal hash of Metadata*"); + } #endif } -// destroy - Delete this node. Only when there are no uses. -void MDNode::destroy() { - setValueSubclassData(getSubclassDataFromValue() | DestroyFlag); - // Placement delete, then free the memory. - this->~MDNode(); - free(this); +void MDNode::dropAllReferences() { + for (unsigned I = 0, E = NumOperands; I != E; ++I) + setOperand(I, nullptr); + if (auto *N = dyn_cast<UniquableMDNode>(this)) + if (!N->isResolved()) { + N->ReplaceableUses->resolveAllUses(/* ResolveUsers */ false); + N->ReplaceableUses.reset(); + } } -/// isFunctionLocalValue - Return true if this is a value that would require a -/// function-local MDNode. -static bool isFunctionLocalValue(Value *V) { - return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) || - (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal()); +namespace llvm { +/// \brief Make MDOperand transparent for hashing. +/// +/// This overload of an implementation detail of the hashing library makes +/// MDOperand hash to the same value as a \a Metadata pointer. +/// +/// Note that overloading \a hash_value() as follows: +/// +/// \code +/// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); } +/// \endcode +/// +/// does not cause MDOperand to be transparent. In particular, a bare pointer +/// doesn't get hashed before it's combined, whereas \a MDOperand would. +static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); } } -MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals, - FunctionLocalness FL, bool Insert) { - LLVMContextImpl *pImpl = Context.pImpl; +void UniquableMDNode::handleChangedOperand(void *Ref, Metadata *New) { + unsigned Op = static_cast<MDOperand *>(Ref) - op_begin(); + assert(Op < getNumOperands() && "Expected valid operand"); - // Add all the operand pointers. Note that we don't have to add the - // isFunctionLocal bit because that's implied by the operands. - // Note that if the operands are later nulled out, the node will be - // removed from the uniquing map. - FoldingSetNodeID ID; - for (Value *V : Vals) - ID.AddPointer(V); + if (isStoredDistinctInContext()) { + assert(isResolved() && "Expected distinct node to be resolved"); - void *InsertPoint; - MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); + // This node is not uniqued. Just set the operand and be done with it. + setOperand(Op, New); + return; + } - if (N || !Insert) - return N; + // This node is uniqued. + eraseFromStore(); - bool isFunctionLocal = false; - switch (FL) { - case FL_Unknown: - for (Value *V : Vals) { - if (!V) continue; - if (isFunctionLocalValue(V)) { - isFunctionLocal = true; - break; - } - } - break; - case FL_No: - isFunctionLocal = false; + Metadata *Old = getOperand(Op); + setOperand(Op, New); + + // Drop uniquing for self-reference cycles. + if (New == this) { + storeDistinctInContext(); + if (!isResolved()) + resolve(); + return; + } + + // Re-unique the node. + auto *Uniqued = uniquify(); + if (Uniqued == this) { + if (!isResolved()) + resolveAfterOperandChange(Old, New); + return; + } + + // Collision. + if (!isResolved()) { + // Still unresolved, so RAUW. + // + // First, clear out all operands to prevent any recursion (similar to + // dropAllReferences(), but we still need the use-list). + for (unsigned O = 0, E = getNumOperands(); O != E; ++O) + setOperand(O, nullptr); + ReplaceableUses->replaceAllUsesWith(Uniqued); + deleteAsSubclass(); + return; + } + + // Store in non-uniqued form if RAUW isn't possible. + storeDistinctInContext(); +} + +void UniquableMDNode::deleteAsSubclass() { + switch (getMetadataID()) { + default: + llvm_unreachable("Invalid subclass of UniquableMDNode"); +#define HANDLE_UNIQUABLE_LEAF(CLASS) \ + case CLASS##Kind: \ + delete cast<CLASS>(this); \ break; - case FL_Yes: - isFunctionLocal = true; +#include "llvm/IR/Metadata.def" + } +} + +UniquableMDNode *UniquableMDNode::uniquify() { + switch (getMetadataID()) { + default: + llvm_unreachable("Invalid subclass of UniquableMDNode"); +#define HANDLE_UNIQUABLE_LEAF(CLASS) \ + case CLASS##Kind: \ + return cast<CLASS>(this)->uniquifyImpl(); +#include "llvm/IR/Metadata.def" + } +} + +void UniquableMDNode::eraseFromStore() { + switch (getMetadataID()) { + default: + llvm_unreachable("Invalid subclass of UniquableMDNode"); +#define HANDLE_UNIQUABLE_LEAF(CLASS) \ + case CLASS##Kind: \ + cast<CLASS>(this)->eraseFromStoreImpl(); \ break; +#include "llvm/IR/Metadata.def" } +} - // Coallocate space for the node and Operands together, then placement new. - void *Ptr = malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand)); - N = new (Ptr) MDNode(Context, Vals, isFunctionLocal); +MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs, + bool ShouldCreate) { + MDTupleInfo::KeyTy Key(MDs); - // Cache the operand hash. - N->Hash = ID.ComputeHash(); + auto &Store = Context.pImpl->MDTuples; + auto I = Store.find_as(Key); + if (I != Store.end()) + return *I; + if (!ShouldCreate) + return nullptr; - // InsertPoint will have been set by the FindNodeOrInsertPos call. - pImpl->MDNodeSet.InsertNode(N, InsertPoint); + // Coallocate space for the node and Operands together, then placement new. + auto *N = new (MDs.size()) MDTuple(Context, MDs, /* AllowRAUW */ true); + N->setHash(Key.Hash); + Store.insert(N); + return N; +} +MDTuple *MDTuple::getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) { + auto *N = new (MDs.size()) MDTuple(Context, MDs, /* AllowRAUW */ false); + N->storeDistinctInContext(); return N; } -MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) { - return getMDNode(Context, Vals, FL_Unknown); +MDTuple *MDTuple::uniquifyImpl() { + recalculateHash(); + MDTupleInfo::KeyTy Key(this); + + auto &Store = getContext().pImpl->MDTuples; + auto I = Store.find_as(Key); + if (I == Store.end()) { + Store.insert(this); + return this; + } + return *I; } -MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, - ArrayRef<Value*> Vals, - bool isFunctionLocal) { - return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No); +void MDTuple::eraseFromStoreImpl() { getContext().pImpl->MDTuples.erase(this); } + +MDLocation::MDLocation(LLVMContext &C, unsigned Line, unsigned Column, + ArrayRef<Metadata *> MDs, bool AllowRAUW) + : UniquableMDNode(C, MDLocationKind, MDs, AllowRAUW) { + assert((MDs.size() == 1 || MDs.size() == 2) && + "Expected a scope and optional inlined-at"); + + // Set line and column. + assert(Line < (1u << 24) && "Expected 24-bit line"); + assert(Column < (1u << 8) && "Expected 8-bit column"); + + MDNodeSubclassData = Line; + SubclassData16 = Column; } -MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) { - return getMDNode(Context, Vals, FL_Unknown, false); +MDLocation *MDLocation::constructHelper(LLVMContext &Context, unsigned Line, + unsigned Column, Metadata *Scope, + Metadata *InlinedAt, bool AllowRAUW) { + SmallVector<Metadata *, 2> Ops; + Ops.push_back(Scope); + if (InlinedAt) + Ops.push_back(InlinedAt); + return new (Ops.size()) MDLocation(Context, Line, Column, Ops, AllowRAUW); } -MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) { - MDNode *N = - (MDNode *)malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand)); - N = new (N) MDNode(Context, Vals, FL_No); - N->setValueSubclassData(N->getSubclassDataFromValue() | - NotUniquedBit); - LeakDetector::addGarbageObject(N); +static void adjustLine(unsigned &Line) { + // Set to unknown on overflow. Still use 24 bits for now. + if (Line >= (1u << 24)) + Line = 0; +} + +static void adjustColumn(unsigned &Column) { + // Set to unknown on overflow. Still use 8 bits for now. + if (Column >= (1u << 8)) + Column = 0; +} + +MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line, + unsigned Column, Metadata *Scope, + Metadata *InlinedAt, bool ShouldCreate) { + // Fixup line/column. + adjustLine(Line); + adjustColumn(Column); + + MDLocationInfo::KeyTy Key(Line, Column, Scope, InlinedAt); + + auto &Store = Context.pImpl->MDLocations; + auto I = Store.find_as(Key); + if (I != Store.end()) + return *I; + if (!ShouldCreate) + return nullptr; + + auto *N = constructHelper(Context, Line, Column, Scope, InlinedAt, + /* AllowRAUW */ true); + Store.insert(N); return N; } -void MDNode::deleteTemporary(MDNode *N) { - assert(N->use_empty() && "Temporary MDNode has uses!"); - assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) && - "Deleting a non-temporary uniqued node!"); - assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) && - "Deleting a non-temporary non-uniqued node!"); - assert((N->getSubclassDataFromValue() & NotUniquedBit) && - "Temporary MDNode does not have NotUniquedBit set!"); - assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 && - "Temporary MDNode has DestroyFlag set!"); - LeakDetector::removeGarbageObject(N); - N->destroy(); -} - -/// getOperand - Return specified operand. -Value *MDNode::getOperand(unsigned i) const { - assert(i < getNumOperands() && "Invalid operand number"); - return *getOperandPtr(const_cast<MDNode*>(this), i); -} - -void MDNode::Profile(FoldingSetNodeID &ID) const { - // Add all the operand pointers. Note that we don't have to add the - // isFunctionLocal bit because that's implied by the operands. - // Note that if the operands are later nulled out, the node will be - // removed from the uniquing map. - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - ID.AddPointer(getOperand(i)); -} - -void MDNode::setIsNotUniqued() { - setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit); - LLVMContextImpl *pImpl = getType()->getContext().pImpl; - pImpl->NonUniquedMDNodes.insert(this); -} - -// Replace value from this node's operand list. -void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) { - Value *From = *Op; - - // If is possible that someone did GV->RAUW(inst), replacing a global variable - // with an instruction or some other function-local object. If this is a - // non-function-local MDNode, it can't point to a function-local object. - // Handle this case by implicitly dropping the MDNode reference to null. - // Likewise if the MDNode is function-local but for a different function. - if (To && isFunctionLocalValue(To)) { - if (!isFunctionLocal()) - To = nullptr; - else { - const Function *F = getFunction(); - const Function *FV = getFunctionForValue(To); - // Metadata can be function-local without having an associated function. - // So only consider functions to have changed if non-null. - if (F && FV && F != FV) - To = nullptr; - } +MDLocation *MDLocation::getDistinct(LLVMContext &Context, unsigned Line, + unsigned Column, Metadata *Scope, + Metadata *InlinedAt) { + // Fixup line/column. + adjustLine(Line); + adjustColumn(Column); + + auto *N = constructHelper(Context, Line, Column, Scope, InlinedAt, + /* AllowRAUW */ false); + N->storeDistinctInContext(); + return N; +} + +MDLocation *MDLocation::uniquifyImpl() { + MDLocationInfo::KeyTy Key(this); + + auto &Store = getContext().pImpl->MDLocations; + auto I = Store.find_as(Key); + if (I == Store.end()) { + Store.insert(this); + return this; } - - if (From == To) - return; + return *I; +} - // Update the operand. - Op->set(To); +void MDLocation::eraseFromStoreImpl() { + getContext().pImpl->MDLocations.erase(this); +} - // If this node is already not being uniqued (because one of the operands - // already went to null), then there is nothing else to do here. - if (isNotUniqued()) return; +MDNodeFwdDecl *MDNode::getTemporary(LLVMContext &Context, + ArrayRef<Metadata *> MDs) { + return MDNodeFwdDecl::get(Context, MDs); +} - LLVMContextImpl *pImpl = getType()->getContext().pImpl; +void MDNode::deleteTemporary(MDNode *N) { delete cast<MDNodeFwdDecl>(N); } - // Remove "this" from the context map. FoldingSet doesn't have to reprofile - // this node to remove it, so we don't care what state the operands are in. - pImpl->MDNodeSet.RemoveNode(this); +void UniquableMDNode::storeDistinctInContext() { + assert(!IsDistinctInContext && "Expected newly distinct metadata"); + IsDistinctInContext = true; + if (auto *T = dyn_cast<MDTuple>(this)) + T->setHash(0); + getContext().pImpl->DistinctMDNodes.insert(this); +} - // If we are dropping an argument to null, we choose to not unique the MDNode - // anymore. This commonly occurs during destruction, and uniquing these - // brings little reuse. Also, this means we don't need to include - // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes. - if (!To) { - setIsNotUniqued(); +void MDNode::replaceOperandWith(unsigned I, Metadata *New) { + if (getOperand(I) == New) return; - } - // Now that the node is out of the folding set, get ready to reinsert it. - // First, check to see if another node with the same operands already exists - // in the set. If so, then this node is redundant. - FoldingSetNodeID ID; - Profile(ID); - void *InsertPoint; - if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) { - replaceAllUsesWith(N); - destroy(); + if (isDistinct()) { + setOperand(I, New); return; } - // Cache the operand hash. - Hash = ID.ComputeHash(); - // InsertPoint will have been set by the FindNodeOrInsertPos call. - pImpl->MDNodeSet.InsertNode(this, InsertPoint); - - // If this MDValue was previously function-local but no longer is, clear - // its function-local flag. - if (isFunctionLocal() && !isFunctionLocalValue(To)) { - bool isStillFunctionLocal = false; - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { - Value *V = getOperand(i); - if (!V) continue; - if (isFunctionLocalValue(V)) { - isStillFunctionLocal = true; + cast<UniquableMDNode>(this)->handleChangedOperand(mutable_begin() + I, New); +} + +void MDNode::setOperand(unsigned I, Metadata *New) { + assert(I < NumOperands); + if (isStoredDistinctInContext() || isa<MDNodeFwdDecl>(this)) + // No need for a callback, this isn't uniqued. + mutable_begin()[I].reset(New, nullptr); + else + mutable_begin()[I].reset(New, this); +} + +/// \brief Get a node, or a self-reference that looks like it. +/// +/// Special handling for finding self-references, for use by \a +/// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from +/// when self-referencing nodes were still uniqued. If the first operand has +/// the same operands as \c Ops, return the first operand instead. +static MDNode *getOrSelfReference(LLVMContext &Context, + ArrayRef<Metadata *> Ops) { + if (!Ops.empty()) + if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0])) + if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) { + for (unsigned I = 1, E = Ops.size(); I != E; ++I) + if (Ops[I] != N->getOperand(I)) + return MDNode::get(Context, Ops); + return N; + } + + return MDNode::get(Context, Ops); +} + +MDNode *MDNode::concatenate(MDNode *A, MDNode *B) { + if (!A) + return B; + if (!B) + return A; + + SmallVector<Metadata *, 4> MDs(A->getNumOperands() + B->getNumOperands()); + + unsigned j = 0; + for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) + MDs[j++] = A->getOperand(i); + for (unsigned i = 0, ie = B->getNumOperands(); i != ie; ++i) + MDs[j++] = B->getOperand(i); + + // FIXME: This preserves long-standing behaviour, but is it really the right + // behaviour? Or was that an unintended side-effect of node uniquing? + return getOrSelfReference(A->getContext(), MDs); +} + +MDNode *MDNode::intersect(MDNode *A, MDNode *B) { + if (!A || !B) + return nullptr; + + SmallVector<Metadata *, 4> MDs; + for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) { + Metadata *MD = A->getOperand(i); + for (unsigned j = 0, je = B->getNumOperands(); j != je; ++j) + if (MD == B->getOperand(j)) { + MDs.push_back(MD); break; } - } - if (!isStillFunctionLocal) - setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit); } + + // FIXME: This preserves long-standing behaviour, but is it really the right + // behaviour? Or was that an unintended side-effect of node uniquing? + return getOrSelfReference(A->getContext(), MDs); } MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) { if (!A || !B) return nullptr; - APFloat AVal = cast<ConstantFP>(A->getOperand(0))->getValueAPF(); - APFloat BVal = cast<ConstantFP>(B->getOperand(0))->getValueAPF(); + APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF(); + APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF(); if (AVal.compare(BVal) == APFloat::cmpLessThan) return A; return B; @@ -425,25 +845,27 @@ static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) { return !A.intersectWith(B).isEmptySet() || isContiguous(A, B); } -static bool tryMergeRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low, - ConstantInt *High) { +static bool tryMergeRange(SmallVectorImpl<ConstantInt *> &EndPoints, + ConstantInt *Low, ConstantInt *High) { ConstantRange NewRange(Low->getValue(), High->getValue()); unsigned Size = EndPoints.size(); - APInt LB = cast<ConstantInt>(EndPoints[Size - 2])->getValue(); - APInt LE = cast<ConstantInt>(EndPoints[Size - 1])->getValue(); + APInt LB = EndPoints[Size - 2]->getValue(); + APInt LE = EndPoints[Size - 1]->getValue(); ConstantRange LastRange(LB, LE); if (canBeMerged(NewRange, LastRange)) { ConstantRange Union = LastRange.unionWith(NewRange); Type *Ty = High->getType(); - EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower()); - EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper()); + EndPoints[Size - 2] = + cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower())); + EndPoints[Size - 1] = + cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper())); return true; } return false; } -static void addRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low, - ConstantInt *High) { +static void addRange(SmallVectorImpl<ConstantInt *> &EndPoints, + ConstantInt *Low, ConstantInt *High) { if (!EndPoints.empty()) if (tryMergeRange(EndPoints, Low, High)) return; @@ -465,31 +887,33 @@ MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) { // First, walk both lists in older of the lower boundary of each interval. // At each step, try to merge the new interval to the last one we adedd. - SmallVector<Value*, 4> EndPoints; + SmallVector<ConstantInt *, 4> EndPoints; int AI = 0; int BI = 0; int AN = A->getNumOperands() / 2; int BN = B->getNumOperands() / 2; while (AI < AN && BI < BN) { - ConstantInt *ALow = cast<ConstantInt>(A->getOperand(2 * AI)); - ConstantInt *BLow = cast<ConstantInt>(B->getOperand(2 * BI)); + ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI)); + ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI)); if (ALow->getValue().slt(BLow->getValue())) { - addRange(EndPoints, ALow, cast<ConstantInt>(A->getOperand(2 * AI + 1))); + addRange(EndPoints, ALow, + mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1))); ++AI; } else { - addRange(EndPoints, BLow, cast<ConstantInt>(B->getOperand(2 * BI + 1))); + addRange(EndPoints, BLow, + mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1))); ++BI; } } while (AI < AN) { - addRange(EndPoints, cast<ConstantInt>(A->getOperand(2 * AI)), - cast<ConstantInt>(A->getOperand(2 * AI + 1))); + addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)), + mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1))); ++AI; } while (BI < BN) { - addRange(EndPoints, cast<ConstantInt>(B->getOperand(2 * BI)), - cast<ConstantInt>(B->getOperand(2 * BI + 1))); + addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)), + mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1))); ++BI; } @@ -497,8 +921,8 @@ MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) { // the last and first ones. unsigned Size = EndPoints.size(); if (Size > 4) { - ConstantInt *FB = cast<ConstantInt>(EndPoints[0]); - ConstantInt *FE = cast<ConstantInt>(EndPoints[1]); + ConstantInt *FB = EndPoints[0]; + ConstantInt *FE = EndPoints[1]; if (tryMergeRange(EndPoints, FB, FE)) { for (unsigned i = 0; i < Size - 2; ++i) { EndPoints[i] = EndPoints[i + 2]; @@ -510,63 +934,60 @@ MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) { // If in the end we have a single range, it is possible that it is now the // full range. Just drop the metadata in that case. if (EndPoints.size() == 2) { - ConstantRange Range(cast<ConstantInt>(EndPoints[0])->getValue(), - cast<ConstantInt>(EndPoints[1])->getValue()); + ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue()); if (Range.isFullSet()) return nullptr; } - return MDNode::get(A->getContext(), EndPoints); + SmallVector<Metadata *, 4> MDs; + MDs.reserve(EndPoints.size()); + for (auto *I : EndPoints) + MDs.push_back(ConstantAsMetadata::get(I)); + return MDNode::get(A->getContext(), MDs); } //===----------------------------------------------------------------------===// // NamedMDNode implementation. // -static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) { - return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands; +static SmallVector<TrackingMDRef, 4> &getNMDOps(void *Operands) { + return *(SmallVector<TrackingMDRef, 4> *)Operands; } NamedMDNode::NamedMDNode(const Twine &N) - : Name(N.str()), Parent(nullptr), - Operands(new SmallVector<TrackingVH<MDNode>, 4>()) { -} + : Name(N.str()), Parent(nullptr), + Operands(new SmallVector<TrackingMDRef, 4>()) {} NamedMDNode::~NamedMDNode() { dropAllReferences(); delete &getNMDOps(Operands); } -/// getNumOperands - Return number of NamedMDNode operands. unsigned NamedMDNode::getNumOperands() const { return (unsigned)getNMDOps(Operands).size(); } -/// getOperand - Return specified operand. MDNode *NamedMDNode::getOperand(unsigned i) const { assert(i < getNumOperands() && "Invalid Operand number!"); - return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]); + auto *N = getNMDOps(Operands)[i].get(); + return cast_or_null<MDNode>(N); } -/// addOperand - Add metadata Operand. -void NamedMDNode::addOperand(MDNode *M) { - assert(!M->isFunctionLocal() && - "NamedMDNode operands must not be function-local!"); - getNMDOps(Operands).push_back(TrackingVH<MDNode>(M)); +void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); } + +void NamedMDNode::setOperand(unsigned I, MDNode *New) { + assert(I < getNumOperands() && "Invalid operand number"); + getNMDOps(Operands)[I].reset(New); } -/// eraseFromParent - Drop all references and remove the node from parent -/// module. void NamedMDNode::eraseFromParent() { getParent()->eraseNamedMetadata(this); } -/// dropAllReferences - Remove all uses and clear node vector. void NamedMDNode::dropAllReferences() { getNMDOps(Operands).clear(); } -/// getName - Return a constant reference to this named metadata's name. StringRef NamedMDNode::getName() const { return StringRef(Name); } @@ -576,7 +997,8 @@ StringRef NamedMDNode::getName() const { // void Instruction::setMetadata(StringRef Kind, MDNode *Node) { - if (!Node && !hasMetadata()) return; + if (!Node && !hasMetadata()) + return; setMetadata(getContext().getMDKindID(Kind), Node); } @@ -615,7 +1037,7 @@ void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) { continue; } - Info[I] = Info.back(); + Info[I] = std::move(Info.back()); Info.pop_back(); --E; } @@ -632,7 +1054,8 @@ void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) { /// node. This updates/replaces metadata if already present, or removes it if /// Node is null. void Instruction::setMetadata(unsigned KindID, MDNode *Node) { - if (!Node && !hasMetadata()) return; + if (!Node && !hasMetadata()) + return; // Handle 'dbg' as a special case since it is not stored in the hash table. if (KindID == LLVMContext::MD_dbg) { @@ -651,13 +1074,14 @@ void Instruction::setMetadata(unsigned KindID, MDNode *Node) { // Handle replacement of an existing value. for (auto &P : Info) if (P.first == KindID) { - P.second = Node; + P.second.reset(Node); return; } } // No replacement, just add it to the list. - Info.push_back(std::make_pair(KindID, Node)); + Info.emplace_back(std::piecewise_construct, std::make_tuple(KindID), + std::make_tuple(Node)); return; } @@ -679,7 +1103,7 @@ void Instruction::setMetadata(unsigned KindID, MDNode *Node) { // Handle removal of an existing value. for (unsigned i = 0, e = Info.size(); i != e; ++i) if (Info[i].first == KindID) { - Info[i] = Info.back(); + Info[i] = std::move(Info.back()); Info.pop_back(); assert(!Info.empty() && "Removing last entry should be handled above"); return; @@ -687,11 +1111,17 @@ void Instruction::setMetadata(unsigned KindID, MDNode *Node) { // Otherwise, removing an entry that doesn't exist on the instruction. } +void Instruction::setAAMetadata(const AAMDNodes &N) { + setMetadata(LLVMContext::MD_tbaa, N.TBAA); + setMetadata(LLVMContext::MD_alias_scope, N.Scope); + setMetadata(LLVMContext::MD_noalias, N.NoAlias); +} + MDNode *Instruction::getMetadataImpl(unsigned KindID) const { // Handle 'dbg' as a special case since it is not stored in the hash table. if (KindID == LLVMContext::MD_dbg) - return DbgLoc.getAsMDNode(getContext()); - + return DbgLoc.getAsMDNode(); + if (!hasMetadataHashEntry()) return nullptr; LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; @@ -703,14 +1133,14 @@ MDNode *Instruction::getMetadataImpl(unsigned KindID) const { return nullptr; } -void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, - MDNode*> > &Result) const { +void Instruction::getAllMetadataImpl( + SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { Result.clear(); // Handle 'dbg' as a special case since it is not stored in the hash table. if (!DbgLoc.isUnknown()) { - Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg, - DbgLoc.getAsMDNode(getContext()))); + Result.push_back( + std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode())); if (!hasMetadataHashEntry()) return; } @@ -721,16 +1151,17 @@ void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, getContext().pImpl->MetadataStore.find(this)->second; assert(!Info.empty() && "Shouldn't have called this"); - Result.append(Info.begin(), Info.end()); + Result.reserve(Result.size() + Info.size()); + for (auto &I : Info) + Result.push_back(std::make_pair(I.first, cast<MDNode>(I.second.get()))); // Sort the resulting array so it is stable. if (Result.size() > 1) array_pod_sort(Result.begin(), Result.end()); } -void Instruction:: -getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned, - MDNode*> > &Result) const { +void Instruction::getAllMetadataOtherThanDebugLocImpl( + SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { Result.clear(); assert(hasMetadataHashEntry() && getContext().pImpl->MetadataStore.count(this) && @@ -738,7 +1169,9 @@ getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned, const LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore.find(this)->second; assert(!Info.empty() && "Shouldn't have called this"); - Result.append(Info.begin(), Info.end()); + Result.reserve(Result.size() + Info.size()); + for (auto &I : Info) + Result.push_back(std::make_pair(I.first, cast<MDNode>(I.second.get()))); // Sort the resulting array so it is stable. if (Result.size() > 1) @@ -752,4 +1185,3 @@ void Instruction::clearMetadataHashEntries() { getContext().pImpl->MetadataStore.erase(this); setHasMetadataHashEntry(false); } - diff --git a/contrib/llvm/lib/IR/MetadataTracking.cpp b/contrib/llvm/lib/IR/MetadataTracking.cpp new file mode 100644 index 0000000..ba97ca0 --- /dev/null +++ b/contrib/llvm/lib/IR/MetadataTracking.cpp @@ -0,0 +1,58 @@ +//===- MetadataTracking.cpp - Implement metadata tracking -----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements Metadata tracking. +// +//===----------------------------------------------------------------------===// + +#include "llvm/IR/MetadataTracking.h" +#include "llvm/IR/Metadata.h" + +using namespace llvm; + +ReplaceableMetadataImpl *ReplaceableMetadataImpl::get(Metadata &MD) { + if (auto *N = dyn_cast<MDNode>(&MD)) { + if (auto *U = dyn_cast<UniquableMDNode>(N)) + return U->ReplaceableUses.get(); + return cast<MDNodeFwdDecl>(N); + } + return dyn_cast<ValueAsMetadata>(&MD); +} + +bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) { + assert(Ref && "Expected live reference"); + assert((Owner || *static_cast<Metadata **>(Ref) == &MD) && + "Reference without owner must be direct"); + if (auto *R = ReplaceableMetadataImpl::get(MD)) { + R->addRef(Ref, Owner); + return true; + } + return false; +} + +void MetadataTracking::untrack(void *Ref, Metadata &MD) { + assert(Ref && "Expected live reference"); + if (auto *R = ReplaceableMetadataImpl::get(MD)) + R->dropRef(Ref); +} + +bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) { + assert(Ref && "Expected live reference"); + assert(New && "Expected live reference"); + assert(Ref != New && "Expected change"); + if (auto *R = ReplaceableMetadataImpl::get(MD)) { + R->moveRef(Ref, New, MD); + return true; + } + return false; +} + +bool MetadataTracking::isReplaceable(const Metadata &MD) { + return ReplaceableMetadataImpl::get(const_cast<Metadata &>(MD)); +} diff --git a/contrib/llvm/lib/IR/Module.cpp b/contrib/llvm/lib/IR/Module.cpp index 58584bd..d32ffcd 100644 --- a/contrib/llvm/lib/IR/Module.cpp +++ b/contrib/llvm/lib/IR/Module.cpp @@ -22,7 +22,7 @@ #include "llvm/IR/GVMaterializer.h" #include "llvm/IR/InstrTypes.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/LeakDetector.h" +#include "llvm/IR/TypeFinder.h" #include "llvm/Support/Dwarf.h" #include "llvm/Support/Path.h" #include "llvm/Support/RandomNumberGenerator.h" @@ -46,7 +46,7 @@ template class llvm::SymbolTableListTraits<GlobalAlias, Module>; // Module::Module(StringRef MID, LLVMContext &C) - : Context(C), Materializer(), ModuleID(MID), RNG(nullptr), DL("") { + : Context(C), Materializer(), ModuleID(MID), DL("") { ValSymTab = new ValueSymbolTable(); NamedMDSymTab = new StringMap<NamedMDNode *>(); Context.addModule(this); @@ -61,9 +61,27 @@ Module::~Module() { NamedMDList.clear(); delete ValSymTab; delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab); - delete RNG; } +RandomNumberGenerator *Module::createRNG(const Pass* P) const { + SmallString<32> Salt(P->getPassName()); + + // This RNG is guaranteed to produce the same random stream only + // when the Module ID and thus the input filename is the same. This + // might be problematic if the input filename extension changes + // (e.g. from .c to .bc or .ll). + // + // We could store this salt in NamedMetadata, but this would make + // the parameter non-const. This would unfortunately make this + // interface unusable by any Machine passes, since they only have a + // const reference to their IR Module. Alternatively we can always + // store salt metadata from the Module constructor. + Salt += sys::path::filename(getModuleIdentifier()); + + return new RandomNumberGenerator(Salt); +} + + /// getNamedValue - Return the first global value in the module with /// the specified name, of arbitrary type. This method returns null /// if a global with the specified name is not found. @@ -259,6 +277,17 @@ void Module::eraseNamedMetadata(NamedMDNode *NMD) { NamedMDList.erase(NMD); } +bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) { + if (ConstantInt *Behavior = mdconst::dyn_extract<ConstantInt>(MD)) { + uint64_t Val = Behavior->getLimitedValue(); + if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) { + MFB = static_cast<ModFlagBehavior>(Val); + return true; + } + } + return false; +} + /// getModuleFlagsMetadata - Returns the module flags in the provided vector. void Module:: getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const { @@ -266,22 +295,22 @@ getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const { if (!ModFlags) return; for (const MDNode *Flag : ModFlags->operands()) { - if (Flag->getNumOperands() >= 3 && isa<ConstantInt>(Flag->getOperand(0)) && + ModFlagBehavior MFB; + if (Flag->getNumOperands() >= 3 && + isValidModFlagBehavior(Flag->getOperand(0), MFB) && isa<MDString>(Flag->getOperand(1))) { // Check the operands of the MDNode before accessing the operands. // The verifier will actually catch these failures. - ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0)); MDString *Key = cast<MDString>(Flag->getOperand(1)); - Value *Val = Flag->getOperand(2); - Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()), - Key, Val)); + Metadata *Val = Flag->getOperand(2); + Flags.push_back(ModuleFlagEntry(MFB, Key, Val)); } } } /// Return the corresponding value if Key appears in module flags, otherwise /// return null. -Value *Module::getModuleFlag(StringRef Key) const { +Metadata *Module::getModuleFlag(StringRef Key) const { SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; getModuleFlagsMetadata(ModuleFlags); for (const ModuleFlagEntry &MFE : ModuleFlags) { @@ -309,14 +338,18 @@ NamedMDNode *Module::getOrInsertModuleFlagsMetadata() { /// metadata. It will create the module-level flags named metadata if it doesn't /// already exist. void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, - Value *Val) { + Metadata *Val) { Type *Int32Ty = Type::getInt32Ty(Context); - Value *Ops[3] = { - ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val - }; + Metadata *Ops[3] = { + ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)), + MDString::get(Context, Key), Val}; getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops)); } void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, + Constant *Val) { + addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val)); +} +void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, uint32_t Val) { Type *Int32Ty = Type::getInt32Ty(Context); addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val)); @@ -324,7 +357,7 @@ void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, void Module::addModuleFlag(MDNode *Node) { assert(Node->getNumOperands() == 3 && "Invalid number of operands for module flag!"); - assert(isa<ConstantInt>(Node->getOperand(0)) && + assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) && isa<MDString>(Node->getOperand(1)) && "Invalid operand types for module flag!"); getOrInsertModuleFlagsMetadata()->addOperand(Node); @@ -358,16 +391,6 @@ const DataLayout *Module::getDataLayout() const { return &DL; } -// We want reproducible builds, but ModuleID may be a full path so we just use -// the filename to salt the RNG (although it is not guaranteed to be unique). -RandomNumberGenerator &Module::getRNG() const { - if (RNG == nullptr) { - StringRef Salt = sys::path::filename(ModuleID); - RNG = new RandomNumberGenerator(Salt); - } - return *RNG; -} - //===----------------------------------------------------------------------===// // Methods to control the materialization of GlobalValues in the Module. // @@ -378,28 +401,17 @@ void Module::setMaterializer(GVMaterializer *GVM) { Materializer.reset(GVM); } -bool Module::isMaterializable(const GlobalValue *GV) const { - if (Materializer) - return Materializer->isMaterializable(GV); - return false; -} - bool Module::isDematerializable(const GlobalValue *GV) const { if (Materializer) return Materializer->isDematerializable(GV); return false; } -bool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) { +std::error_code Module::materialize(GlobalValue *GV) { if (!Materializer) - return false; + return std::error_code(); - std::error_code EC = Materializer->Materialize(GV); - if (!EC) - return false; - if (ErrInfo) - *ErrInfo = EC.message(); - return true; + return Materializer->materialize(GV); } void Module::Dematerialize(GlobalValue *GV) { @@ -413,13 +425,10 @@ std::error_code Module::materializeAll() { return Materializer->MaterializeModule(this); } -std::error_code Module::materializeAllPermanently(bool ReleaseBuffer) { +std::error_code Module::materializeAllPermanently() { if (std::error_code EC = materializeAll()) return EC; - if (ReleaseBuffer) - Materializer->releaseBuffer(); - Materializer.reset(); return std::error_code(); } @@ -428,6 +437,19 @@ std::error_code Module::materializeAllPermanently(bool ReleaseBuffer) { // Other module related stuff. // +std::vector<StructType *> Module::getIdentifiedStructTypes() const { + // If we have a materializer, it is possible that some unread function + // uses a type that is currently not visible to a TypeFinder, so ask + // the materializer which types it created. + if (Materializer) + return Materializer->getIdentifiedStructTypes(); + + std::vector<StructType *> Ret; + TypeFinder SrcStructTypes; + SrcStructTypes.run(*this, true); + Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end()); + return Ret; +} // dropAllReferences() - This function causes all the subelements to "let go" // of all references that they are maintaining. This allows one to 'delete' a @@ -448,27 +470,26 @@ void Module::dropAllReferences() { } unsigned Module::getDwarfVersion() const { - Value *Val = getModuleFlag("Dwarf Version"); + auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version")); if (!Val) return dwarf::DWARF_VERSION; - return cast<ConstantInt>(Val)->getZExtValue(); + return cast<ConstantInt>(Val->getValue())->getZExtValue(); } Comdat *Module::getOrInsertComdat(StringRef Name) { - Comdat C; - StringMapEntry<Comdat> &Entry = - ComdatSymTab.GetOrCreateValue(Name, std::move(C)); + auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first; Entry.second.Name = &Entry; return &Entry.second; } PICLevel::Level Module::getPICLevel() const { - Value *Val = getModuleFlag("PIC Level"); + auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level")); if (Val == NULL) return PICLevel::Default; - return static_cast<PICLevel::Level>(cast<ConstantInt>(Val)->getZExtValue()); + return static_cast<PICLevel::Level>( + cast<ConstantInt>(Val->getValue())->getZExtValue()); } void Module::setPICLevel(PICLevel::Level PL) { diff --git a/contrib/llvm/lib/IR/PassManager.cpp b/contrib/llvm/lib/IR/PassManager.cpp index 2e2a7cb..a5f407c 100644 --- a/contrib/llvm/lib/IR/PassManager.cpp +++ b/contrib/llvm/lib/IR/PassManager.cpp @@ -10,174 +10,13 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/PassManager.h" -#include "llvm/Support/CommandLine.h" -#include "llvm/Support/Debug.h" using namespace llvm; -static cl::opt<bool> -DebugPM("debug-pass-manager", cl::Hidden, - cl::desc("Print pass management debugging information")); - -PreservedAnalyses ModulePassManager::run(Module *M, ModuleAnalysisManager *AM) { - PreservedAnalyses PA = PreservedAnalyses::all(); - - if (DebugPM) - dbgs() << "Starting module pass manager run.\n"; - - for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) { - if (DebugPM) - dbgs() << "Running module pass: " << Passes[Idx]->name() << "\n"; - - PreservedAnalyses PassPA = Passes[Idx]->run(M, AM); - if (AM) - AM->invalidate(M, PassPA); - PA.intersect(std::move(PassPA)); - - M->getContext().yield(); - } - - if (DebugPM) - dbgs() << "Finished module pass manager run.\n"; - - return PA; -} - -ModuleAnalysisManager::ResultConceptT & -ModuleAnalysisManager::getResultImpl(void *PassID, Module *M) { - ModuleAnalysisResultMapT::iterator RI; - bool Inserted; - std::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair( - PassID, std::unique_ptr<detail::AnalysisResultConcept<Module *>>())); - - // If we don't have a cached result for this module, look up the pass and run - // it to produce a result, which we then add to the cache. - if (Inserted) - RI->second = lookupPass(PassID).run(M, this); - - return *RI->second; -} - -ModuleAnalysisManager::ResultConceptT * -ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module *M) const { - ModuleAnalysisResultMapT::const_iterator RI = - ModuleAnalysisResults.find(PassID); - return RI == ModuleAnalysisResults.end() ? nullptr : &*RI->second; -} - -void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) { - ModuleAnalysisResults.erase(PassID); -} - -void ModuleAnalysisManager::invalidateImpl(Module *M, - const PreservedAnalyses &PA) { - // FIXME: This is a total hack based on the fact that erasure doesn't - // invalidate iteration for DenseMap. - for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(), - E = ModuleAnalysisResults.end(); - I != E; ++I) - if (I->second->invalidate(M, PA)) - ModuleAnalysisResults.erase(I); -} - -PreservedAnalyses FunctionPassManager::run(Function *F, - FunctionAnalysisManager *AM) { - PreservedAnalyses PA = PreservedAnalyses::all(); - - if (DebugPM) - dbgs() << "Starting function pass manager run.\n"; - - for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) { - if (DebugPM) - dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n"; - - PreservedAnalyses PassPA = Passes[Idx]->run(F, AM); - if (AM) - AM->invalidate(F, PassPA); - PA.intersect(std::move(PassPA)); - - F->getContext().yield(); - } - - if (DebugPM) - dbgs() << "Finished function pass manager run.\n"; - - return PA; -} - -bool FunctionAnalysisManager::empty() const { - assert(FunctionAnalysisResults.empty() == - FunctionAnalysisResultLists.empty() && - "The storage and index of analysis results disagree on how many there " - "are!"); - return FunctionAnalysisResults.empty(); -} - -void FunctionAnalysisManager::clear() { - FunctionAnalysisResults.clear(); - FunctionAnalysisResultLists.clear(); -} - -FunctionAnalysisManager::ResultConceptT & -FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) { - FunctionAnalysisResultMapT::iterator RI; - bool Inserted; - std::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair( - std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator())); - - // If we don't have a cached result for this function, look up the pass and - // run it to produce a result, which we then add to the cache. - if (Inserted) { - FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F]; - ResultList.emplace_back(PassID, lookupPass(PassID).run(F, this)); - RI->second = std::prev(ResultList.end()); - } - - return *RI->second->second; -} - -FunctionAnalysisManager::ResultConceptT * -FunctionAnalysisManager::getCachedResultImpl(void *PassID, Function *F) const { - FunctionAnalysisResultMapT::const_iterator RI = - FunctionAnalysisResults.find(std::make_pair(PassID, F)); - return RI == FunctionAnalysisResults.end() ? nullptr : &*RI->second->second; -} - -void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) { - FunctionAnalysisResultMapT::iterator RI = - FunctionAnalysisResults.find(std::make_pair(PassID, F)); - if (RI == FunctionAnalysisResults.end()) - return; - - FunctionAnalysisResultLists[F].erase(RI->second); -} - -void FunctionAnalysisManager::invalidateImpl(Function *F, - const PreservedAnalyses &PA) { - // Clear all the invalidated results associated specifically with this - // function. - SmallVector<void *, 8> InvalidatedPassIDs; - FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F]; - for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(), - E = ResultsList.end(); - I != E;) - if (I->second->invalidate(F, PA)) { - InvalidatedPassIDs.push_back(I->first); - I = ResultsList.erase(I); - } else { - ++I; - } - while (!InvalidatedPassIDs.empty()) - FunctionAnalysisResults.erase( - std::make_pair(InvalidatedPassIDs.pop_back_val(), F)); - if (ResultsList.empty()) - FunctionAnalysisResultLists.erase(F); -} - char FunctionAnalysisManagerModuleProxy::PassID; FunctionAnalysisManagerModuleProxy::Result -FunctionAnalysisManagerModuleProxy::run(Module *M) { +FunctionAnalysisManagerModuleProxy::run(Module &M) { assert(FAM->empty() && "Function analyses ran prior to the module proxy!"); return Result(*FAM); } @@ -189,7 +28,7 @@ FunctionAnalysisManagerModuleProxy::Result::~Result() { } bool FunctionAnalysisManagerModuleProxy::Result::invalidate( - Module *M, const PreservedAnalyses &PA) { + Module &M, const PreservedAnalyses &PA) { // If this proxy isn't marked as preserved, then we can't even invalidate // individual function analyses, there may be an invalid set of Function // objects in the cache making it impossible to incrementally preserve them. diff --git a/contrib/llvm/lib/IR/PassRegistry.cpp b/contrib/llvm/lib/IR/PassRegistry.cpp index 91940a9..b879fef 100644 --- a/contrib/llvm/lib/IR/PassRegistry.cpp +++ b/contrib/llvm/lib/IR/PassRegistry.cpp @@ -36,8 +36,7 @@ PassRegistry *PassRegistry::getPassRegistry() { // Accessors // -PassRegistry::~PassRegistry() { -} +PassRegistry::~PassRegistry() {} const PassInfo *PassRegistry::getPassInfo(const void *TI) const { sys::SmartScopedReader<true> Guard(Lock); @@ -58,77 +57,62 @@ const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const { void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) { sys::SmartScopedWriter<true> Guard(Lock); bool Inserted = - PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second; + PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second; assert(Inserted && "Pass registered multiple times!"); (void)Inserted; PassInfoStringMap[PI.getPassArgument()] = &PI; - + // Notify any listeners. - for (std::vector<PassRegistrationListener*>::iterator - I = Listeners.begin(), E = Listeners.end(); I != E; ++I) - (*I)->passRegistered(&PI); - - if (ShouldFree) ToFree.push_back(std::unique_ptr<const PassInfo>(&PI)); -} + for (auto *Listener : Listeners) + Listener->passRegistered(&PI); -void PassRegistry::unregisterPass(const PassInfo &PI) { - sys::SmartScopedWriter<true> Guard(Lock); - MapType::iterator I = PassInfoMap.find(PI.getTypeInfo()); - assert(I != PassInfoMap.end() && "Pass registered but not in map!"); - - // Remove pass from the map. - PassInfoMap.erase(I); - PassInfoStringMap.erase(PI.getPassArgument()); + if (ShouldFree) + ToFree.push_back(std::unique_ptr<const PassInfo>(&PI)); } void PassRegistry::enumerateWith(PassRegistrationListener *L) { sys::SmartScopedReader<true> Guard(Lock); - for (auto I = PassInfoMap.begin(), E = PassInfoMap.end(); I != E; ++I) - L->passEnumerate(I->second); + for (auto PassInfoPair : PassInfoMap) + L->passEnumerate(PassInfoPair.second); } - /// Analysis Group Mechanisms. -void PassRegistry::registerAnalysisGroup(const void *InterfaceID, +void PassRegistry::registerAnalysisGroup(const void *InterfaceID, const void *PassID, - PassInfo& Registeree, - bool isDefault, + PassInfo &Registeree, bool isDefault, bool ShouldFree) { - PassInfo *InterfaceInfo = const_cast<PassInfo*>(getPassInfo(InterfaceID)); + PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID)); if (!InterfaceInfo) { // First reference to Interface, register it now. registerPass(Registeree); InterfaceInfo = &Registeree; } - assert(Registeree.isAnalysisGroup() && + assert(Registeree.isAnalysisGroup() && "Trying to join an analysis group that is a normal pass!"); if (PassID) { - PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID)); + PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID)); assert(ImplementationInfo && "Must register pass before adding to AnalysisGroup!"); sys::SmartScopedWriter<true> Guard(Lock); - + // Make sure we keep track of the fact that the implementation implements // the interface. ImplementationInfo->addInterfaceImplemented(InterfaceInfo); - AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo]; - assert(AGI.Implementations.count(ImplementationInfo) == 0 && - "Cannot add a pass to the same analysis group more than once!"); - AGI.Implementations.insert(ImplementationInfo); if (isDefault) { assert(InterfaceInfo->getNormalCtor() == nullptr && "Default implementation for analysis group already specified!"); - assert(ImplementationInfo->getNormalCtor() && - "Cannot specify pass as default if it does not have a default ctor"); + assert( + ImplementationInfo->getNormalCtor() && + "Cannot specify pass as default if it does not have a default ctor"); InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); InterfaceInfo->setTargetMachineCtor( ImplementationInfo->getTargetMachineCtor()); } } - + if (ShouldFree) ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree)); } @@ -140,7 +124,7 @@ void PassRegistry::addRegistrationListener(PassRegistrationListener *L) { void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) { sys::SmartScopedWriter<true> Guard(Lock); - + auto I = std::find(Listeners.begin(), Listeners.end(), L); Listeners.erase(I); } diff --git a/contrib/llvm/lib/IR/Statepoint.cpp b/contrib/llvm/lib/IR/Statepoint.cpp new file mode 100644 index 0000000..270c016 --- /dev/null +++ b/contrib/llvm/lib/IR/Statepoint.cpp @@ -0,0 +1,61 @@ +//===-- IR/Statepoint.cpp -- gc.statepoint utilities --- -----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// +//===----------------------------------------------------------------------===// + +#include "llvm/IR/Function.h" +#include "llvm/IR/Constant.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/Statepoint.h" +#include "llvm/Support/CommandLine.h" + +using namespace std; +using namespace llvm; + +bool llvm::isStatepoint(const ImmutableCallSite &CS) { + const Function *F = CS.getCalledFunction(); + return (F && F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint); +} +bool llvm::isStatepoint(const Instruction *inst) { + if (isa<InvokeInst>(inst) || isa<CallInst>(inst)) { + ImmutableCallSite CS(inst); + return isStatepoint(CS); + } + return false; +} +bool llvm::isStatepoint(const Instruction &inst) { + return isStatepoint(&inst); +} + +bool llvm::isGCRelocate(const ImmutableCallSite &CS) { + return isGCRelocate(CS.getInstruction()); +} +bool llvm::isGCRelocate(const Instruction *inst) { + if (const CallInst *call = dyn_cast<CallInst>(inst)) { + if (const Function *F = call->getCalledFunction()) { + return F->getIntrinsicID() == Intrinsic::experimental_gc_relocate; + } + } + return false; +} + +bool llvm::isGCResult(const ImmutableCallSite &CS) { + return isGCResult(CS.getInstruction()); +} +bool llvm::isGCResult(const Instruction *inst) { + if (const CallInst *call = dyn_cast<CallInst>(inst)) { + if (Function *F = call->getCalledFunction()) { + return (F->getIntrinsicID() == Intrinsic::experimental_gc_result_int || + F->getIntrinsicID() == Intrinsic::experimental_gc_result_float || + F->getIntrinsicID() == Intrinsic::experimental_gc_result_ptr); + } + } + return false; +} diff --git a/contrib/llvm/lib/IR/SymbolTableListTraitsImpl.h b/contrib/llvm/lib/IR/SymbolTableListTraitsImpl.h index 8302597..a18f982 100644 --- a/contrib/llvm/lib/IR/SymbolTableListTraitsImpl.h +++ b/contrib/llvm/lib/IR/SymbolTableListTraitsImpl.h @@ -13,8 +13,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_SYMBOLTABLELISTTRAITS_IMPL_H -#define LLVM_SYMBOLTABLELISTTRAITS_IMPL_H +#ifndef LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H +#define LLVM_LIB_IR_SYMBOLTABLELISTTRAITSIMPL_H #include "llvm/IR/SymbolTableListTraits.h" #include "llvm/IR/ValueSymbolTable.h" diff --git a/contrib/llvm/lib/IR/Type.cpp b/contrib/llvm/lib/IR/Type.cpp index 1efde47..889705e 100644 --- a/contrib/llvm/lib/IR/Type.cpp +++ b/contrib/llvm/lib/IR/Type.cpp @@ -89,9 +89,13 @@ bool Type::canLosslesslyBitCastTo(Type *Ty) const { // At this point we have only various mismatches of the first class types // remaining and ptr->ptr. Just select the lossless conversions. Everything - // else is not lossless. - if (this->isPointerTy()) - return Ty->isPointerTy(); + // else is not lossless. Conservatively assume we can't losslessly convert + // between pointers with different address spaces. + if (const PointerType *PTy = dyn_cast<PointerType>(this)) { + if (const PointerType *OtherPTy = dyn_cast<PointerType>(Ty)) + return PTy->getAddressSpace() == OtherPTy->getAddressSpace(); + return false; + } return false; // Other types have no identity values } @@ -155,7 +159,7 @@ int Type::getFPMantissaWidth() const { /// isSizedDerivedType - Derived types like structures and arrays are sized /// iff all of the members of the type are sized as well. Since asking for /// their size is relatively uncommon, move this operation out of line. -bool Type::isSizedDerivedType(SmallPtrSet<const Type*, 4> *Visited) const { +bool Type::isSizedDerivedType(SmallPtrSetImpl<const Type*> *Visited) const { if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) return ATy->getElementType()->isSized(Visited); @@ -356,8 +360,7 @@ FunctionType *FunctionType::get(Type *ReturnType, ArrayRef<Type*> Params, bool isVarArg) { LLVMContextImpl *pImpl = ReturnType->getContext().pImpl; FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg); - LLVMContextImpl::FunctionTypeMap::iterator I = - pImpl->FunctionTypes.find_as(Key); + auto I = pImpl->FunctionTypes.find_as(Key); FunctionType *FT; if (I == pImpl->FunctionTypes.end()) { @@ -365,9 +368,9 @@ FunctionType *FunctionType::get(Type *ReturnType, Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1), AlignOf<FunctionType>::Alignment); new (FT) FunctionType(ReturnType, Params, isVarArg); - pImpl->FunctionTypes[FT] = true; + pImpl->FunctionTypes.insert(FT); } else { - FT = I->first; + FT = *I; } return FT; @@ -400,8 +403,7 @@ StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes, bool isPacked) { LLVMContextImpl *pImpl = Context.pImpl; AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked); - LLVMContextImpl::StructTypeMap::iterator I = - pImpl->AnonStructTypes.find_as(Key); + auto I = pImpl->AnonStructTypes.find_as(Key); StructType *ST; if (I == pImpl->AnonStructTypes.end()) { @@ -409,9 +411,9 @@ StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes, ST = new (Context.pImpl->TypeAllocator) StructType(Context); ST->setSubclassData(SCDB_IsLiteral); // Literal struct. ST->setBody(ETypes, isPacked); - Context.pImpl->AnonStructTypes[ST] = true; + Context.pImpl->AnonStructTypes.insert(ST); } else { - ST = I->first; + ST = *I; } return ST; @@ -454,10 +456,11 @@ void StructType::setName(StringRef Name) { } // Look up the entry for the name. - EntryTy *Entry = &getContext().pImpl->NamedStructTypes.GetOrCreateValue(Name); - + auto IterBool = + getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this)); + // While we have a name collision, try a random rename. - if (Entry->getValue()) { + if (!IterBool.second) { SmallString<64> TempStr(Name); TempStr.push_back('.'); raw_svector_ostream TmpStream(TempStr); @@ -467,19 +470,16 @@ void StructType::setName(StringRef Name) { TempStr.resize(NameSize + 1); TmpStream.resync(); TmpStream << getContext().pImpl->NamedStructTypesUniqueID++; - - Entry = &getContext().pImpl-> - NamedStructTypes.GetOrCreateValue(TmpStream.str()); - } while (Entry->getValue()); - } - // Okay, we found an entry that isn't used. It's us! - Entry->setValue(this); + IterBool = getContext().pImpl->NamedStructTypes.insert( + std::make_pair(TmpStream.str(), this)); + } while (!IterBool.second); + } // Delete the old string data. if (SymbolTableEntry) ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator()); - SymbolTableEntry = Entry; + SymbolTableEntry = &*IterBool.first; } //===----------------------------------------------------------------------===// @@ -506,7 +506,9 @@ StructType *StructType::get(Type *type, ...) { StructFields.push_back(type); type = va_arg(ap, llvm::Type*); } - return llvm::StructType::get(Ctx, StructFields); + auto *Ret = llvm::StructType::get(Ctx, StructFields); + va_end(ap); + return Ret; } StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements, @@ -547,16 +549,18 @@ StructType *StructType::create(StringRef Name, Type *type, ...) { StructFields.push_back(type); type = va_arg(ap, llvm::Type*); } - return llvm::StructType::create(Ctx, StructFields, Name); + auto *Ret = llvm::StructType::create(Ctx, StructFields, Name); + va_end(ap); + return Ret; } -bool StructType::isSized(SmallPtrSet<const Type*, 4> *Visited) const { +bool StructType::isSized(SmallPtrSetImpl<const Type*> *Visited) const { if ((getSubclassData() & SCDB_IsSized) != 0) return true; if (isOpaque()) return false; - if (Visited && !Visited->insert(this)) + if (Visited && !Visited->insert(this).second) return false; // Okay, our struct is sized if all of the elements are, but if one of the @@ -591,6 +595,7 @@ void StructType::setBody(Type *type, ...) { type = va_arg(ap, llvm::Type*); } setBody(StructFields); + va_end(ap); } bool StructType::isValidElementType(Type *ElemTy) { diff --git a/contrib/llvm/lib/IR/TypeFinder.cpp b/contrib/llvm/lib/IR/TypeFinder.cpp index 689b903..e2fb8f8 100644 --- a/contrib/llvm/lib/IR/TypeFinder.cpp +++ b/contrib/llvm/lib/IR/TypeFinder.cpp @@ -40,13 +40,16 @@ void TypeFinder::run(const Module &M, bool onlyNamed) { } // Get types from functions. - SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst; + SmallVector<std::pair<unsigned, MDNode *>, 4> MDForInst; for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) { incorporateType(FI->getType()); if (FI->hasPrefixData()) incorporateValue(FI->getPrefixData()); + if (FI->hasPrologueData()) + incorporateValue(FI->getPrologueData()); + // First incorporate the arguments. for (Function::const_arg_iterator AI = FI->arg_begin(), AE = FI->arg_end(); AI != AE; ++AI) @@ -122,8 +125,13 @@ void TypeFinder::incorporateType(Type *Ty) { /// other ways. GlobalValues, basic blocks, instructions, and inst operands are /// all explicitly enumerated. void TypeFinder::incorporateValue(const Value *V) { - if (const MDNode *M = dyn_cast<MDNode>(V)) - return incorporateMDNode(M); + if (const auto *M = dyn_cast<MetadataAsValue>(V)) { + if (const auto *N = dyn_cast<MDNode>(M->getMetadata())) + return incorporateMDNode(N); + if (const auto *MDV = dyn_cast<ValueAsMetadata>(M->getMetadata())) + return incorporateValue(MDV->getValue()); + return; + } if (!isa<Constant>(V) || isa<GlobalValue>(V)) return; @@ -149,11 +157,21 @@ void TypeFinder::incorporateValue(const Value *V) { /// find types hiding within. void TypeFinder::incorporateMDNode(const MDNode *V) { // Already visited? - if (!VisitedConstants.insert(V).second) + if (!VisitedMetadata.insert(V).second) return; // Look in operands for types. - for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) - if (Value *Op = V->getOperand(i)) - incorporateValue(Op); + for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) { + Metadata *Op = V->getOperand(i); + if (!Op) + continue; + if (auto *N = dyn_cast<MDNode>(Op)) { + incorporateMDNode(N); + continue; + } + if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) { + incorporateValue(C->getValue()); + continue; + } + } } diff --git a/contrib/llvm/lib/IR/Use.cpp b/contrib/llvm/lib/IR/Use.cpp index 047861c..cae845d 100644 --- a/contrib/llvm/lib/IR/Use.cpp +++ b/contrib/llvm/lib/IR/Use.cpp @@ -52,7 +52,7 @@ unsigned Use::getOperandNo() const { // Sets up the waymarking algorithm's tags for a series of Uses. See the // algorithm details here: // -// http://www.llvm.org/docs/ProgrammersManual.html#UserLayout +// http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm // Use *Use::initTags(Use *const Start, Use *Stop) { ptrdiff_t Done = 0; diff --git a/contrib/llvm/lib/IR/UseListOrder.cpp b/contrib/llvm/lib/IR/UseListOrder.cpp new file mode 100644 index 0000000..d064e67 --- /dev/null +++ b/contrib/llvm/lib/IR/UseListOrder.cpp @@ -0,0 +1,43 @@ +//===- UseListOrder.cpp - Implement Use List Order ------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Implement structures and command-line options for preserving use-list order. +// +//===----------------------------------------------------------------------===// + +#include "llvm/IR/UseListOrder.h" +#include "llvm/Support/CommandLine.h" + +using namespace llvm; + +static cl::opt<bool> PreserveBitcodeUseListOrder( + "preserve-bc-use-list-order", + cl::desc("Experimental support to preserve bitcode use-list order."), + cl::init(false), cl::Hidden); + +static cl::opt<bool> PreserveAssemblyUseListOrder( + "preserve-ll-use-list-order", + cl::desc("Experimental support to preserve assembly use-list order."), + cl::init(false), cl::Hidden); + +bool llvm::shouldPreserveBitcodeUseListOrder() { + return PreserveBitcodeUseListOrder; +} + +bool llvm::shouldPreserveAssemblyUseListOrder() { + return PreserveAssemblyUseListOrder; +} + +void llvm::setPreserveBitcodeUseListOrder(bool ShouldPreserve) { + PreserveBitcodeUseListOrder = ShouldPreserve; +} + +void llvm::setPreserveAssemblyUseListOrder(bool ShouldPreserve) { + PreserveAssemblyUseListOrder = ShouldPreserve; +} diff --git a/contrib/llvm/lib/IR/User.cpp b/contrib/llvm/lib/IR/User.cpp index 9406828..ee83eacf 100644 --- a/contrib/llvm/lib/IR/User.cpp +++ b/contrib/llvm/lib/IR/User.cpp @@ -20,9 +20,6 @@ namespace llvm { void User::anchor() {} -// replaceUsesOfWith - Replaces all references to the "From" definition with -// references to the "To" definition. -// void User::replaceUsesOfWith(Value *From, Value *To) { if (From == To) return; // Duh what? diff --git a/contrib/llvm/lib/IR/Value.cpp b/contrib/llvm/lib/IR/Value.cpp index 1ab2183..5f7e258 100644 --- a/contrib/llvm/lib/IR/Value.cpp +++ b/contrib/llvm/lib/IR/Value.cpp @@ -23,7 +23,6 @@ #include "llvm/IR/GetElementPtrTypeIterator.h" #include "llvm/IR/InstrTypes.h" #include "llvm/IR/Instructions.h" -#include "llvm/IR/LeakDetector.h" #include "llvm/IR/Module.h" #include "llvm/IR/Operator.h" #include "llvm/IR/ValueHandle.h" @@ -44,8 +43,8 @@ static inline Type *checkType(Type *Ty) { } Value::Value(Type *ty, unsigned scid) - : VTy(checkType(ty)), UseList(nullptr), Name(nullptr), SubclassID(scid), - HasValueHandle(0), SubclassOptionalData(0), SubclassData(0) { + : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), HasValueHandle(0), + SubclassOptionalData(0), SubclassData(0), NumOperands(0) { // FIXME: Why isn't this in the subclass gunk?? // Note, we cannot call isa<CallInst> before the CallInst has been // constructed. @@ -62,6 +61,8 @@ Value::~Value() { // Notify all ValueHandles (if present) that this value is going away. if (HasValueHandle) ValueHandleBase::ValueIsDeleted(this); + if (isUsedByMetadata()) + ValueAsMetadata::handleDeletion(this); #ifndef NDEBUG // Only in -g mode... // Check to make sure that there are no uses of this value that are still @@ -81,15 +82,16 @@ Value::~Value() { // If this value is named, destroy the name. This should not be in a symtab // at this point. - if (Name && SubclassID != MDStringVal) - Name->Destroy(); + destroyValueName(); +} - // There should be no uses of this object anymore, remove it. - LeakDetector::removeGarbageObject(this); +void Value::destroyValueName() { + ValueName *Name = getValueName(); + if (Name) + Name->Destroy(); + setValueName(nullptr); } -/// hasNUses - Return true if this Value has exactly N users. -/// bool Value::hasNUses(unsigned N) const { const_use_iterator UI = use_begin(), E = use_end(); @@ -98,9 +100,6 @@ bool Value::hasNUses(unsigned N) const { return UI == E; } -/// hasNUsesOrMore - Return true if this value has N users or more. This is -/// logically equivalent to getNumUses() >= N. -/// bool Value::hasNUsesOrMore(unsigned N) const { const_use_iterator UI = use_begin(), E = use_end(); @@ -110,8 +109,6 @@ bool Value::hasNUsesOrMore(unsigned N) const { return true; } -/// isUsedInBasicBlock - Return true if this value is used in the specified -/// basic block. bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { // This can be computed either by scanning the instructions in BB, or by // scanning the use list of this Value. Both lists can be very long, but @@ -133,10 +130,6 @@ bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { return false; } - -/// getNumUses - This method computes the number of uses of this Value. This -/// is a linear time operation. Use hasOneUse or hasNUses to check for specific -/// values. unsigned Value::getNumUses() const { return (unsigned)std::distance(use_begin(), use_end()); } @@ -156,9 +149,7 @@ static bool getSymTab(Value *V, ValueSymbolTable *&ST) { } else if (Argument *A = dyn_cast<Argument>(V)) { if (Function *P = A->getParent()) ST = &P->getValueSymbolTable(); - } else if (isa<MDString>(V)) - return true; - else { + } else { assert(isa<Constant>(V) && "Unknown value type!"); return true; // no name is setable for this. } @@ -169,14 +160,12 @@ StringRef Value::getName() const { // Make sure the empty string is still a C string. For historical reasons, // some clients want to call .data() on the result and expect it to be null // terminated. - if (!Name) return StringRef("", 0); - return Name->getKey(); + if (!getValueName()) + return StringRef("", 0); + return getValueName()->getKey(); } void Value::setName(const Twine &NewName) { - assert(SubclassID != MDStringVal && - "Cannot set the name of MDString with this method!"); - // Fast path for common IRBuilder case of setName("") when there is no name. if (NewName.isTriviallyEmpty() && !hasName()) return; @@ -203,20 +192,17 @@ void Value::setName(const Twine &NewName) { if (!ST) { // No symbol table to update? Just do the change. if (NameRef.empty()) { // Free the name for this value. - Name->Destroy(); - Name = nullptr; + destroyValueName(); return; } - if (Name) - Name->Destroy(); - // NOTE: Could optimize for the case the name is shrinking to not deallocate // then reallocated. + destroyValueName(); // Create the new name. - Name = ValueName::Create(NameRef); - Name->setValue(this); + setValueName(ValueName::Create(NameRef)); + getValueName()->setValue(this); return; } @@ -224,24 +210,18 @@ void Value::setName(const Twine &NewName) { // then reallocated. if (hasName()) { // Remove old name. - ST->removeValueName(Name); - Name->Destroy(); - Name = nullptr; + ST->removeValueName(getValueName()); + destroyValueName(); if (NameRef.empty()) return; } // Name is changing to something new. - Name = ST->createValueName(NameRef, this); + setValueName(ST->createValueName(NameRef, this)); } - -/// takeName - transfer the name from V to this value, setting V's name to -/// empty. It is an error to call V->takeName(V). void Value::takeName(Value *V) { - assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!"); - ValueSymbolTable *ST = nullptr; // If this value has a name, drop it. if (hasName()) { @@ -255,9 +235,8 @@ void Value::takeName(Value *V) { // Remove old name. if (ST) - ST->removeValueName(Name); - Name->Destroy(); - Name = nullptr; + ST->removeValueName(getValueName()); + destroyValueName(); } // Now we know that this has no name. @@ -283,9 +262,9 @@ void Value::takeName(Value *V) { // This works even if both values have no symtab yet. if (ST == VST) { // Take the name! - Name = V->Name; - V->Name = nullptr; - Name->setValue(this); + setValueName(V->getValueName()); + V->setValueName(nullptr); + getValueName()->setValue(this); return; } @@ -293,19 +272,19 @@ void Value::takeName(Value *V) { // then reinsert it into ST. if (VST) - VST->removeValueName(V->Name); - Name = V->Name; - V->Name = nullptr; - Name->setValue(this); + VST->removeValueName(V->getValueName()); + setValueName(V->getValueName()); + V->setValueName(nullptr); + getValueName()->setValue(this); if (ST) ST->reinsertValue(this); } #ifndef NDEBUG -static bool contains(SmallPtrSet<ConstantExpr *, 4> &Cache, ConstantExpr *Expr, +static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr, Constant *C) { - if (!Cache.insert(Expr)) + if (!Cache.insert(Expr).second) return false; for (auto &O : Expr->operands()) { @@ -347,6 +326,8 @@ void Value::replaceAllUsesWith(Value *New) { // Notify all ValueHandles (if present) that this value is going away. if (HasValueHandle) ValueHandleBase::ValueIsRAUWd(this, New); + if (isUsedByMetadata()) + ValueAsMetadata::handleRAUW(this, New); while (!use_empty()) { Use &U = *UseList; @@ -366,6 +347,28 @@ void Value::replaceAllUsesWith(Value *New) { BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New)); } +// Like replaceAllUsesWith except it does not handle constants or basic blocks. +// This routine leaves uses within BB. +void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) { + assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!"); + assert(!contains(New, this) && + "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!"); + assert(New->getType() == getType() && + "replaceUses of value with new value of different type!"); + assert(BB && "Basic block that may contain a use of 'New' must be defined\n"); + + use_iterator UI = use_begin(), E = use_end(); + for (; UI != E;) { + Use &U = *UI; + ++UI; + auto *Usr = dyn_cast<Instruction>(U.getUser()); + if (Usr && Usr->getParent() == BB) + continue; + U.set(New); + } + return; +} + namespace { // Various metrics for how much to strip off of pointers. enum PointerStripKind { @@ -414,7 +417,7 @@ static Value *stripPointerCastsAndOffsets(Value *V) { return V; } assert(V->getType()->isPointerTy() && "Unexpected operand type!"); - } while (Visited.insert(V)); + } while (Visited.insert(V).second); return V; } @@ -464,7 +467,7 @@ Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, return V; } assert(V->getType()->isPointerTy() && "Unexpected operand type!"); - } while (Visited.insert(V)); + } while (Visited.insert(V).second); return V; } @@ -473,10 +476,12 @@ Value *Value::stripInBoundsOffsets() { return stripPointerCastsAndOffsets<PSK_InBounds>(this); } -/// isDereferenceablePointer - Test if this value is always a pointer to -/// allocated and suitably aligned memory for a simple load or store. +/// \brief Check if Value is always a dereferenceable pointer. +/// +/// Test if V is always a pointer to allocated and suitably aligned memory for +/// a simple load or store. static bool isDereferenceablePointer(const Value *V, const DataLayout *DL, - SmallPtrSet<const Value *, 32> &Visited) { + SmallPtrSetImpl<const Value *> &Visited) { // Note that it is not safe to speculate into a malloc'd region because // malloc may return null. @@ -533,7 +538,7 @@ static bool isDereferenceablePointer(const Value *V, const DataLayout *DL, // For GEPs, determine if the indexing lands within the allocated object. if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { // Conservatively require that the base pointer be fully dereferenceable. - if (!Visited.insert(GEP->getOperand(0))) + if (!Visited.insert(GEP->getOperand(0)).second) return false; if (!isDereferenceablePointer(GEP->getOperand(0), DL, Visited)) return false; @@ -572,8 +577,6 @@ static bool isDereferenceablePointer(const Value *V, const DataLayout *DL, return false; } -/// isDereferenceablePointer - Test if this value is always a pointer to -/// allocated and suitably aligned memory for a simple load or store. bool Value::isDereferenceablePointer(const DataLayout *DL) const { // When dereferenceability information is provided by a dereferenceable // attribute, we know exactly how many bytes are dereferenceable. If we can @@ -600,10 +603,6 @@ bool Value::isDereferenceablePointer(const DataLayout *DL) const { return ::isDereferenceablePointer(this, DL, Visited); } -/// DoPHITranslation - If this value is a PHI node with CurBB as its parent, -/// return the value in the PHI node corresponding to PredBB. If not, return -/// ourself. This is useful if you want to know the value something has in a -/// predecessor block. Value *Value::DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) { PHINode *PN = dyn_cast<PHINode>(this); @@ -614,12 +613,29 @@ Value *Value::DoPHITranslation(const BasicBlock *CurBB, LLVMContext &Value::getContext() const { return VTy->getContext(); } +void Value::reverseUseList() { + if (!UseList || !UseList->Next) + // No need to reverse 0 or 1 uses. + return; + + Use *Head = UseList; + Use *Current = UseList->Next; + Head->Next = nullptr; + while (Current) { + Use *Next = Current->Next; + Current->Next = Head; + Head->setPrev(&Current->Next); + Head = Current; + Current = Next; + } + UseList = Head; + Head->setPrev(&UseList); +} + //===----------------------------------------------------------------------===// // ValueHandleBase Class //===----------------------------------------------------------------------===// -/// AddToExistingUseList - Add this ValueHandle to the use list for VP, where -/// List is known to point into the existing use list. void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { assert(List && "Handle list is null?"); @@ -629,7 +645,7 @@ void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { setPrevPtr(List); if (Next) { Next->setPrevPtr(&Next); - assert(VP.getPointer() == Next->VP.getPointer() && "Added to wrong list?"); + assert(V == Next->V && "Added to wrong list?"); } } @@ -643,16 +659,15 @@ void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { Next->setPrevPtr(&Next); } -/// AddToUseList - Add this ValueHandle to the use list for VP. void ValueHandleBase::AddToUseList() { - assert(VP.getPointer() && "Null pointer doesn't have a use list!"); + assert(V && "Null pointer doesn't have a use list!"); - LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl; + LLVMContextImpl *pImpl = V->getContext().pImpl; - if (VP.getPointer()->HasValueHandle) { + if (V->HasValueHandle) { // If this value already has a ValueHandle, then it must be in the // ValueHandles map already. - ValueHandleBase *&Entry = pImpl->ValueHandles[VP.getPointer()]; + ValueHandleBase *&Entry = pImpl->ValueHandles[V]; assert(Entry && "Value doesn't have any handles?"); AddToExistingUseList(&Entry); return; @@ -666,10 +681,10 @@ void ValueHandleBase::AddToUseList() { DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); - ValueHandleBase *&Entry = Handles[VP.getPointer()]; + ValueHandleBase *&Entry = Handles[V]; assert(!Entry && "Value really did already have handles?"); AddToExistingUseList(&Entry); - VP.getPointer()->HasValueHandle = true; + V->HasValueHandle = true; // If reallocation didn't happen or if this was the first insertion, don't // walk the table. @@ -681,15 +696,14 @@ void ValueHandleBase::AddToUseList() { // Okay, reallocation did happen. Fix the Prev Pointers. for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), E = Handles.end(); I != E; ++I) { - assert(I->second && I->first == I->second->VP.getPointer() && + assert(I->second && I->first == I->second->V && "List invariant broken!"); I->second->setPrevPtr(&I->second); } } -/// RemoveFromUseList - Remove this ValueHandle from its current use list. void ValueHandleBase::RemoveFromUseList() { - assert(VP.getPointer() && VP.getPointer()->HasValueHandle && + assert(V && V->HasValueHandle && "Pointer doesn't have a use list!"); // Unlink this from its use list. @@ -706,11 +720,11 @@ void ValueHandleBase::RemoveFromUseList() { // If the Next pointer was null, then it is possible that this was the last // ValueHandle watching VP. If so, delete its entry from the ValueHandles // map. - LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl; + LLVMContextImpl *pImpl = V->getContext().pImpl; DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; if (Handles.isPointerIntoBucketsArray(PrevPtr)) { - Handles.erase(VP.getPointer()); - VP.getPointer()->HasValueHandle = false; + Handles.erase(V); + V->HasValueHandle = false; } } @@ -775,6 +789,8 @@ void ValueHandleBase::ValueIsDeleted(Value *V) { void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); assert(Old != New && "Changing value into itself!"); + assert(Old->getType() == New->getType() && + "replaceAllUses of value with new value of different type!"); // Get the linked list base, which is guaranteed to exist since the // HasValueHandle flag is set. diff --git a/contrib/llvm/lib/IR/ValueSymbolTable.cpp b/contrib/llvm/lib/IR/ValueSymbolTable.cpp index e9e979a..4f078f0 100644 --- a/contrib/llvm/lib/IR/ValueSymbolTable.cpp +++ b/contrib/llvm/lib/IR/ValueSymbolTable.cpp @@ -38,8 +38,8 @@ void ValueSymbolTable::reinsertValue(Value* V) { assert(V->hasName() && "Can't insert nameless Value into symbol table"); // Try inserting the name, assuming it won't conflict. - if (vmap.insert(V->Name)) { - //DEBUG(dbgs() << " Inserted value: " << V->Name << ": " << *V << "\n"); + if (vmap.insert(V->getValueName())) { + //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n"); return; } @@ -47,8 +47,8 @@ void ValueSymbolTable::reinsertValue(Value* V) { SmallString<256> UniqueName(V->getName().begin(), V->getName().end()); // The name is too already used, just free it so we can allocate a new name. - V->Name->Destroy(); - + V->getValueName()->Destroy(); + unsigned BaseSize = UniqueName.size(); while (1) { // Trim any suffix off and append the next number. @@ -56,11 +56,10 @@ void ValueSymbolTable::reinsertValue(Value* V) { raw_svector_ostream(UniqueName) << ++LastUnique; // Try insert the vmap entry with this suffix. - ValueName &NewName = vmap.GetOrCreateValue(UniqueName); - if (!NewName.getValue()) { + auto IterBool = vmap.insert(std::make_pair(UniqueName, V)); + if (IterBool.second) { // Newly inserted name. Success! - NewName.setValue(V); - V->Name = &NewName; + V->setValueName(&*IterBool.first); //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n"); return; } @@ -78,12 +77,11 @@ void ValueSymbolTable::removeValueName(ValueName *V) { /// auto-renames the name and returns that instead. ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) { // In the common case, the name is not already in the symbol table. - ValueName &Entry = vmap.GetOrCreateValue(Name); - if (!Entry.getValue()) { - Entry.setValue(V); + auto IterBool = vmap.insert(std::make_pair(Name, V)); + if (IterBool.second) { //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": " // << *V << "\n"); - return &Entry; + return &*IterBool.first; } // Otherwise, there is a naming conflict. Rename this value. @@ -95,12 +93,11 @@ ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) { raw_svector_ostream(UniqueName) << ++LastUnique; // Try insert the vmap entry with this suffix. - ValueName &NewName = vmap.GetOrCreateValue(UniqueName); - if (!NewName.getValue()) { - // Newly inserted name. Success! - NewName.setValue(V); - //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n"); - return &NewName; + auto IterBool = vmap.insert(std::make_pair(UniqueName, V)); + if (IterBool.second) { + // DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << + // "\n"); + return &*IterBool.first; } } } diff --git a/contrib/llvm/lib/IR/Verifier.cpp b/contrib/llvm/lib/IR/Verifier.cpp index 9cf911b..4bf2d1a 100644 --- a/contrib/llvm/lib/IR/Verifier.cpp +++ b/contrib/llvm/lib/IR/Verifier.cpp @@ -68,6 +68,7 @@ #include "llvm/IR/Metadata.h" #include "llvm/IR/Module.h" #include "llvm/IR/PassManager.h" +#include "llvm/IR/Statepoint.h" #include "llvm/Pass.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" @@ -101,6 +102,13 @@ struct VerifierSupport { } } + void WriteMetadata(const Metadata *MD) { + if (!MD) + return; + MD->printAsOperand(OS, true, M); + OS << '\n'; + } + void WriteType(Type *T) { if (!T) return; @@ -127,6 +135,24 @@ struct VerifierSupport { Broken = true; } + void CheckFailed(const Twine &Message, const Metadata *V1, const Metadata *V2, + const Metadata *V3 = nullptr, const Metadata *V4 = nullptr) { + OS << Message.str() << "\n"; + WriteMetadata(V1); + WriteMetadata(V2); + WriteMetadata(V3); + WriteMetadata(V4); + Broken = true; + } + + void CheckFailed(const Twine &Message, const Metadata *V1, + const Value *V2 = nullptr) { + OS << Message.str() << "\n"; + WriteMetadata(V1); + WriteValue(V2); + Broken = true; + } + void CheckFailed(const Twine &Message, const Value *V1, Type *T2, const Value *V3 = nullptr) { OS << Message.str() << "\n"; @@ -155,7 +181,6 @@ class Verifier : public InstVisitor<Verifier>, VerifierSupport { friend class InstVisitor<Verifier>; LLVMContext *Context; - const DataLayout *DL; DominatorTree DT; /// \brief When verifying a basic block, keep track of all of the @@ -166,17 +191,21 @@ class Verifier : public InstVisitor<Verifier>, VerifierSupport { SmallPtrSet<Instruction *, 16> InstsInThisBlock; /// \brief Keep track of the metadata nodes that have been checked already. - SmallPtrSet<MDNode *, 32> MDNodes; + SmallPtrSet<Metadata *, 32> MDNodes; /// \brief The personality function referenced by the LandingPadInsts. /// All LandingPadInsts within the same function must use the same /// personality function. const Value *PersonalityFn; + /// \brief Whether we've seen a call to @llvm.frameallocate in this function + /// already. + bool SawFrameAllocate; + public: explicit Verifier(raw_ostream &OS = dbgs()) - : VerifierSupport(OS), Context(nullptr), DL(nullptr), - PersonalityFn(nullptr) {} + : VerifierSupport(OS), Context(nullptr), PersonalityFn(nullptr), + SawFrameAllocate(false) {} bool verify(const Function &F) { M = F.getParent(); @@ -211,6 +240,7 @@ public: visit(const_cast<Function &>(F)); InstsInThisBlock.clear(); PersonalityFn = nullptr; + SawFrameAllocate = false; return !Broken; } @@ -257,10 +287,12 @@ private: void visitGlobalVariable(const GlobalVariable &GV); void visitGlobalAlias(const GlobalAlias &GA); void visitAliaseeSubExpr(const GlobalAlias &A, const Constant &C); - void visitAliaseeSubExpr(SmallPtrSet<const GlobalAlias *, 4> &Visited, + void visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias *> &Visited, const GlobalAlias &A, const Constant &C); void visitNamedMDNode(const NamedMDNode &NMD); - void visitMDNode(MDNode &MD, Function *F); + void visitMDNode(MDNode &MD); + void visitMetadataAsValue(MetadataAsValue &MD, Function *F); + void visitValueAsMetadata(ValueAsMetadata &MD, Function *F); void visitComdat(const Comdat &C); void visitModuleIdents(const Module &M); void visitModuleFlags(const Module &M); @@ -269,6 +301,8 @@ private: SmallVectorImpl<const MDNode *> &Requirements); void visitFunction(const Function &F); void visitBasicBlock(BasicBlock &BB); + void visitRangeMetadata(Instruction& I, MDNode* Range, Type* Ty); + // InstVisitor overrides... using InstVisitor<Verifier>::visit; @@ -335,7 +369,6 @@ private: void VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs, const Value *V); - void VerifyBitcastType(const Value *V, Type *DestTy, Type *SrcTy); void VerifyConstantExprBitcastType(const ConstantExpr *CE); }; class DebugInfoVerifier : public VerifierSupport { @@ -375,8 +408,8 @@ void Verifier::visit(Instruction &I) { void Verifier::visitGlobalValue(const GlobalValue &GV) { - Assert1(!GV.isDeclaration() || GV.isMaterializable() || - GV.hasExternalLinkage() || GV.hasExternalWeakLinkage(), + Assert1(!GV.isDeclaration() || GV.hasExternalLinkage() || + GV.hasExternalWeakLinkage(), "Global is external, but doesn't have external or weak linkage!", &GV); @@ -478,7 +511,7 @@ void Verifier::visitGlobalVariable(const GlobalVariable &GV) { while (!WorkStack.empty()) { const Value *V = WorkStack.pop_back_val(); - if (!Visited.insert(V)) + if (!Visited.insert(V).second) continue; if (const User *U = dyn_cast<User>(V)) { @@ -502,13 +535,13 @@ void Verifier::visitAliaseeSubExpr(const GlobalAlias &GA, const Constant &C) { visitAliaseeSubExpr(Visited, GA, C); } -void Verifier::visitAliaseeSubExpr(SmallPtrSet<const GlobalAlias *, 4> &Visited, +void Verifier::visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias*> &Visited, const GlobalAlias &GA, const Constant &C) { if (const auto *GV = dyn_cast<GlobalValue>(&C)) { Assert1(!GV->isDeclaration(), "Alias must point to a definition", &GA); if (const auto *GA2 = dyn_cast<GlobalAlias>(GV)) { - Assert1(Visited.insert(GA2), "Aliases cannot form a cycle", &GA); + Assert1(Visited.insert(GA2).second, "Aliases cannot form a cycle", &GA); Assert1(!GA2->mayBeOverridden(), "Alias cannot point to a weak alias", &GA); @@ -557,46 +590,77 @@ void Verifier::visitNamedMDNode(const NamedMDNode &NMD) { if (!MD) continue; - Assert1(!MD->isFunctionLocal(), - "Named metadata operand cannot be function local!", MD); - visitMDNode(*MD, nullptr); + visitMDNode(*MD); } } -void Verifier::visitMDNode(MDNode &MD, Function *F) { +void Verifier::visitMDNode(MDNode &MD) { // Only visit each node once. Metadata can be mutually recursive, so this // avoids infinite recursion here, as well as being an optimization. - if (!MDNodes.insert(&MD)) + if (!MDNodes.insert(&MD).second) return; for (unsigned i = 0, e = MD.getNumOperands(); i != e; ++i) { - Value *Op = MD.getOperand(i); + Metadata *Op = MD.getOperand(i); if (!Op) continue; - if (isa<Constant>(Op) || isa<MDString>(Op)) + Assert2(!isa<LocalAsMetadata>(Op), "Invalid operand for global metadata!", + &MD, Op); + if (auto *N = dyn_cast<MDNode>(Op)) { + visitMDNode(*N); continue; - if (MDNode *N = dyn_cast<MDNode>(Op)) { - Assert2(MD.isFunctionLocal() || !N->isFunctionLocal(), - "Global metadata operand cannot be function local!", &MD, N); - visitMDNode(*N, F); + } + if (auto *V = dyn_cast<ValueAsMetadata>(Op)) { + visitValueAsMetadata(*V, nullptr); continue; } - Assert2(MD.isFunctionLocal(), "Invalid operand for global metadata!", &MD, Op); - - // If this was an instruction, bb, or argument, verify that it is in the - // function that we expect. - Function *ActualF = nullptr; - if (Instruction *I = dyn_cast<Instruction>(Op)) - ActualF = I->getParent()->getParent(); - else if (BasicBlock *BB = dyn_cast<BasicBlock>(Op)) - ActualF = BB->getParent(); - else if (Argument *A = dyn_cast<Argument>(Op)) - ActualF = A->getParent(); - assert(ActualF && "Unimplemented function local metadata case!"); - - Assert2(ActualF == F, "function-local metadata used in wrong function", - &MD, Op); } + + // Check these last, so we diagnose problems in operands first. + Assert1(!isa<MDNodeFwdDecl>(MD), "Expected no forward declarations!", &MD); + Assert1(MD.isResolved(), "All nodes should be resolved!", &MD); +} + +void Verifier::visitValueAsMetadata(ValueAsMetadata &MD, Function *F) { + Assert1(MD.getValue(), "Expected valid value", &MD); + Assert2(!MD.getValue()->getType()->isMetadataTy(), + "Unexpected metadata round-trip through values", &MD, MD.getValue()); + + auto *L = dyn_cast<LocalAsMetadata>(&MD); + if (!L) + return; + + Assert1(F, "function-local metadata used outside a function", L); + + // If this was an instruction, bb, or argument, verify that it is in the + // function that we expect. + Function *ActualF = nullptr; + if (Instruction *I = dyn_cast<Instruction>(L->getValue())) { + Assert2(I->getParent(), "function-local metadata not in basic block", L, I); + ActualF = I->getParent()->getParent(); + } else if (BasicBlock *BB = dyn_cast<BasicBlock>(L->getValue())) + ActualF = BB->getParent(); + else if (Argument *A = dyn_cast<Argument>(L->getValue())) + ActualF = A->getParent(); + assert(ActualF && "Unimplemented function local metadata case!"); + + Assert1(ActualF == F, "function-local metadata used in wrong function", L); +} + +void Verifier::visitMetadataAsValue(MetadataAsValue &MDV, Function *F) { + Metadata *MD = MDV.getMetadata(); + if (auto *N = dyn_cast<MDNode>(MD)) { + visitMDNode(*N); + return; + } + + // Only visit each node once. Metadata can be mutually recursive, so this + // avoids infinite recursion here, as well as being an optimization. + if (!MDNodes.insert(MD).second) + return; + + if (auto *V = dyn_cast<ValueAsMetadata>(MD)) + visitValueAsMetadata(*V, F); } void Verifier::visitComdat(const Comdat &C) { @@ -647,7 +711,7 @@ void Verifier::visitModuleFlags(const Module &M) { for (unsigned I = 0, E = Requirements.size(); I != E; ++I) { const MDNode *Requirement = Requirements[I]; const MDString *Flag = cast<MDString>(Requirement->getOperand(0)); - const Value *ReqValue = Requirement->getOperand(1); + const Metadata *ReqValue = Requirement->getOperand(1); const MDNode *Op = SeenIDs.lookup(Flag); if (!Op) { @@ -673,24 +737,23 @@ Verifier::visitModuleFlag(const MDNode *Op, // constant int), the flag ID (an MDString), and the value. Assert1(Op->getNumOperands() == 3, "incorrect number of operands in module flag", Op); - ConstantInt *Behavior = dyn_cast<ConstantInt>(Op->getOperand(0)); + Module::ModFlagBehavior MFB; + if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) { + Assert1( + mdconst::dyn_extract<ConstantInt>(Op->getOperand(0)), + "invalid behavior operand in module flag (expected constant integer)", + Op->getOperand(0)); + Assert1(false, + "invalid behavior operand in module flag (unexpected constant)", + Op->getOperand(0)); + } MDString *ID = dyn_cast<MDString>(Op->getOperand(1)); - Assert1(Behavior, - "invalid behavior operand in module flag (expected constant integer)", - Op->getOperand(0)); - unsigned BehaviorValue = Behavior->getZExtValue(); Assert1(ID, "invalid ID operand in module flag (expected metadata string)", Op->getOperand(1)); // Sanity check the values for behaviors with additional requirements. - switch (BehaviorValue) { - default: - Assert1(false, - "invalid behavior operand in module flag (unexpected constant)", - Op->getOperand(0)); - break; - + switch (MFB) { case Module::Error: case Module::Warning: case Module::Override: @@ -726,7 +789,7 @@ Verifier::visitModuleFlag(const MDNode *Op, } // Unless this is a "requires" flag, check the ID is unique. - if (BehaviorValue != Module::Require) { + if (MFB != Module::Require) { bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second; Assert1(Inserted, "module flag identifiers must be unique (or of 'require' type)", @@ -959,48 +1022,13 @@ void Verifier::VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs, } } -void Verifier::VerifyBitcastType(const Value *V, Type *DestTy, Type *SrcTy) { - // Get the size of the types in bits, we'll need this later - unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); - unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); - - // BitCast implies a no-op cast of type only. No bits change. - // However, you can't cast pointers to anything but pointers. - Assert1(SrcTy->isPointerTy() == DestTy->isPointerTy(), - "Bitcast requires both operands to be pointer or neither", V); - Assert1(SrcBitSize == DestBitSize, - "Bitcast requires types of same width", V); - - // Disallow aggregates. - Assert1(!SrcTy->isAggregateType(), - "Bitcast operand must not be aggregate", V); - Assert1(!DestTy->isAggregateType(), - "Bitcast type must not be aggregate", V); - - // Without datalayout, assume all address spaces are the same size. - // Don't check if both types are not pointers. - // Skip casts between scalars and vectors. - if (!DL || - !SrcTy->isPtrOrPtrVectorTy() || - !DestTy->isPtrOrPtrVectorTy() || - SrcTy->isVectorTy() != DestTy->isVectorTy()) { +void Verifier::VerifyConstantExprBitcastType(const ConstantExpr *CE) { + if (CE->getOpcode() != Instruction::BitCast) return; - } - - unsigned SrcAS = SrcTy->getPointerAddressSpace(); - unsigned DstAS = DestTy->getPointerAddressSpace(); - - Assert1(SrcAS == DstAS, - "Bitcasts between pointers of different address spaces is not legal." - "Use AddrSpaceCast instead.", V); -} -void Verifier::VerifyConstantExprBitcastType(const ConstantExpr *CE) { - if (CE->getOpcode() == Instruction::BitCast) { - Type *SrcTy = CE->getOperand(0)->getType(); - Type *DstTy = CE->getType(); - VerifyBitcastType(CE, DstTy, SrcTy); - } + Assert1(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0), + CE->getType()), + "Invalid bitcast", CE); } bool Verifier::VerifyAttributeCount(AttributeSet Attrs, unsigned Params) { @@ -1055,20 +1083,19 @@ void Verifier::visitFunction(const Function &F) { "Attribute 'builtin' can only be applied to a callsite.", &F); // Check that this function meets the restrictions on this calling convention. + // Sometimes varargs is used for perfectly forwarding thunks, so some of these + // restrictions can be lifted. switch (F.getCallingConv()) { default: - break; case CallingConv::C: break; case CallingConv::Fast: case CallingConv::Cold: - case CallingConv::X86_FastCall: - case CallingConv::X86_ThisCall: case CallingConv::Intel_OCL_BI: case CallingConv::PTX_Kernel: case CallingConv::PTX_Device: - Assert1(!F.isVarArg(), - "Varargs functions must have C calling conventions!", &F); + Assert1(!F.isVarArg(), "Calling convention does not support varargs or " + "perfect forwarding!", &F); break; } @@ -1101,7 +1128,7 @@ void Verifier::visitFunction(const Function &F) { // Check the entry node const BasicBlock *Entry = &F.getEntryBlock(); - Assert1(pred_begin(Entry) == pred_end(Entry), + Assert1(pred_empty(Entry), "Entry block to function must not have predecessors!", Entry); // The address of the entry block cannot be taken, unless it is dead. @@ -1176,6 +1203,12 @@ void Verifier::visitBasicBlock(BasicBlock &BB) { } } } + + // Check that all instructions have their parent pointers set up correctly. + for (auto &I : BB) + { + Assert(I.getParent() == &BB, "Instruction has bogus parent pointer!"); + } } void Verifier::visitTerminatorInst(TerminatorInst &I) { @@ -1218,7 +1251,7 @@ void Verifier::visitSwitchInst(SwitchInst &SI) { for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); i != e; ++i) { Assert1(i.getCaseValue()->getType() == SwitchTy, "Switch constants must all be same type as switch value!", &SI); - Assert2(Constants.insert(i.getCaseValue()), + Assert2(Constants.insert(i.getCaseValue()).second, "Duplicate integer as switch case", &SI, i.getCaseValue()); } @@ -1476,9 +1509,9 @@ void Verifier::visitIntToPtrInst(IntToPtrInst &I) { } void Verifier::visitBitCastInst(BitCastInst &I) { - Type *SrcTy = I.getOperand(0)->getType(); - Type *DestTy = I.getType(); - VerifyBitcastType(&I, DestTy, SrcTy); + Assert1( + CastInst::castIsValid(Instruction::BitCast, I.getOperand(0), I.getType()), + "Invalid bitcast", &I); visitInstruction(I); } @@ -1887,6 +1920,57 @@ static bool isContiguous(const ConstantRange &A, const ConstantRange &B) { return A.getUpper() == B.getLower() || A.getLower() == B.getUpper(); } +void Verifier::visitRangeMetadata(Instruction& I, + MDNode* Range, Type* Ty) { + assert(Range && + Range == I.getMetadata(LLVMContext::MD_range) && + "precondition violation"); + + unsigned NumOperands = Range->getNumOperands(); + Assert1(NumOperands % 2 == 0, "Unfinished range!", Range); + unsigned NumRanges = NumOperands / 2; + Assert1(NumRanges >= 1, "It should have at least one range!", Range); + + ConstantRange LastRange(1); // Dummy initial value + for (unsigned i = 0; i < NumRanges; ++i) { + ConstantInt *Low = + mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i)); + Assert1(Low, "The lower limit must be an integer!", Low); + ConstantInt *High = + mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i + 1)); + Assert1(High, "The upper limit must be an integer!", High); + Assert1(High->getType() == Low->getType() && + High->getType() == Ty, "Range types must match instruction type!", + &I); + + APInt HighV = High->getValue(); + APInt LowV = Low->getValue(); + ConstantRange CurRange(LowV, HighV); + Assert1(!CurRange.isEmptySet() && !CurRange.isFullSet(), + "Range must not be empty!", Range); + if (i != 0) { + Assert1(CurRange.intersectWith(LastRange).isEmptySet(), + "Intervals are overlapping", Range); + Assert1(LowV.sgt(LastRange.getLower()), "Intervals are not in order", + Range); + Assert1(!isContiguous(CurRange, LastRange), "Intervals are contiguous", + Range); + } + LastRange = ConstantRange(LowV, HighV); + } + if (NumRanges > 2) { + APInt FirstLow = + mdconst::dyn_extract<ConstantInt>(Range->getOperand(0))->getValue(); + APInt FirstHigh = + mdconst::dyn_extract<ConstantInt>(Range->getOperand(1))->getValue(); + ConstantRange FirstRange(FirstLow, FirstHigh); + Assert1(FirstRange.intersectWith(LastRange).isEmptySet(), + "Intervals are overlapping", Range); + Assert1(!isContiguous(FirstRange, LastRange), "Intervals are contiguous", + Range); + } +} + void Verifier::visitLoadInst(LoadInst &LI) { PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType()); Assert1(PTy, "Load operand must be a pointer.", &LI); @@ -1914,52 +1998,6 @@ void Verifier::visitLoadInst(LoadInst &LI) { "Non-atomic load cannot have SynchronizationScope specified", &LI); } - if (MDNode *Range = LI.getMetadata(LLVMContext::MD_range)) { - unsigned NumOperands = Range->getNumOperands(); - Assert1(NumOperands % 2 == 0, "Unfinished range!", Range); - unsigned NumRanges = NumOperands / 2; - Assert1(NumRanges >= 1, "It should have at least one range!", Range); - - ConstantRange LastRange(1); // Dummy initial value - for (unsigned i = 0; i < NumRanges; ++i) { - ConstantInt *Low = dyn_cast<ConstantInt>(Range->getOperand(2*i)); - Assert1(Low, "The lower limit must be an integer!", Low); - ConstantInt *High = dyn_cast<ConstantInt>(Range->getOperand(2*i + 1)); - Assert1(High, "The upper limit must be an integer!", High); - Assert1(High->getType() == Low->getType() && - High->getType() == ElTy, "Range types must match load type!", - &LI); - - APInt HighV = High->getValue(); - APInt LowV = Low->getValue(); - ConstantRange CurRange(LowV, HighV); - Assert1(!CurRange.isEmptySet() && !CurRange.isFullSet(), - "Range must not be empty!", Range); - if (i != 0) { - Assert1(CurRange.intersectWith(LastRange).isEmptySet(), - "Intervals are overlapping", Range); - Assert1(LowV.sgt(LastRange.getLower()), "Intervals are not in order", - Range); - Assert1(!isContiguous(CurRange, LastRange), "Intervals are contiguous", - Range); - } - LastRange = ConstantRange(LowV, HighV); - } - if (NumRanges > 2) { - APInt FirstLow = - dyn_cast<ConstantInt>(Range->getOperand(0))->getValue(); - APInt FirstHigh = - dyn_cast<ConstantInt>(Range->getOperand(1))->getValue(); - ConstantRange FirstRange(FirstLow, FirstHigh); - Assert1(FirstRange.intersectWith(LastRange).isEmptySet(), - "Intervals are overlapping", Range); - Assert1(!isContiguous(FirstRange, LastRange), "Intervals are contiguous", - Range); - } - - - } - visitInstruction(LI); } @@ -2214,11 +2252,15 @@ void Verifier::visitInstruction(Instruction &I) { if (Function *F = dyn_cast<Function>(I.getOperand(i))) { // Check to make sure that the "address of" an intrinsic function is never // taken. - Assert1(!F->isIntrinsic() || i == (isa<CallInst>(I) ? e-1 : 0), + Assert1(!F->isIntrinsic() || i == (isa<CallInst>(I) ? e-1 : + isa<InvokeInst>(I) ? e-3 : 0), "Cannot take the address of an intrinsic!", &I); Assert1(!F->isIntrinsic() || isa<CallInst>(I) || - F->getIntrinsicID() == Intrinsic::donothing, - "Cannot invoke an intrinsinc other than donothing", &I); + F->getIntrinsicID() == Intrinsic::donothing || + F->getIntrinsicID() == Intrinsic::experimental_patchpoint_void || + F->getIntrinsicID() == Intrinsic::experimental_patchpoint_i64, + "Cannot invoke an intrinsinc other than" + " donothing or patchpoint", &I); Assert1(F->getParent() == M, "Referencing function in another module!", &I); } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) { @@ -2246,7 +2288,7 @@ void Verifier::visitInstruction(Instruction &I) { while (!Stack.empty()) { const ConstantExpr *V = Stack.pop_back_val(); - if (!Visited.insert(V)) + if (!Visited.insert(V).second) continue; VerifyConstantExprBitcastType(V); @@ -2264,8 +2306,8 @@ void Verifier::visitInstruction(Instruction &I) { Assert1(I.getType()->isFPOrFPVectorTy(), "fpmath requires a floating point result!", &I); Assert1(MD->getNumOperands() == 1, "fpmath takes one operand!", &I); - Value *Op0 = MD->getOperand(0); - if (ConstantFP *CFP0 = dyn_cast_or_null<ConstantFP>(Op0)) { + if (ConstantFP *CFP0 = + mdconst::dyn_extract_or_null<ConstantFP>(MD->getOperand(0))) { APFloat Accuracy = CFP0->getValueAPF(); Assert1(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(), "fpmath accuracy not a positive number!", &I); @@ -2274,9 +2316,19 @@ void Verifier::visitInstruction(Instruction &I) { } } - MDNode *MD = I.getMetadata(LLVMContext::MD_range); - Assert1(!MD || isa<LoadInst>(I) || isa<CallInst>(I) || isa<InvokeInst>(I), - "Ranges are only for loads, calls and invokes!", &I); + if (MDNode *Range = I.getMetadata(LLVMContext::MD_range)) { + Assert1(isa<LoadInst>(I) || isa<CallInst>(I) || isa<InvokeInst>(I), + "Ranges are only for loads, calls and invokes!", &I); + visitRangeMetadata(I, Range, I.getType()); + } + + if (I.getMetadata(LLVMContext::MD_nonnull)) { + Assert1(I.getType()->isPointerTy(), + "nonnull applies only to pointer types", &I); + Assert1(isa<LoadInst>(I), + "nonnull applies only to load instructions, use attributes" + " for calls or invokes", &I); + } InstsInThisBlock.insert(&I); } @@ -2382,6 +2434,26 @@ bool Verifier::VerifyIntrinsicType(Type *Ty, !isa<VectorType>(ArgTys[D.getArgumentNumber()]) || VectorType::getHalfElementsVectorType( cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty; + case IITDescriptor::SameVecWidthArgument: { + if (D.getArgumentNumber() >= ArgTys.size()) + return true; + VectorType * ReferenceType = + dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]); + VectorType *ThisArgType = dyn_cast<VectorType>(Ty); + if (!ThisArgType || !ReferenceType || + (ReferenceType->getVectorNumElements() != + ThisArgType->getVectorNumElements())) + return true; + return VerifyIntrinsicType(ThisArgType->getVectorElementType(), + Infos, ArgTys); + } + case IITDescriptor::PtrToArgument: { + if (D.getArgumentNumber() >= ArgTys.size()) + return true; + Type * ReferenceType = ArgTys[D.getArgumentNumber()]; + PointerType *ThisArgType = dyn_cast<PointerType>(Ty); + return (!ThisArgType || ThisArgType->getElementType() != ReferenceType); + } } llvm_unreachable("unhandled"); } @@ -2459,8 +2531,8 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { // If the intrinsic takes MDNode arguments, verify that they are either global // or are local to *this* function. for (unsigned i = 0, e = CI.getNumArgOperands(); i != e; ++i) - if (MDNode *MD = dyn_cast<MDNode>(CI.getArgOperand(i))) - visitMDNode(*MD, CI.getParent()->getParent()); + if (auto *MD = dyn_cast<MetadataAsValue>(CI.getArgOperand(i))) + visitMetadataAsValue(*MD, CI.getParent()->getParent()); switch (ID) { default: @@ -2472,11 +2544,8 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { "constant int", &CI); break; case Intrinsic::dbg_declare: { // llvm.dbg.declare - Assert1(CI.getArgOperand(0) && isa<MDNode>(CI.getArgOperand(0)), - "invalid llvm.dbg.declare intrinsic call 1", &CI); - MDNode *MD = cast<MDNode>(CI.getArgOperand(0)); - Assert1(MD->getNumOperands() == 1, - "invalid llvm.dbg.declare intrinsic call 2", &CI); + Assert1(CI.getArgOperand(0) && isa<MetadataAsValue>(CI.getArgOperand(0)), + "invalid llvm.dbg.declare intrinsic call 1", &CI); } break; case Intrinsic::memcpy: case Intrinsic::memmove: @@ -2536,7 +2605,189 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { Assert1(isa<ConstantInt>(CI.getArgOperand(1)), "llvm.invariant.end parameter #2 must be a constant integer", &CI); break; + + case Intrinsic::frameallocate: { + BasicBlock *BB = CI.getParent(); + Assert1(BB == &BB->getParent()->front(), + "llvm.frameallocate used outside of entry block", &CI); + Assert1(!SawFrameAllocate, + "multiple calls to llvm.frameallocate in one function", &CI); + SawFrameAllocate = true; + Assert1(isa<ConstantInt>(CI.getArgOperand(0)), + "llvm.frameallocate argument must be constant integer size", &CI); + break; + } + case Intrinsic::framerecover: { + Value *FnArg = CI.getArgOperand(0)->stripPointerCasts(); + Function *Fn = dyn_cast<Function>(FnArg); + Assert1(Fn && !Fn->isDeclaration(), "llvm.framerecover first " + "argument must be function defined in this module", &CI); + break; + } + + case Intrinsic::experimental_gc_statepoint: { + Assert1(!CI.doesNotAccessMemory() && + !CI.onlyReadsMemory(), + "gc.statepoint must read and write memory to preserve " + "reordering restrictions required by safepoint semantics", &CI); + Assert1(!CI.isInlineAsm(), + "gc.statepoint support for inline assembly unimplemented", &CI); + + const Value *Target = CI.getArgOperand(0); + const PointerType *PT = dyn_cast<PointerType>(Target->getType()); + Assert2(PT && PT->getElementType()->isFunctionTy(), + "gc.statepoint callee must be of function pointer type", + &CI, Target); + FunctionType *TargetFuncType = cast<FunctionType>(PT->getElementType()); + Assert1(!TargetFuncType->isVarArg(), + "gc.statepoint support for var arg functions not implemented", &CI); + + const Value *NumCallArgsV = CI.getArgOperand(1); + Assert1(isa<ConstantInt>(NumCallArgsV), + "gc.statepoint number of arguments to underlying call " + "must be constant integer", &CI); + const int NumCallArgs = cast<ConstantInt>(NumCallArgsV)->getZExtValue(); + Assert1(NumCallArgs >= 0, + "gc.statepoint number of arguments to underlying call " + "must be positive", &CI); + Assert1(NumCallArgs == (int)TargetFuncType->getNumParams(), + "gc.statepoint mismatch in number of call args", &CI); + + const Value *Unused = CI.getArgOperand(2); + Assert1(isa<ConstantInt>(Unused) && + cast<ConstantInt>(Unused)->isNullValue(), + "gc.statepoint parameter #3 must be zero", &CI); + + // Verify that the types of the call parameter arguments match + // the type of the wrapped callee. + for (int i = 0; i < NumCallArgs; i++) { + Type *ParamType = TargetFuncType->getParamType(i); + Type *ArgType = CI.getArgOperand(3+i)->getType(); + Assert1(ArgType == ParamType, + "gc.statepoint call argument does not match wrapped " + "function type", &CI); + } + const int EndCallArgsInx = 2+NumCallArgs; + const Value *NumDeoptArgsV = CI.getArgOperand(EndCallArgsInx+1); + Assert1(isa<ConstantInt>(NumDeoptArgsV), + "gc.statepoint number of deoptimization arguments " + "must be constant integer", &CI); + const int NumDeoptArgs = cast<ConstantInt>(NumDeoptArgsV)->getZExtValue(); + Assert1(NumDeoptArgs >= 0, + "gc.statepoint number of deoptimization arguments " + "must be positive", &CI); + + Assert1(4 + NumCallArgs + NumDeoptArgs <= (int)CI.getNumArgOperands(), + "gc.statepoint too few arguments according to length fields", &CI); + + // Check that the only uses of this gc.statepoint are gc.result or + // gc.relocate calls which are tied to this statepoint and thus part + // of the same statepoint sequence + for (User *U : CI.users()) { + const CallInst *Call = dyn_cast<const CallInst>(U); + Assert2(Call, "illegal use of statepoint token", &CI, U); + if (!Call) continue; + Assert2(isGCRelocate(Call) || isGCResult(Call), + "gc.result or gc.relocate are the only value uses" + "of a gc.statepoint", &CI, U); + if (isGCResult(Call)) { + Assert2(Call->getArgOperand(0) == &CI, + "gc.result connected to wrong gc.statepoint", + &CI, Call); + } else if (isGCRelocate(Call)) { + Assert2(Call->getArgOperand(0) == &CI, + "gc.relocate connected to wrong gc.statepoint", + &CI, Call); + } + } + + // Note: It is legal for a single derived pointer to be listed multiple + // times. It's non-optimal, but it is legal. It can also happen after + // insertion if we strip a bitcast away. + // Note: It is really tempting to check that each base is relocated and + // that a derived pointer is never reused as a base pointer. This turns + // out to be problematic since optimizations run after safepoint insertion + // can recognize equality properties that the insertion logic doesn't know + // about. See example statepoint.ll in the verifier subdirectory + break; } + case Intrinsic::experimental_gc_result_int: + case Intrinsic::experimental_gc_result_float: + case Intrinsic::experimental_gc_result_ptr: { + // Are we tied to a statepoint properly? + CallSite StatepointCS(CI.getArgOperand(0)); + const Function *StatepointFn = + StatepointCS.getInstruction() ? StatepointCS.getCalledFunction() : nullptr; + Assert2(StatepointFn && StatepointFn->isDeclaration() && + StatepointFn->getIntrinsicID() == Intrinsic::experimental_gc_statepoint, + "gc.result operand #1 must be from a statepoint", + &CI, CI.getArgOperand(0)); + + // Assert that result type matches wrapped callee. + const Value *Target = StatepointCS.getArgument(0); + const PointerType *PT = cast<PointerType>(Target->getType()); + const FunctionType *TargetFuncType = + cast<FunctionType>(PT->getElementType()); + Assert1(CI.getType() == TargetFuncType->getReturnType(), + "gc.result result type does not match wrapped callee", + &CI); + break; + } + case Intrinsic::experimental_gc_relocate: { + // Are we tied to a statepoint properly? + CallSite StatepointCS(CI.getArgOperand(0)); + const Function *StatepointFn = + StatepointCS.getInstruction() ? StatepointCS.getCalledFunction() : nullptr; + Assert2(StatepointFn && StatepointFn->isDeclaration() && + StatepointFn->getIntrinsicID() == Intrinsic::experimental_gc_statepoint, + "gc.relocate operand #1 must be from a statepoint", + &CI, CI.getArgOperand(0)); + + // Both the base and derived must be piped through the safepoint + Value* Base = CI.getArgOperand(1); + Assert1(isa<ConstantInt>(Base), + "gc.relocate operand #2 must be integer offset", &CI); + + Value* Derived = CI.getArgOperand(2); + Assert1(isa<ConstantInt>(Derived), + "gc.relocate operand #3 must be integer offset", &CI); + + const int BaseIndex = cast<ConstantInt>(Base)->getZExtValue(); + const int DerivedIndex = cast<ConstantInt>(Derived)->getZExtValue(); + // Check the bounds + Assert1(0 <= BaseIndex && + BaseIndex < (int)StatepointCS.arg_size(), + "gc.relocate: statepoint base index out of bounds", &CI); + Assert1(0 <= DerivedIndex && + DerivedIndex < (int)StatepointCS.arg_size(), + "gc.relocate: statepoint derived index out of bounds", &CI); + + // Check that BaseIndex and DerivedIndex fall within the 'gc parameters' + // section of the statepoint's argument + const int NumCallArgs = + cast<ConstantInt>(StatepointCS.getArgument(1))->getZExtValue(); + const int NumDeoptArgs = + cast<ConstantInt>(StatepointCS.getArgument(NumCallArgs + 3))->getZExtValue(); + const int GCParamArgsStart = NumCallArgs + NumDeoptArgs + 4; + const int GCParamArgsEnd = StatepointCS.arg_size(); + Assert1(GCParamArgsStart <= BaseIndex && + BaseIndex < GCParamArgsEnd, + "gc.relocate: statepoint base index doesn't fall within the " + "'gc parameters' section of the statepoint call", &CI); + Assert1(GCParamArgsStart <= DerivedIndex && + DerivedIndex < GCParamArgsEnd, + "gc.relocate: statepoint derived index doesn't fall within the " + "'gc parameters' section of the statepoint call", &CI); + + + // Assert that the result type matches the type of the relocated pointer + GCRelocateOperands Operands(&CI); + Assert1(Operands.derivedPtr()->getType() == CI.getType(), + "gc.relocate: relocating a pointer shouldn't change its type", + &CI); + break; + } + }; } void DebugInfoVerifier::verifyDebugInfo() { @@ -2615,7 +2866,7 @@ bool llvm::verifyModule(const Module &M, raw_ostream *OS) { bool Broken = false; for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) - if (!I->isDeclaration()) + if (!I->isDeclaration() && !I->isMaterializable()) Broken |= !V.verify(*I); // Note that this function's return value is inverted from what you would @@ -2699,15 +2950,15 @@ ModulePass *llvm::createDebugInfoVerifierPass(bool FatalErrors) { return new DebugInfoVerifierLegacyPass(FatalErrors); } -PreservedAnalyses VerifierPass::run(Module *M) { - if (verifyModule(*M, &dbgs()) && FatalErrors) +PreservedAnalyses VerifierPass::run(Module &M) { + if (verifyModule(M, &dbgs()) && FatalErrors) report_fatal_error("Broken module found, compilation aborted!"); return PreservedAnalyses::all(); } -PreservedAnalyses VerifierPass::run(Function *F) { - if (verifyFunction(*F, &dbgs()) && FatalErrors) +PreservedAnalyses VerifierPass::run(Function &F) { + if (verifyFunction(F, &dbgs()) && FatalErrors) report_fatal_error("Broken function found, compilation aborted!"); return PreservedAnalyses::all(); |