blob: d10b4f4aaebc14ac41c71a4d4f3a83416c64272e [file] [log] [blame]
Chandler Carruthf49f1a872015-12-27 08:13:45 +00001//===- ForceFunctionAttrs.cpp - Force function attrs for debugging --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
11#include "llvm/ADT/StringSwitch.h"
12#include "llvm/IR/Function.h"
13#include "llvm/IR/LLVMContext.h"
14#include "llvm/IR/Module.h"
15#include "llvm/Support/Debug.h"
16#include "llvm/Support/raw_ostream.h"
17using namespace llvm;
18
19#define DEBUG_TYPE "forceattrs"
20
21static cl::list<std::string>
22 ForceAttributes("force-attribute", cl::Hidden,
23 cl::desc("Add an attribute to a function. This should be a "
24 "pair of 'function-name:attribute-name', for "
Weiming Zhaoc7c18d62016-01-06 18:18:16 +000025 "example -force-attribute=foo:noinline. This "
Chandler Carruthf49f1a872015-12-27 08:13:45 +000026 "option can be specified multiple times."));
27
28static Attribute::AttrKind parseAttrKind(StringRef Kind) {
29 return StringSwitch<Attribute::AttrKind>(Kind)
30 .Case("alwaysinline", Attribute::AlwaysInline)
31 .Case("builtin", Attribute::Builtin)
32 .Case("cold", Attribute::Cold)
33 .Case("convergent", Attribute::Convergent)
34 .Case("inlinehint", Attribute::InlineHint)
35 .Case("jumptable", Attribute::JumpTable)
36 .Case("minsize", Attribute::MinSize)
37 .Case("naked", Attribute::Naked)
38 .Case("nobuiltin", Attribute::NoBuiltin)
39 .Case("noduplicate", Attribute::NoDuplicate)
40 .Case("noimplicitfloat", Attribute::NoImplicitFloat)
41 .Case("noinline", Attribute::NoInline)
42 .Case("nonlazybind", Attribute::NonLazyBind)
43 .Case("noredzone", Attribute::NoRedZone)
44 .Case("noreturn", Attribute::NoReturn)
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +000045 .Case("nocf_check", Attribute::NoCfCheck)
Chandler Carruthf49f1a872015-12-27 08:13:45 +000046 .Case("norecurse", Attribute::NoRecurse)
47 .Case("nounwind", Attribute::NoUnwind)
48 .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)
55 .Case("sanitize_address", Attribute::SanitizeAddress)
Evgeniy Stepanovc667c1f2017-12-09 00:21:41 +000056 .Case("sanitize_hwaddress", Attribute::SanitizeHWAddress)
Chandler Carruthf49f1a872015-12-27 08:13:45 +000057 .Case("sanitize_memory", Attribute::SanitizeMemory)
58 .Case("sanitize_thread", Attribute::SanitizeThread)
59 .Case("ssp", Attribute::StackProtect)
60 .Case("sspreq", Attribute::StackProtectReq)
61 .Case("sspstrong", Attribute::StackProtectStrong)
Andrew Kaylor53a5fbb2017-08-14 21:15:13 +000062 .Case("strictfp", Attribute::StrictFP)
Chandler Carruthf49f1a872015-12-27 08:13:45 +000063 .Case("uwtable", Attribute::UWTable)
64 .Default(Attribute::None);
65}
66
67/// If F has any forced attributes given on the command line, add them.
68static void addForcedAttributes(Function &F) {
69 for (auto &S : ForceAttributes) {
70 auto KV = StringRef(S).split(':');
71 if (KV.first != F.getName())
72 continue;
73
74 auto Kind = parseAttrKind(KV.second);
75 if (Kind == Attribute::None) {
76 DEBUG(dbgs() << "ForcedAttribute: " << KV.second
77 << " unknown or not handled!\n");
78 continue;
79 }
80 if (F.hasFnAttribute(Kind))
81 continue;
82 F.addFnAttr(Kind);
83 }
84}
85
Chandler Carruth164a2aa62016-06-17 00:11:01 +000086PreservedAnalyses ForceFunctionAttrsPass::run(Module &M,
87 ModuleAnalysisManager &) {
Chandler Carruthf49f1a872015-12-27 08:13:45 +000088 if (ForceAttributes.empty())
89 return PreservedAnalyses::all();
90
91 for (Function &F : M.functions())
92 addForcedAttributes(F);
93
94 // Just conservatively invalidate analyses, this isn't likely to be important.
95 return PreservedAnalyses::none();
96}
97
98namespace {
99struct ForceFunctionAttrsLegacyPass : public ModulePass {
100 static char ID; // Pass identification, replacement for typeid
101 ForceFunctionAttrsLegacyPass() : ModulePass(ID) {
102 initializeForceFunctionAttrsLegacyPassPass(
103 *PassRegistry::getPassRegistry());
104 }
105
106 bool runOnModule(Module &M) override {
107 if (ForceAttributes.empty())
108 return false;
109
110 for (Function &F : M.functions())
111 addForcedAttributes(F);
112
113 // Conservatively assume we changed something.
114 return true;
115 }
116};
117}
118
119char ForceFunctionAttrsLegacyPass::ID = 0;
120INITIALIZE_PASS(ForceFunctionAttrsLegacyPass, "forceattrs",
121 "Force set function attributes", false, false)
122
123Pass *llvm::createForceFunctionAttrsLegacyPass() {
124 return new ForceFunctionAttrsLegacyPass();
125}