Index: include/llvm/IR/DiagnosticInfo.h =================================================================== --- include/llvm/IR/DiagnosticInfo.h +++ include/llvm/IR/DiagnosticInfo.h @@ -583,14 +583,14 @@ /// matches the regular expression given in -Rpass=, then the diagnostic will /// be emitted. \p RemarkName is a textual identifier for the remark. \p /// Loc is the debug location and \p CodeRegion is the region that the - /// optimization operates on (currently on block is supported). + /// optimization operates on (currently only block is supported). OptimizationRemark(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const Value *CodeRegion); /// Same as above but the debug location and code region is derived from \p /// Instr. OptimizationRemark(const char *PassName, StringRef RemarkName, - Instruction *Inst); + const Instruction *Inst); static bool classof(const DiagnosticInfo *DI) { return DI->getKind() == DK_OptimizationRemark; Index: lib/CodeGen/StackProtector.cpp =================================================================== --- lib/CodeGen/StackProtector.cpp +++ lib/CodeGen/StackProtector.cpp @@ -18,6 +18,7 @@ #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/BranchProbabilityInfo.h" #include "llvm/Analysis/EHPersonalities.h" +#include "llvm/Analysis/OptimizationDiagnosticInfo.h" #include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/StackProtector.h" #include "llvm/IR/Attributes.h" @@ -58,7 +59,7 @@ char StackProtector::ID = 0; INITIALIZE_TM_PASS(StackProtector, "stack-protector", "Insert stack protectors", - false, true) + false, true) FunctionPass *llvm::createStackProtectorPass(const TargetMachine *TM) { return new StackProtector(TM); @@ -229,7 +230,19 @@ if (F->hasFnAttribute(Attribute::SafeStack)) return false; + // We are constructing the OptimizationRemarkEmitter on the fly rather than + // using the analysis pass to avoid building DominatorTree and LoopInfo which + // are not available this late in the IR pipeline. + OptimizationRemarkEmitter ORE(F); + Twine ReasonStub = + "stack protection applied to function " + F->getName() + " due to "; + StringRef RemarkName = "stack-protector-reason"; + if (F->hasFnAttribute(Attribute::StackProtectReq)) { + if (!F->getBasicBlockList().empty()) + ORE.emit( + OptimizationRemark(DEBUG_TYPE, RemarkName, F->getSubprogram(), &F->front()) + << (ReasonStub + "a function attribute or command-line switch").str()); NeedsProtector = true; Strong = true; // Use the same heuristic as strong to determine SSPLayout } else if (F->hasFnAttribute(Attribute::StackProtectStrong)) @@ -243,20 +256,26 @@ for (const Instruction &I : BB) { if (const AllocaInst *AI = dyn_cast(&I)) { if (AI->isArrayAllocation()) { + OptimizationRemark Remark(DEBUG_TYPE, RemarkName, &I); + Twine Reason = + ReasonStub + "a call to alloca or use of a variable length array"; if (const auto *CI = dyn_cast(AI->getArraySize())) { if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) { // A call to alloca with size >= SSPBufferSize requires // stack protectors. Layout.insert(std::make_pair(AI, SSPLK_LargeArray)); + ORE.emit(Remark << Reason.str()); NeedsProtector = true; } else if (Strong) { // Require protectors for all alloca calls in strong mode. Layout.insert(std::make_pair(AI, SSPLK_SmallArray)); + ORE.emit(Remark << Reason.str()); NeedsProtector = true; } } else { // A call to alloca with a variable size requires protectors. Layout.insert(std::make_pair(AI, SSPLK_LargeArray)); + ORE.emit(Remark << Reason.str()); NeedsProtector = true; } continue; @@ -266,6 +285,10 @@ if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) { Layout.insert(std::make_pair(AI, IsLarge ? SSPLK_LargeArray : SSPLK_SmallArray)); + ORE.emit(OptimizationRemark(DEBUG_TYPE, RemarkName, &I) + << (ReasonStub + + "a stack allocated buffer or struct containing a buffer") + .str()); NeedsProtector = true; continue; } @@ -273,6 +296,10 @@ if (Strong && HasAddressTaken(AI)) { ++NumAddrTaken; Layout.insert(std::make_pair(AI, SSPLK_AddrOf)); + ORE.emit( + OptimizationRemark(DEBUG_TYPE, RemarkName, &I) + << (ReasonStub + "the address of a local variable being taken") + .str()); NeedsProtector = true; } } Index: lib/IR/DiagnosticInfo.cpp =================================================================== --- lib/IR/DiagnosticInfo.cpp +++ lib/IR/DiagnosticInfo.cpp @@ -228,7 +228,7 @@ *cast(CodeRegion)->getParent(), Loc, CodeRegion) {} OptimizationRemark::OptimizationRemark(const char *PassName, - StringRef RemarkName, Instruction *Inst) + StringRef RemarkName, const Instruction *Inst) : DiagnosticInfoIROptimization(DK_OptimizationRemark, DS_Remark, PassName, RemarkName, *Inst->getParent()->getParent(), Inst->getDebugLoc(), Inst->getParent()) {}