@@ -40,21 +40,13 @@ namespace {
40
40
// / IRNormalizer aims to transform LLVM IR into normal form.
41
41
class IRNormalizer {
42
42
public:
43
- // / \name Normalizer flags.
44
- // / @{
45
- // / Preserves original order of instructions.
46
- static cl::opt<bool > PreserveOrder;
47
- // / Renames all instructions (including user-named).
48
- static cl::opt<bool > RenameAll; // TODO: Don't rename on empty name
49
- // / Folds all regular instructions (including pre-outputs).
50
- static cl::opt<bool > FoldPreOutputs;
51
- // / Sorts and reorders operands in commutative instructions.
52
- static cl::opt<bool > ReorderOperands;
53
- // / @}
54
-
55
43
bool runOnFunction (Function &F);
56
44
45
+ IRNormalizer (IRNormalizerOptions Options) : Options(Options) {}
46
+
57
47
private:
48
+ const IRNormalizerOptions Options;
49
+
58
50
// Random constant for hashing, so the state isn't zero.
59
51
const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL ;
60
52
DenseSet<const Instruction *> NamedInstructions;
@@ -96,19 +88,6 @@ class IRNormalizer {
96
88
};
97
89
} // namespace
98
90
99
- cl::opt<bool > IRNormalizer::PreserveOrder (
100
- " norm-preserve-order" , cl::Hidden, cl::init(false ),
101
- cl::desc(" Preserves original instruction order" ));
102
- cl::opt<bool > IRNormalizer::RenameAll (
103
- " norm-rename-all" , cl::Hidden, cl::init(true ),
104
- cl::desc(" Renames all instructions (including user-named)" ));
105
- cl::opt<bool > IRNormalizer::FoldPreOutputs (
106
- " norm-fold-all" , cl::Hidden, cl::init(true ),
107
- cl::desc(" Folds all regular instructions (including pre-outputs)" ));
108
- cl::opt<bool > IRNormalizer::ReorderOperands (
109
- " norm-reorder-operands" , cl::Hidden, cl::init(true ),
110
- cl::desc(" Sorts and reorders operands in commutative instructions" ));
111
-
112
91
// / Entry method to the IRNormalizer.
113
92
// /
114
93
// / \param F Function to normalize.
@@ -118,7 +97,7 @@ bool IRNormalizer::runOnFunction(Function &F) {
118
97
119
98
Outputs = collectOutputInstructions (F);
120
99
121
- if (!PreserveOrder)
100
+ if (!Options. PreserveOrder )
122
101
reorderInstructions (F);
123
102
124
103
// TODO: Reorder basic blocks via a topological sort.
@@ -127,8 +106,8 @@ bool IRNormalizer::runOnFunction(Function &F) {
127
106
nameInstruction (I);
128
107
129
108
for (auto &I : instructions (F)) {
130
- if (!PreserveOrder) {
131
- if (ReorderOperands)
109
+ if (!Options. PreserveOrder ) {
110
+ if (Options. ReorderOperands )
132
111
reorderInstructionOperandsByNames (&I);
133
112
134
113
if (auto *Phi = dyn_cast<PHINode>(&I))
@@ -146,7 +125,7 @@ bool IRNormalizer::runOnFunction(Function &F) {
146
125
void IRNormalizer::nameFunctionArguments (Function &F) const {
147
126
int ArgumentCounter = 0 ;
148
127
for (auto &A : F.args ()) {
149
- if (RenameAll || A.getName ().empty ()) {
128
+ if (Options. RenameAll || A.getName ().empty ()) {
150
129
A.setName (" a" + Twine (ArgumentCounter));
151
130
ArgumentCounter += 1 ;
152
131
}
@@ -167,7 +146,7 @@ void IRNormalizer::nameBasicBlocks(Function &F) const {
167
146
if (isOutput (&I))
168
147
Hash = hashing::detail::hash_16_bytes (Hash, I.getOpcode ());
169
148
170
- if (RenameAll || B.getName ().empty ()) {
149
+ if (Options. RenameAll || B.getName ().empty ()) {
171
150
// Name basic block. Substring hash to make diffs more readable.
172
151
B.setName (" bb" + std::to_string (Hash).substr (0 , 5 ));
173
152
}
@@ -219,7 +198,7 @@ void IRNormalizer::sortCommutativeOperands(Instruction *I, T &Operands) const {
219
198
void IRNormalizer::nameAsInitialInstruction (Instruction *I) const {
220
199
if (I->getType ()->isVoidTy ())
221
200
return ;
222
- if (!(I->getName ().empty () || RenameAll))
201
+ if (!(I->getName ().empty () || Options. RenameAll ))
223
202
return ;
224
203
LLVM_DEBUG (dbgs () << " Naming initial instruction: " << *I << " \n " );
225
204
@@ -359,7 +338,7 @@ void IRNormalizer::nameAsRegularInstruction(Instruction *I) {
359
338
}
360
339
Name.append (" )" );
361
340
362
- if ((I->getName ().empty () || RenameAll) && !I->getType ()->isVoidTy ())
341
+ if ((I->getName ().empty () || Options. RenameAll ) && !I->getType ()->isVoidTy ())
363
342
I->setName (Name);
364
343
}
365
344
@@ -379,7 +358,7 @@ void IRNormalizer::nameAsRegularInstruction(Instruction *I) {
379
358
void IRNormalizer::foldInstructionName (Instruction *I) const {
380
359
// If this flag is raised, fold all regular
381
360
// instructions (including pre-outputs).
382
- if (!FoldPreOutputs) {
361
+ if (!Options. FoldPreOutputs ) {
383
362
// Don't fold if one of the users is an output instruction.
384
363
for (auto *U : I->users ())
385
364
if (auto *IU = dyn_cast<Instruction>(U))
@@ -690,7 +669,7 @@ SetVector<int> IRNormalizer::getOutputFootprint(
690
669
691
670
PreservedAnalyses IRNormalizerPass::run (Function &F,
692
671
FunctionAnalysisManager &AM) const {
693
- IRNormalizer{} .runOnFunction (F);
672
+ IRNormalizer (Options) .runOnFunction (F);
694
673
PreservedAnalyses PA;
695
674
PA.preserveSet <CFGAnalyses>();
696
675
return PA;
0 commit comments