llvm.org GIT mirror llvm / 49ca55e
Transforms: Introduce Transforms/Utils.h rather than spreading the declarations amongst Scalar.h and IPO.h Fixes layering - Transforms/Utils shouldn't depend on including a Scalar or IPO header, because Scalar and IPO depend on Utils. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@328717 91177308-0d34-0410-b5e6-96231b3b80d8 David Blaikie 1 year, 3 months ago
43 changed file(s) with 359 addition(s) and 311 deletion(s). Raw diff Collapse all Expand all
0 #include "../include/KaleidoscopeJIT.h"
1 #include "llvm/ADT/APFloat.h"
12 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/IR/BasicBlock.h"
34 #include "llvm/IR/Constants.h"
45 #include "llvm/IR/DerivedTypes.h"
56 #include "llvm/IR/Function.h"
7 #include "llvm/IR/IRBuilder.h"
68 #include "llvm/IR/Instructions.h"
7 #include "llvm/IR/IRBuilder.h"
89 #include "llvm/IR/LLVMContext.h"
910 #include "llvm/IR/LegacyPassManager.h"
1011 #include "llvm/IR/Module.h"
1415 #include "llvm/Target/TargetMachine.h"
1516 #include "llvm/Transforms/Scalar.h"
1617 #include "llvm/Transforms/Scalar/GVN.h"
17 #include "../include/KaleidoscopeJIT.h"
18 #include "llvm/Transforms/Utils.h"
1819 #include
1920 #include
2021 #include
4646 #include "llvm/Transforms/ObjCARC.h"
4747 #include "llvm/Transforms/Scalar.h"
4848 #include "llvm/Transforms/Scalar/GVN.h"
49 #include "llvm/Transforms/Utils.h"
4950 #include "llvm/Transforms/Utils/SymbolRewriter.h"
5051 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
5152 #include "llvm/Transforms/Vectorize.h"
208208 /// createPartialInliningPass - This pass inlines parts of functions.
209209 ///
210210 ModulePass *createPartialInliningPass();
211
212 //===----------------------------------------------------------------------===//
213 // createMetaRenamerPass - Rename everything with metasyntatic names.
214 //
215 ModulePass *createMetaRenamerPass();
216211
217212 //===----------------------------------------------------------------------===//
218213 /// createBarrierNoopPass - This pass is purely a module pass barrier in a pass
222222
223223 //===----------------------------------------------------------------------===//
224224 //
225 // PromoteMemoryToRegister - This pass is used to promote memory references to
226 // be register references. A simple example of the transformation performed by
227 // this pass is:
228 //
229 // FROM CODE TO CODE
230 // %X = alloca i32, i32 1 ret i32 42
231 // store i32 42, i32 *%X
232 // %Y = load i32* %X
233 // ret i32 %Y
234 //
235 FunctionPass *createPromoteMemoryToRegisterPass();
236
237 //===----------------------------------------------------------------------===//
238 //
239225 // DemoteRegisterToMemoryPass - This pass is used to demote registers to memory
240226 // references. In basically undoes the PromoteMemoryToRegister pass to make cfg
241227 // hacking easier.
288274
289275 //===----------------------------------------------------------------------===//
290276 //
291 // BreakCriticalEdges - Break all of the critical edges in the CFG by inserting
292 // a dummy basic block. This pass may be "required" by passes that cannot deal
293 // with critical edges. For this usage, a pass must call:
294 //
295 // AU.addRequiredID(BreakCriticalEdgesID);
296 //
297 // This pass obviously invalidates the CFG, but can update forward dominator
298 // (set, immediate dominators, tree, and frontier) information.
299 //
300 FunctionPass *createBreakCriticalEdgesPass();
301 extern char &BreakCriticalEdgesID;
302
303 //===----------------------------------------------------------------------===//
304 //
305 // LoopSimplify - Insert Pre-header blocks into the CFG for every function in
306 // the module. This pass updates dominator information, loop information, and
307 // does not add critical edges to the CFG.
308 //
309 // AU.addRequiredID(LoopSimplifyID);
310 //
311 Pass *createLoopSimplifyPass();
312 extern char &LoopSimplifyID;
313
314 //===----------------------------------------------------------------------===//
315 //
316277 // TailCallElimination - This pass eliminates call instructions to the current
317278 // function which occur immediately before return instructions.
318279 //
320281
321282 //===----------------------------------------------------------------------===//
322283 //
323 // LowerSwitch - This pass converts SwitchInst instructions into a sequence of
324 // chained binary branch instructions.
325 //
326 FunctionPass *createLowerSwitchPass();
327 extern char &LowerSwitchID;
328
329 //===----------------------------------------------------------------------===//
330 //
331 // LowerInvoke - This pass removes invoke instructions, converting them to call
332 // instructions.
333 //
334 FunctionPass *createLowerInvokePass();
335 extern char &LowerInvokePassID;
336
337 //===----------------------------------------------------------------------===//
338 //
339 // LCSSA - This pass inserts phi nodes at loop boundaries to simplify other loop
340 // optimizations.
341 //
342 Pass *createLCSSAPass();
343 extern char &LCSSAID;
344
345 //===----------------------------------------------------------------------===//
346 //
347284 // EarlyCSE - This pass performs a simple and fast CSE pass over the dominator
348285 // tree.
349286 //
402339 // ConstantHoisting - This pass prepares a function for expensive constants.
403340 //
404341 FunctionPass *createConstantHoistingPass();
405
406 //===----------------------------------------------------------------------===//
407 //
408 // InstructionNamer - Give any unnamed non-void instructions "tmp" names.
409 //
410 FunctionPass *createInstructionNamerPass();
411 extern char &InstructionNamerID;
412342
413343 //===----------------------------------------------------------------------===//
414344 //
451381
452382 //===----------------------------------------------------------------------===//
453383 //
454 // InstructionSimplifier - Remove redundant instructions.
455 //
456 FunctionPass *createInstructionSimplifierPass();
457 extern char &InstructionSimplifierID;
458
459 //===----------------------------------------------------------------------===//
460 //
461384 // LowerExpectIntrinsics - Removes llvm.expect intrinsics and creates
462385 // "block_weights" metadata.
463386 FunctionPass *createLowerExpectIntrinsicPass();
474397 // ScalarizerPass - Converts vector operations into scalar operations
475398 //
476399 FunctionPass *createScalarizerPass();
477
478 //===----------------------------------------------------------------------===//
479 //
480 // AddDiscriminators - Add DWARF path discriminators to the IR.
481 FunctionPass *createAddDiscriminatorsPass();
482400
483401 //===----------------------------------------------------------------------===//
484402 //
549467
550468 //===----------------------------------------------------------------------===//
551469 //
552 // LoopSimplifyCFG - This pass performs basic CFG simplification on loops,
553 // primarily to help other loop passes.
554 //
555 Pass *createLoopSimplifyCFGPass();
556
557 //===----------------------------------------------------------------------===//
558 //
559470 // LoopVersioning - Perform loop multi-versioning.
560471 //
561472 FunctionPass *createLoopVersioningPass();
576487 //
577488 FunctionPass *createLibCallsShrinkWrapPass();
578489
579 //===----------------------------------------------------------------------===//
580 //
581 // EntryExitInstrumenter pass - Instrument function entry/exit with calls to
582 // mcount(), @__cyg_profile_func_{enter,exit} and the like. There are two
583 // variants, intended to run pre- and post-inlining, respectively.
584 //
585 FunctionPass *createEntryExitInstrumenterPass();
586 FunctionPass *createPostInlineEntryExitInstrumenterPass();
587
588490 } // End llvm namespace
589491
590492 #endif
0 //===- llvm/Transforms/Utils.h - Utility Transformations --------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header file defines prototypes for accessor functions that expose passes
10 // in the Utils transformations library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TRANSFORMS_UTILS_H
15 #define LLVM_TRANSFORMS_UTILS_H
16
17 namespace llvm {
18
19 class ModulePass;
20 class FunctionPass;
21 class Pass;
22
23 //===----------------------------------------------------------------------===//
24 // createMetaRenamerPass - Rename everything with metasyntatic names.
25 //
26 ModulePass *createMetaRenamerPass();
27
28 //===----------------------------------------------------------------------===//
29 //
30 // LowerInvoke - This pass removes invoke instructions, converting them to call
31 // instructions.
32 //
33 FunctionPass *createLowerInvokePass();
34 extern char &LowerInvokePassID;
35
36 //===----------------------------------------------------------------------===//
37 //
38 // InstructionNamer - Give any unnamed non-void instructions "tmp" names.
39 //
40 FunctionPass *createInstructionNamerPass();
41 extern char &InstructionNamerID;
42
43 //===----------------------------------------------------------------------===//
44 //
45 // LowerSwitch - This pass converts SwitchInst instructions into a sequence of
46 // chained binary branch instructions.
47 //
48 FunctionPass *createLowerSwitchPass();
49 extern char &LowerSwitchID;
50
51 //===----------------------------------------------------------------------===//
52 //
53 // EntryExitInstrumenter pass - Instrument function entry/exit with calls to
54 // mcount(), @__cyg_profile_func_{enter,exit} and the like. There are two
55 // variants, intended to run pre- and post-inlining, respectively.
56 //
57 FunctionPass *createEntryExitInstrumenterPass();
58 FunctionPass *createPostInlineEntryExitInstrumenterPass();
59
60 //===----------------------------------------------------------------------===//
61 //
62 // BreakCriticalEdges - Break all of the critical edges in the CFG by inserting
63 // a dummy basic block. This pass may be "required" by passes that cannot deal
64 // with critical edges. For this usage, a pass must call:
65 //
66 // AU.addRequiredID(BreakCriticalEdgesID);
67 //
68 // This pass obviously invalidates the CFG, but can update forward dominator
69 // (set, immediate dominators, tree, and frontier) information.
70 //
71 FunctionPass *createBreakCriticalEdgesPass();
72 extern char &BreakCriticalEdgesID;
73
74 //===----------------------------------------------------------------------===//
75 //
76 // LCSSA - This pass inserts phi nodes at loop boundaries to simplify other loop
77 // optimizations.
78 //
79 Pass *createLCSSAPass();
80 extern char &LCSSAID;
81
82 //===----------------------------------------------------------------------===//
83 //
84 // AddDiscriminators - Add DWARF path discriminators to the IR.
85 FunctionPass *createAddDiscriminatorsPass();
86
87 //===----------------------------------------------------------------------===//
88 //
89 // PromoteMemoryToRegister - This pass is used to promote memory references to
90 // be register references. A simple example of the transformation performed by
91 // this pass is:
92 //
93 // FROM CODE TO CODE
94 // %X = alloca i32, i32 1 ret i32 42
95 // store i32 42, i32 *%X
96 // %Y = load i32* %X
97 // ret i32 %Y
98 //
99 FunctionPass *createPromoteMemoryToRegisterPass();
100
101 //===----------------------------------------------------------------------===//
102 //
103 // LoopSimplify - Insert Pre-header blocks into the CFG for every function in
104 // the module. This pass updates dominator information, loop information, and
105 // does not add critical edges to the CFG.
106 //
107 // AU.addRequiredID(LoopSimplifyID);
108 //
109 Pass *createLoopSimplifyPass();
110 extern char &LoopSimplifyID;
111
112 //===----------------------------------------------------------------------===//
113 //
114 // LoopSimplifyCFG - This pass performs basic CFG simplification on loops,
115 // primarily to help other loop passes.
116 //
117 Pass *createLoopSimplifyCFGPass();
118
119 //===----------------------------------------------------------------------===//
120 //
121 // InstructionSimplifier - Remove redundant instructions.
122 //
123 FunctionPass *createInstructionSimplifierPass();
124 extern char &InstructionSimplifierID;
125 }
126
127 #endif
4040 #include "llvm/Support/Threading.h"
4141 #include "llvm/Target/TargetMachine.h"
4242 #include "llvm/Transforms/Scalar.h"
43 #include "llvm/Transforms/Utils.h"
4344 #include "llvm/Transforms/Utils/SymbolRewriter.h"
4445 #include
4546 #include
3939 #include "llvm/Pass.h"
4040 #include "llvm/Support/Casting.h"
4141 #include "llvm/Transforms/Scalar.h"
42 #include "llvm/Transforms/Utils.h"
4243
4344 using namespace llvm;
4445
5656 #include "llvm/Support/KnownBits.h"
5757 #include "llvm/Support/raw_ostream.h"
5858 #include "llvm/Transforms/Scalar.h"
59 #include "llvm/Transforms/Utils.h"
5960 #include
6061 #include
6162 #include
137137 #include "llvm/Support/Debug.h"
138138 #include "llvm/Support/raw_ostream.h"
139139 #include "llvm/Transforms/Scalar.h"
140 #include "llvm/Transforms/Utils.h"
140141 #include
141142 #include
142143 #include
5050 #include "llvm/Support/Debug.h"
5151 #include "llvm/Support/raw_ostream.h"
5252 #include "llvm/Transforms/Scalar.h"
53 #include "llvm/Transforms/Utils.h"
5354 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
5455 #include "llvm/Transforms/Utils/LoopUtils.h"
5556
4747 #include "llvm/Support/CommandLine.h"
4848 #include "llvm/Support/Debug.h"
4949 #include "llvm/Transforms/Scalar.h"
50 #include "llvm/Transforms/Utils.h"
5051 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
5152 #include "llvm/Transforms/Utils/LoopUtils.h"
5253 #include
2424 #include "llvm/Support/TargetRegistry.h"
2525 #include "llvm/Target/TargetOptions.h"
2626 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Transforms/Utils.h"
2728 using namespace llvm;
2829
2930 #define DEBUG_TYPE "wasm"
2323 LoopExtractor.cpp
2424 LowerTypeTests.cpp
2525 MergeFunctions.cpp
26 MetaRenamer.cpp
2726 PartialInlining.cpp
2827 PassManagerBuilder.cpp
2928 PruneEH.cpp
5858 initializeSampleProfileLoaderLegacyPassPass(Registry);
5959 initializeFunctionImportLegacyPassPass(Registry);
6060 initializeWholeProgramDevirtPass(Registry);
61 initializeMetaRenamerPass(Registry);
6261 }
6362
6463 void LLVMInitializeIPO(LLVMPassRegistryRef R) {
2222 #include "llvm/Support/CommandLine.h"
2323 #include "llvm/Transforms/IPO.h"
2424 #include "llvm/Transforms/Scalar.h"
25 #include "llvm/Transforms/Utils.h"
2526 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
2627 #include "llvm/Transforms/Utils/CodeExtractor.h"
2728 #include
+0
-178
lib/Transforms/IPO/MetaRenamer.cpp less more
None //===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass renames everything with metasyntatic names. The intent is to use
10 // this pass after bugpoint reduction to conceal the nature of the original
11 // program.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Analysis/TargetLibraryInfo.h"
20 #include "llvm/IR/Argument.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/GlobalAlias.h"
25 #include "llvm/IR/GlobalVariable.h"
26 #include "llvm/IR/Instruction.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/IR/TypeFinder.h"
30 #include "llvm/Pass.h"
31 #include "llvm/Transforms/IPO.h"
32
33 using namespace llvm;
34
35 static const char *const metaNames[] = {
36 // See http://en.wikipedia.org/wiki/Metasyntactic_variable
37 "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
38 "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
39 };
40
41 namespace {
42
43 // This PRNG is from the ISO C spec. It is intentionally simple and
44 // unsuitable for cryptographic use. We're just looking for enough
45 // variety to surprise and delight users.
46 struct PRNG {
47 unsigned long next;
48
49 void srand(unsigned int seed) {
50 next = seed;
51 }
52
53 int rand() {
54 next = next * 1103515245 + 12345;
55 return (unsigned int)(next / 65536) % 32768;
56 }
57 };
58
59 struct Renamer {
60 Renamer(unsigned int seed) {
61 prng.srand(seed);
62 }
63
64 const char *newName() {
65 return metaNames[prng.rand() % array_lengthof(metaNames)];
66 }
67
68 PRNG prng;
69 };
70
71 struct MetaRenamer : public ModulePass {
72 // Pass identification, replacement for typeid
73 static char ID;
74
75 MetaRenamer() : ModulePass(ID) {
76 initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
77 }
78
79 void getAnalysisUsage(AnalysisUsage &AU) const override {
80 AU.addRequired();
81 AU.setPreservesAll();
82 }
83
84 bool runOnModule(Module &M) override {
85 // Seed our PRNG with simple additive sum of ModuleID. We're looking to
86 // simply avoid always having the same function names, and we need to
87 // remain deterministic.
88 unsigned int randSeed = 0;
89 for (auto C : M.getModuleIdentifier())
90 randSeed += C;
91
92 Renamer renamer(randSeed);
93
94 // Rename all aliases
95 for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) {
96 StringRef Name = AI->getName();
97 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
98 continue;
99
100 AI->setName("alias");
101 }
102
103 // Rename all global variables
104 for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) {
105 StringRef Name = GI->getName();
106 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
107 continue;
108
109 GI->setName("global");
110 }
111
112 // Rename all struct types
113 TypeFinder StructTypes;
114 StructTypes.run(M, true);
115 for (StructType *STy : StructTypes) {
116 if (STy->isLiteral() || STy->getName().empty()) continue;
117
118 SmallString<128> NameStorage;
119 STy->setName((Twine("struct.") +
120 renamer.newName()).toStringRef(NameStorage));
121 }
122
123 // Rename all functions
124 const TargetLibraryInfo &TLI =
125 getAnalysis().getTLI();
126 for (auto &F : M) {
127 StringRef Name = F.getName();
128 LibFunc Tmp;
129 // Leave library functions alone because their presence or absence could
130 // affect the behavior of other passes.
131 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
132 TLI.getLibFunc(F, Tmp))
133 continue;
134
135 // Leave @main alone. The output of -metarenamer might be passed to
136 // lli for execution and the latter needs a main entry point.
137 if (Name != "main")
138 F.setName(renamer.newName());
139
140 runOnFunction(F);
141 }
142 return true;
143 }
144
145 bool runOnFunction(Function &F) {
146 for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI)
147 if (!AI->getType()->isVoidTy())
148 AI->setName("arg");
149
150 for (auto &BB : F) {
151 BB.setName("bb");
152
153 for (auto &I : BB)
154 if (!I.getType()->isVoidTy())
155 I.setName("tmp");
156 }
157 return true;
158 }
159 };
160
161 } // end anonymous namespace
162
163 char MetaRenamer::ID = 0;
164
165 INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
166 "Assign new names to everything", false, false)
167 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
168 INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
169 "Assign new names to everything", false, false)
170
171 //===----------------------------------------------------------------------===//
172 //
173 // MetaRenamer - Rename everything with metasyntactic names.
174 //
175 ModulePass *llvm::createMetaRenamerPass() {
176 return new MetaRenamer();
177 }
3636 #include "llvm/Transforms/Scalar.h"
3737 #include "llvm/Transforms/Scalar/GVN.h"
3838 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
39 #include "llvm/Transforms/Utils.h"
3940 #include "llvm/Transforms/Vectorize.h"
4041
4142 using namespace llvm;
3939 #include "llvm/Support/ErrorHandling.h"
4040 #include "llvm/Support/raw_ostream.h"
4141 #include "llvm/Transforms/Scalar.h"
42 #include "llvm/Transforms/Utils.h"
4243 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
4344 #include "llvm/Transforms/Utils/LoopUtils.h"
4445 #include
5151 #include "llvm/Support/Debug.h"
5252 #include "llvm/Support/raw_ostream.h"
5353 #include "llvm/Transforms/Scalar.h"
54 #include "llvm/Transforms/Utils.h"
5455 #include "llvm/Transforms/Utils/LoopVersioning.h"
5556 #include
5657 #include
5151 #include "llvm/Support/Debug.h"
5252 #include "llvm/Support/raw_ostream.h"
5353 #include "llvm/Transforms/Scalar.h"
54 #include "llvm/Transforms/Utils.h"
5455 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
5556 #include "llvm/Transforms/Utils/LoopUtils.h"
5657 #include
3030 #include "llvm/IR/Dominators.h"
3131 #include "llvm/Transforms/Scalar.h"
3232 #include "llvm/Transforms/Scalar/LoopPassManager.h"
33 #include "llvm/Transforms/Utils.h"
3334 #include "llvm/Transforms/Utils/LoopUtils.h"
3435 using namespace llvm;
3536
105105 #include "llvm/Support/MathExtras.h"
106106 #include "llvm/Support/raw_ostream.h"
107107 #include "llvm/Transforms/Scalar.h"
108 #include "llvm/Transforms/Utils.h"
108109 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
109110 #include
110111 #include
5252 #include "llvm/Support/raw_ostream.h"
5353 #include "llvm/Transforms/Scalar.h"
5454 #include "llvm/Transforms/Scalar/LoopPassManager.h"
55 #include "llvm/Transforms/Utils.h"
5556 #include "llvm/Transforms/Utils/LoopSimplify.h"
5657 #include "llvm/Transforms/Utils/LoopUtils.h"
5758 #include "llvm/Transforms/Utils/UnrollLoop.h"
8585 #include "llvm/Support/Debug.h"
8686 #include "llvm/Support/raw_ostream.h"
8787 #include "llvm/Transforms/Scalar.h"
88 #include "llvm/Transforms/Utils.h"
8889 #include "llvm/Transforms/Utils/LoopUtils.h"
8990 #include "llvm/Transforms/Utils/LoopVersioning.h"
9091 #include
2525 #include "llvm/IR/Module.h"
2626 #include "llvm/Pass.h"
2727 #include "llvm/Transforms/Scalar.h"
28 #include "llvm/Transforms/Utils.h"
2829 #include
2930 using namespace llvm;
3031
177177 unwrap(PM)->add(createLoopRerollPass());
178178 }
179179
180 void LLVMAddLoopSimplifyCFGPass(LLVMPassManagerRef PM) {
181 unwrap(PM)->add(createLoopSimplifyCFGPass());
182 }
183
184180 void LLVMAddLoopUnrollPass(LLVMPassManagerRef PM) {
185181 unwrap(PM)->add(createLoopUnrollPass());
186182 }
195191
196192 void LLVMAddPartiallyInlineLibCallsPass(LLVMPassManagerRef PM) {
197193 unwrap(PM)->add(createPartiallyInlineLibCallsPass());
198 }
199
200 void LLVMAddLowerSwitchPass(LLVMPassManagerRef PM) {
201 unwrap(PM)->add(createLowerSwitchPass());
202 }
203
204 void LLVMAddPromoteMemoryToRegisterPass(LLVMPassManagerRef PM) {
205 unwrap(PM)->add(createPromoteMemoryToRegisterPass());
206194 }
207195
208196 void LLVMAddReassociatePass(LLVMPassManagerRef PM) {
3939 #include "llvm/Support/ErrorHandling.h"
4040 #include "llvm/Support/raw_ostream.h"
4141 #include "llvm/Transforms/Scalar.h"
42 #include "llvm/Transforms/Utils.h"
4243 #include "llvm/Transforms/Utils/SSAUpdater.h"
4344 #include
4445 #include
6868 #include "llvm/Support/CommandLine.h"
6969 #include "llvm/Support/Debug.h"
7070 #include "llvm/Support/raw_ostream.h"
71 #include "llvm/Transforms/Scalar.h"
71 #include "llvm/Transforms/Utils.h"
7272 #include
7373
7474 using namespace llvm;
2727 #include "llvm/IR/Instructions.h"
2828 #include "llvm/IR/Type.h"
2929 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Transforms/Scalar.h"
30 #include "llvm/Transforms/Utils.h"
3131 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
3232 #include "llvm/Transforms/Utils/Cloning.h"
3333 #include "llvm/Transforms/Utils/ValueMapper.h"
3434 LowerMemIntrinsics.cpp
3535 LowerSwitch.cpp
3636 Mem2Reg.cpp
37 MetaRenamer.cpp
3738 ModuleUtils.cpp
3839 NameAnonGlobals.cpp
3940 OrderedInstructions.cpp
1414 #include "llvm/IR/Module.h"
1515 #include "llvm/IR/Type.h"
1616 #include "llvm/Pass.h"
17 #include "llvm/Transforms/Scalar.h"
17 #include "llvm/Transforms/Utils.h"
1818 using namespace llvm;
1919
2020 static void insertCall(Function &CurFn, StringRef Func,
1616 #include "llvm/IR/Function.h"
1717 #include "llvm/IR/Type.h"
1818 #include "llvm/Pass.h"
19 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/Transforms/Utils.h"
2020 using namespace llvm;
2121
2222 namespace {
4242 #include "llvm/IR/Instructions.h"
4343 #include "llvm/IR/PredIteratorCache.h"
4444 #include "llvm/Pass.h"
45 #include "llvm/Transforms/Scalar.h"
45 #include "llvm/Transforms/Utils.h"
4646 #include "llvm/Transforms/Utils/LoopUtils.h"
4747 #include "llvm/Transforms/Utils/SSAUpdater.h"
4848 using namespace llvm;
6464 #include "llvm/IR/Type.h"
6565 #include "llvm/Support/Debug.h"
6666 #include "llvm/Support/raw_ostream.h"
67 #include "llvm/Transforms/Scalar.h"
67 #include "llvm/Transforms/Utils.h"
6868 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
6969 #include "llvm/Transforms/Utils/LoopUtils.h"
7070 using namespace llvm;
2020 //
2121 //===----------------------------------------------------------------------===//
2222
23 #include "llvm/ADT/SmallSet.h"
2324 #include "llvm/ADT/Statistic.h"
24 #include "llvm/ADT/SmallSet.h"
2525 #include "llvm/Analysis/AliasAnalysis.h"
2626 #include "llvm/Analysis/LoopIterator.h"
2727 #include "llvm/Analysis/ScalarEvolution.h"
3232 #include "llvm/IR/Module.h"
3333 #include "llvm/Support/Debug.h"
3434 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Transforms/Scalar.h"
35 #include "llvm/Transforms/Utils.h"
3636 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
3737 #include "llvm/Transforms/Utils/Cloning.h"
3838 #include "llvm/Transforms/Utils/LoopUtils.h"
2020 #include "llvm/IR/LLVMContext.h"
2121 #include "llvm/IR/Module.h"
2222 #include "llvm/Pass.h"
23 #include "llvm/Transforms/Scalar.h"
23 #include "llvm/Transforms/Utils.h"
2424 using namespace llvm;
2525
2626 #define DEBUG_TYPE "lowerinvoke"
2828 #include "llvm/Support/Compiler.h"
2929 #include "llvm/Support/Debug.h"
3030 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Transforms/Scalar.h"
31 #include "llvm/Transforms/Utils.h"
3232 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
3333 #include
3434 #include
2121 #include "llvm/IR/PassManager.h"
2222 #include "llvm/Pass.h"
2323 #include "llvm/Support/Casting.h"
24 #include "llvm/Transforms/Scalar.h"
24 #include "llvm/Transforms/Utils.h"
2525 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
2626 #include
2727
0 //===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass renames everything with metasyntatic names. The intent is to use
10 // this pass after bugpoint reduction to conceal the nature of the original
11 // program.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Analysis/TargetLibraryInfo.h"
20 #include "llvm/IR/Argument.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/GlobalAlias.h"
25 #include "llvm/IR/GlobalVariable.h"
26 #include "llvm/IR/Instruction.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/IR/TypeFinder.h"
30 #include "llvm/Pass.h"
31 #include "llvm/Transforms/Utils.h"
32
33 using namespace llvm;
34
35 static const char *const metaNames[] = {
36 // See http://en.wikipedia.org/wiki/Metasyntactic_variable
37 "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
38 "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
39 };
40
41 namespace {
42
43 // This PRNG is from the ISO C spec. It is intentionally simple and
44 // unsuitable for cryptographic use. We're just looking for enough
45 // variety to surprise and delight users.
46 struct PRNG {
47 unsigned long next;
48
49 void srand(unsigned int seed) {
50 next = seed;
51 }
52
53 int rand() {
54 next = next * 1103515245 + 12345;
55 return (unsigned int)(next / 65536) % 32768;
56 }
57 };
58
59 struct Renamer {
60 Renamer(unsigned int seed) {
61 prng.srand(seed);
62 }
63
64 const char *newName() {
65 return metaNames[prng.rand() % array_lengthof(metaNames)];
66 }
67
68 PRNG prng;
69 };
70
71 struct MetaRenamer : public ModulePass {
72 // Pass identification, replacement for typeid
73 static char ID;
74
75 MetaRenamer() : ModulePass(ID) {
76 initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
77 }
78
79 void getAnalysisUsage(AnalysisUsage &AU) const override {
80 AU.addRequired();
81 AU.setPreservesAll();
82 }
83
84 bool runOnModule(Module &M) override {
85 // Seed our PRNG with simple additive sum of ModuleID. We're looking to
86 // simply avoid always having the same function names, and we need to
87 // remain deterministic.
88 unsigned int randSeed = 0;
89 for (auto C : M.getModuleIdentifier())
90 randSeed += C;
91
92 Renamer renamer(randSeed);
93
94 // Rename all aliases
95 for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) {
96 StringRef Name = AI->getName();
97 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
98 continue;
99
100 AI->setName("alias");
101 }
102
103 // Rename all global variables
104 for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) {
105 StringRef Name = GI->getName();
106 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
107 continue;
108
109 GI->setName("global");
110 }
111
112 // Rename all struct types
113 TypeFinder StructTypes;
114 StructTypes.run(M, true);
115 for (StructType *STy : StructTypes) {
116 if (STy->isLiteral() || STy->getName().empty()) continue;
117
118 SmallString<128> NameStorage;
119 STy->setName((Twine("struct.") +
120 renamer.newName()).toStringRef(NameStorage));
121 }
122
123 // Rename all functions
124 const TargetLibraryInfo &TLI =
125 getAnalysis().getTLI();
126 for (auto &F : M) {
127 StringRef Name = F.getName();
128 LibFunc Tmp;
129 // Leave library functions alone because their presence or absence could
130 // affect the behavior of other passes.
131 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
132 TLI.getLibFunc(F, Tmp))
133 continue;
134
135 // Leave @main alone. The output of -metarenamer might be passed to
136 // lli for execution and the latter needs a main entry point.
137 if (Name != "main")
138 F.setName(renamer.newName());
139
140 runOnFunction(F);
141 }
142 return true;
143 }
144
145 bool runOnFunction(Function &F) {
146 for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI)
147 if (!AI->getType()->isVoidTy())
148 AI->setName("arg");
149
150 for (auto &BB : F) {
151 BB.setName("bb");
152
153 for (auto &I : BB)
154 if (!I.getType()->isVoidTy())
155 I.setName("tmp");
156 }
157 return true;
158 }
159 };
160
161 } // end anonymous namespace
162
163 char MetaRenamer::ID = 0;
164
165 INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
166 "Assign new names to everything", false, false)
167 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
168 INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
169 "Assign new names to everything", false, false)
170
171 //===----------------------------------------------------------------------===//
172 //
173 // MetaRenamer - Rename everything with metasyntactic names.
174 //
175 ModulePass *llvm::createMetaRenamerPass() {
176 return new MetaRenamer();
177 }
3131 #include "llvm/Support/Debug.h"
3232 #include "llvm/Support/DebugCounter.h"
3333 #include "llvm/Support/FormattedStream.h"
34 #include "llvm/Transforms/Scalar.h"
34 #include "llvm/Transforms/Utils.h"
3535 #include "llvm/Transforms/Utils/OrderedInstructions.h"
3636 #include
3737 #define DEBUG_TYPE "predicateinfo"
2727 #include "llvm/IR/Function.h"
2828 #include "llvm/IR/Type.h"
2929 #include "llvm/Pass.h"
30 #include "llvm/Transforms/Scalar.h"
30 #include "llvm/Transforms/Utils.h"
3131 using namespace llvm;
3232
3333 #define DEBUG_TYPE "instsimplify"
1818 #include "llvm/IR/Function.h"
1919 #include "llvm/IR/Instructions.h"
2020 #include "llvm/IR/Type.h"
21 #include "llvm/Transforms/Scalar.h"
21 #include "llvm/Transforms/Utils.h"
2222 using namespace llvm;
2323
2424 char UnifyFunctionExitNodes::ID = 0;
1111 //
1212 //===----------------------------------------------------------------------===//
1313
14 #include "llvm/Transforms/Utils.h"
1415 #include "llvm-c/Initialization.h"
16 #include "llvm/IR/LegacyPassManager.h"
1517 #include "llvm/InitializePasses.h"
1618 #include "llvm/PassRegistry.h"
1719
3335 initializeStripNonLineTableDebugInfoPass(Registry);
3436 initializeUnifyFunctionExitNodesPass(Registry);
3537 initializeInstSimplifierPass(Registry);
38 initializeMetaRenamerPass(Registry);
3639 initializeStripGCRelocatesPass(Registry);
3740 initializePredicateInfoPrinterLegacyPassPass(Registry);
3841 }
4144 void LLVMInitializeTransformUtils(LLVMPassRegistryRef R) {
4245 initializeTransformUtils(*unwrap(R));
4346 }
47
48 void LLVMAddLoopSimplifyCFGPass(LLVMPassManagerRef PM) {
49 unwrap(PM)->add(createLoopSimplifyCFGPass());
50 }
51
52 void LLVMAddLowerSwitchPass(LLVMPassManagerRef PM) {
53 unwrap(PM)->add(createLowerSwitchPass());
54 }
55
56 void LLVMAddPromoteMemoryToRegisterPass(LLVMPassManagerRef PM) {
57 unwrap(PM)->add(createPromoteMemoryToRegisterPass());
58 }
59