Index: include/llvm/IR/AutoUpgrade.h =================================================================== --- include/llvm/IR/AutoUpgrade.h +++ include/llvm/IR/AutoUpgrade.h @@ -21,6 +21,7 @@ class Constant; class Function; class Instruction; + class LLVMContext; class MDNode; class Module; class GlobalVariable; @@ -51,6 +52,11 @@ /// module is modified. bool UpgradeModuleFlags(Module &M); + /// If the given TBAA tag uses the scalar TBAA format, create a new node + /// corresponding to the upgrade to the struct-path aware TBAA format. + /// Otherwise return the \p TBAANode itself. + MDNode *UpgradeTBAANode(LLVMContext &Context, MDNode *TBAANode); + /// If the TBAA tag for the given instruction uses the scalar TBAA format, /// we upgrade it to the struct-path aware TBAA format. void UpgradeInstWithTBAATag(Instruction *I); Index: lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- lib/Bitcode/Reader/BitcodeReader.cpp +++ lib/Bitcode/Reader/BitcodeReader.cpp @@ -257,8 +257,6 @@ std::vector > FunctionPrologues; std::vector > FunctionPersonalityFns; - SmallVector InstsWithTBAATag; - bool HasSeenOldLoopTags = false; /// The set of attributes by index. Index zero in the file is for null, and @@ -3752,6 +3750,7 @@ // After the VST has been parsed, we need to make sure intrinsic name // are auto-upgraded. return globalCleanup(); + return std::error_code(); } break; case bitc::USELIST_BLOCK_ID: @@ -4404,11 +4403,11 @@ if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop) MD = upgradeInstructionLoopAttachment(*MD); - Inst->setMetadata(I->second, MD); if (I->second == LLVMContext::MD_tbaa) { - InstsWithTBAATag.push_back(Inst); - continue; + assert(!isa() && "should load metadata before attachment"); + MD = UpgradeTBAANode(Inst->getContext(), MD); } + Inst->setMetadata(I->second, MD); } break; } @@ -5821,11 +5820,6 @@ if (!BasicBlockFwdRefs.empty()) return error("Never resolved function from blockaddress"); - // Upgrading intrinsic calls before TBAA can cause TBAA metadata to be lost, - // to prevent this instructions with TBAA tags should be upgraded first. - for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++) - UpgradeInstWithTBAATag(InstsWithTBAATag[I]); - // Upgrade any intrinsic calls that slipped through (should not happen!) and // delete the old functions to clean up. We can't do this unless the entire // module is materialized because there could always be another function body Index: lib/IR/AutoUpgrade.cpp =================================================================== --- lib/IR/AutoUpgrade.cpp +++ lib/IR/AutoUpgrade.cpp @@ -1462,28 +1462,33 @@ } } -void llvm::UpgradeInstWithTBAATag(Instruction *I) { - MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa); - assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag"); +MDNode *llvm::UpgradeTBAANode(LLVMContext &Context, MDNode *MD) { // Check if the tag uses struct-path aware TBAA format. if (isa(MD->getOperand(0)) && MD->getNumOperands() >= 3) - return; + return MD; if (MD->getNumOperands() == 3) { Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)}; - MDNode *ScalarType = MDNode::get(I->getContext(), Elts); + MDNode *ScalarType = MDNode::get(Context, Elts); // Create a MDNode Metadata *Elts2[] = {ScalarType, ScalarType, - ConstantAsMetadata::get(Constant::getNullValue( - Type::getInt64Ty(I->getContext()))), + ConstantAsMetadata::get( + Constant::getNullValue(Type::getInt64Ty(Context))), MD->getOperand(2)}; - I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2)); - } else { - // Create a MDNode - Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue( - Type::getInt64Ty(I->getContext())))}; - I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts)); + return MDNode::get(Context, Elts2); } + // Create a MDNode + Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue( + Type::getInt64Ty(Context)))}; + return MDNode::get(Context, Elts); +} + +void llvm::UpgradeInstWithTBAATag(Instruction *I) { + MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa); + assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag"); + auto *UpgradedMD = UpgradeTBAANode(I->getContext(), MD); + if (MD != UpgradedMD) + I->setMetadata(LLVMContext::MD_tbaa, UpgradedMD); } Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,