Index: lib/IR/AutoUpgrade.cpp =================================================================== --- lib/IR/AutoUpgrade.cpp +++ lib/IR/AutoUpgrade.cpp @@ -2260,12 +2260,30 @@ return RetCode; } +static bool replaceModuleFlagBehavior(Metadata *M, + Module::ModFlagBehavior After, + Module::ModFlagBehavior Before, + LLVMContext &Context) { + auto *MD = dyn_cast(M); + assert(MD->getNumOperands() > 2 && "Module Flag need to have at least 3 Ops"); + if (ConstantInt *Behavior = + mdconst::dyn_extract_or_null(MD->getOperand(0))) { + if (Behavior->getLimitedValue() == Before) { + Type *Int32Ty = Type::getInt32Ty(Context); + MD->replaceOperandWith( + 0, ConstantAsMetadata::get(ConstantInt::get(Int32Ty, After))); + return true; + } + } + return false; +} + bool llvm::UpgradeModuleFlags(Module &M) { const NamedMDNode *ModFlags = M.getModuleFlagsMetadata(); if (!ModFlags) return false; - bool HasObjCFlag = false, HasClassProperties = false; + bool HasObjCFlag = false, HasClassProperties = false, Changed = false; for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) { MDNode *Op = ModFlags->getOperand(I); if (Op->getNumOperands() < 2) @@ -2277,6 +2295,11 @@ HasObjCFlag = true; if (ID->getString() == "Objective-C Class Properties") HasClassProperties = true; + // Upgrade PIC/PIE Module Flags. The module flag behavior for these two + // field was Error and now they are Max. + if (ID->getString() == "PIC Level" || ID->getString() == "PIE Level") + Changed |= replaceModuleFlagBehavior(Op, Module::Max, Module::Error, + M.getContext()); } // "Objective-C Class Properties" is recently added for Objective-C. We // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module @@ -2286,9 +2309,10 @@ if (HasObjCFlag && !HasClassProperties) { M.addModuleFlag(llvm::Module::Override, "Objective-C Class Properties", (uint32_t)0); - return true; + Changed = true; } - return false; + + return Changed; } static bool isOldLoopArgument(Metadata *MD) { Index: test/Bitcode/upgrade-module-flag.ll =================================================================== --- test/Bitcode/upgrade-module-flag.ll +++ test/Bitcode/upgrade-module-flag.ll @@ -1,9 +1,13 @@ ; RUN: llvm-as < %s | llvm-dis | FileCheck %s ; RUN: verify-uselistorder < %s -!llvm.module.flags = !{!0} +!llvm.module.flags = !{!0, !1, !2} -!0 = !{i32 1, !"Objective-C Image Info Version", i32 0} +!0 = !{i32 1, !"PIC Level", i32 1} +!1 = !{i32 1, !"PIE Level", i32 1} +!2 = !{i32 1, !"Objective-C Image Info Version", i32 0} -; CHECK: !0 = !{i32 1, !"Objective-C Image Info Version", i32 0} -; CHECK: !1 = !{i32 4, !"Objective-C Class Properties", i32 0} +; CHECK: !0 = !{i32 7, !"PIC Level", i32 1} +; CHECK: !1 = !{i32 7, !"PIE Level", i32 1} +; CHECK: !2 = !{i32 1, !"Objective-C Image Info Version", i32 0} +; CHECK: !3 = !{i32 4, !"Objective-C Class Properties", i32 0} Index: test/Linker/module-flags-pic-1-a.ll =================================================================== --- test/Linker/module-flags-pic-1-a.ll +++ test/Linker/module-flags-pic-1-a.ll @@ -2,8 +2,8 @@ ; test linking modules with specified and default PIC levels -!0 = !{ i32 1, !"PIC Level", i32 1 } +!0 = !{ i32 7, !"PIC Level", i32 1 } !llvm.module.flags = !{!0} ; CHECK: !llvm.module.flags = !{!0} -; CHECK: !0 = !{i32 1, !"PIC Level", i32 1} +; CHECK: !0 = !{i32 7, !"PIC Level", i32 1}