blob: cd1fc3798201fb465df2e7a0eb13e35115409c8d [file] [log] [blame]
Chandler Carruthf49f1a872015-12-27 08:13:45 +00001//===- ForceFunctionAttrs.cpp - Force function attrs for debugging --------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chandler Carruthf49f1a872015-12-27 08:13:45 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
10#include "llvm/ADT/StringSwitch.h"
11#include "llvm/IR/Function.h"
12#include "llvm/IR/LLVMContext.h"
13#include "llvm/IR/Module.h"
14#include "llvm/Support/Debug.h"
15#include "llvm/Support/raw_ostream.h"
16using namespace llvm;
17
18#define DEBUG_TYPE "forceattrs"
19
20static cl::list<std::string>
21 ForceAttributes("force-attribute", cl::Hidden,
22 cl::desc("Add an attribute to a function. This should be a "
23 "pair of 'function-name:attribute-name', for "
Weiming Zhaoc7c18d62016-01-06 18:18:16 +000024 "example -force-attribute=foo:noinline. This "
Chandler Carruthf49f1a872015-12-27 08:13:45 +000025 "option can be specified multiple times."));
26
27static Attribute::AttrKind parseAttrKind(StringRef Kind) {
28 return StringSwitch<Attribute::AttrKind>(Kind)
29 .Case("alwaysinline", Attribute::AlwaysInline)
30 .Case("builtin", Attribute::Builtin)
31 .Case("cold", Attribute::Cold)
32 .Case("convergent", Attribute::Convergent)
33 .Case("inlinehint", Attribute::InlineHint)
34 .Case("jumptable", Attribute::JumpTable)
35 .Case("minsize", Attribute::MinSize)
36 .Case("naked", Attribute::Naked)
37 .Case("nobuiltin", Attribute::NoBuiltin)
38 .Case("noduplicate", Attribute::NoDuplicate)
39 .Case("noimplicitfloat", Attribute::NoImplicitFloat)
40 .Case("noinline", Attribute::NoInline)
41 .Case("nonlazybind", Attribute::NonLazyBind)
42 .Case("noredzone", Attribute::NoRedZone)
43 .Case("noreturn", Attribute::NoReturn)
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +000044 .Case("nocf_check", Attribute::NoCfCheck)
Chandler Carruthf49f1a872015-12-27 08:13:45 +000045 .Case("norecurse", Attribute::NoRecurse)
46 .Case("nounwind", Attribute::NoUnwind)
Matt Morehouse236cdaf2018-03-22 17:07:51 +000047 .Case("optforfuzzing", Attribute::OptForFuzzing)
Chandler Carruthf49f1a872015-12-27 08:13:45 +000048 .Case("optnone", Attribute::OptimizeNone)
49 .Case("optsize", Attribute::OptimizeForSize)
50 .Case("readnone", Attribute::ReadNone)
51 .Case("readonly", Attribute::ReadOnly)
52 .Case("argmemonly", Attribute::ArgMemOnly)
53 .Case("returns_twice", Attribute::ReturnsTwice)
54 .Case("safestack", Attribute::SafeStack)
Vlad Tsyrklevich07cf78c2018-04-03 21:40:27 +000055 .Case("shadowcallstack", Attribute::ShadowCallStack)
Chandler Carruthf49f1a872015-12-27 08:13:45 +000056 .Case("sanitize_address", Attribute::SanitizeAddress)
Evgeniy Stepanovc667c1f2017-12-09 00:21:41 +000057 .Case("sanitize_hwaddress", Attribute::SanitizeHWAddress)
Chandler Carruthf49f1a872015-12-27 08:13:45 +000058 .Case("sanitize_memory", Attribute::SanitizeMemory)
59 .Case("sanitize_thread", Attribute::SanitizeThread)
Chandler Carruth664aa862018-09-04 12:38:00 +000060 .Case("speculative_load_hardening", Attribute::SpeculativeLoadHardening)
Chandler Carruthf49f1a872015-12-27 08:13:45 +000061 .Case("ssp", Attribute::StackProtect)
62 .Case("sspreq", Attribute::StackProtectReq)
63 .Case("sspstrong", Attribute::StackProtectStrong)
Andrew Kaylor53a5fbb2017-08-14 21:15:13 +000064 .Case("strictfp", Attribute::StrictFP)
Chandler Carruthf49f1a872015-12-27 08:13:45 +000065 .Case("uwtable", Attribute::UWTable)
66 .Default(Attribute::None);
67}
68
69/// If F has any forced attributes given on the command line, add them.
70static void addForcedAttributes(Function &F) {
71 for (auto &S : ForceAttributes) {
72 auto KV = StringRef(S).split(':');
73 if (KV.first != F.getName())
74 continue;
75
76 auto Kind = parseAttrKind(KV.second);
77 if (Kind == Attribute::None) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000078 LLVM_DEBUG(dbgs() << "ForcedAttribute: " << KV.second
79 << " unknown or not handled!\n");
Chandler Carruthf49f1a872015-12-27 08:13:45 +000080 continue;
81 }
82 if (F.hasFnAttribute(Kind))
83 continue;
84 F.addFnAttr(Kind);
85 }
86}
87
Chandler Carruth164a2aa62016-06-17 00:11:01 +000088PreservedAnalyses ForceFunctionAttrsPass::run(Module &M,
89 ModuleAnalysisManager &) {
Chandler Carruthf49f1a872015-12-27 08:13:45 +000090 if (ForceAttributes.empty())
91 return PreservedAnalyses::all();
92
93 for (Function &F : M.functions())
94 addForcedAttributes(F);
95
96 // Just conservatively invalidate analyses, this isn't likely to be important.
97 return PreservedAnalyses::none();
98}
99
100namespace {
101struct ForceFunctionAttrsLegacyPass : public ModulePass {
102 static char ID; // Pass identification, replacement for typeid
103 ForceFunctionAttrsLegacyPass() : ModulePass(ID) {
104 initializeForceFunctionAttrsLegacyPassPass(
105 *PassRegistry::getPassRegistry());
106 }
107
108 bool runOnModule(Module &M) override {
109 if (ForceAttributes.empty())
110 return false;
111
112 for (Function &F : M.functions())
113 addForcedAttributes(F);
114
115 // Conservatively assume we changed something.
116 return true;
117 }
118};
119}
120
121char ForceFunctionAttrsLegacyPass::ID = 0;
122INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs",
123 "Force set function attributes", false, false)
124
125Pass *llvm::createForceFunctionAttrsLegacyPass() {
126 return new ForceFunctionAttrsLegacyPass();
127}