@@ -40,21 +40,13 @@ namespace {
4040// / IRNormalizer aims to transform LLVM IR into normal form.
4141class IRNormalizer {
4242public:
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-
5543 bool runOnFunction (Function &F);
5644
45+ IRNormalizer (IRNormalizerOptions Options) : Options(Options) {}
46+
5747private:
48+ const IRNormalizerOptions Options;
49+
5850 // Random constant for hashing, so the state isn't zero.
5951 const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL ;
6052 DenseSet<const Instruction *> NamedInstructions;
@@ -96,19 +88,6 @@ class IRNormalizer {
9688};
9789} // namespace
9890
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-
11291// / Entry method to the IRNormalizer.
11392// /
11493// / \param F Function to normalize.
@@ -118,7 +97,7 @@ bool IRNormalizer::runOnFunction(Function &F) {
11897
11998 Outputs = collectOutputInstructions (F);
12099
121- if (!PreserveOrder)
100+ if (!Options. PreserveOrder )
122101 reorderInstructions (F);
123102
124103 // TODO: Reorder basic blocks via a topological sort.
@@ -127,8 +106,8 @@ bool IRNormalizer::runOnFunction(Function &F) {
127106 nameInstruction (I);
128107
129108 for (auto &I : instructions (F)) {
130- if (!PreserveOrder) {
131- if (ReorderOperands)
109+ if (!Options. PreserveOrder ) {
110+ if (Options. ReorderOperands )
132111 reorderInstructionOperandsByNames (&I);
133112
134113 if (auto *Phi = dyn_cast<PHINode>(&I))
@@ -146,7 +125,7 @@ bool IRNormalizer::runOnFunction(Function &F) {
146125void IRNormalizer::nameFunctionArguments (Function &F) const {
147126 int ArgumentCounter = 0 ;
148127 for (auto &A : F.args ()) {
149- if (RenameAll || A.getName ().empty ()) {
128+ if (Options. RenameAll || A.getName ().empty ()) {
150129 A.setName (" a" + Twine (ArgumentCounter));
151130 ArgumentCounter += 1 ;
152131 }
@@ -167,7 +146,7 @@ void IRNormalizer::nameBasicBlocks(Function &F) const {
167146 if (isOutput (&I))
168147 Hash = hashing::detail::hash_16_bytes (Hash, I.getOpcode ());
169148
170- if (RenameAll || B.getName ().empty ()) {
149+ if (Options. RenameAll || B.getName ().empty ()) {
171150 // Name basic block. Substring hash to make diffs more readable.
172151 B.setName (" bb" + std::to_string (Hash).substr (0 , 5 ));
173152 }
@@ -219,7 +198,7 @@ void IRNormalizer::sortCommutativeOperands(Instruction *I, T &Operands) const {
219198void IRNormalizer::nameAsInitialInstruction (Instruction *I) const {
220199 if (I->getType ()->isVoidTy ())
221200 return ;
222- if (!(I->getName ().empty () || RenameAll))
201+ if (!(I->getName ().empty () || Options. RenameAll ))
223202 return ;
224203 LLVM_DEBUG (dbgs () << " Naming initial instruction: " << *I << " \n " );
225204
@@ -359,7 +338,7 @@ void IRNormalizer::nameAsRegularInstruction(Instruction *I) {
359338 }
360339 Name.append (" )" );
361340
362- if ((I->getName ().empty () || RenameAll) && !I->getType ()->isVoidTy ())
341+ if ((I->getName ().empty () || Options. RenameAll ) && !I->getType ()->isVoidTy ())
363342 I->setName (Name);
364343}
365344
@@ -379,7 +358,7 @@ void IRNormalizer::nameAsRegularInstruction(Instruction *I) {
379358void IRNormalizer::foldInstructionName (Instruction *I) const {
380359 // If this flag is raised, fold all regular
381360 // instructions (including pre-outputs).
382- if (!FoldPreOutputs) {
361+ if (!Options. FoldPreOutputs ) {
383362 // Don't fold if one of the users is an output instruction.
384363 for (auto *U : I->users ())
385364 if (auto *IU = dyn_cast<Instruction>(U))
@@ -690,7 +669,7 @@ SetVector<int> IRNormalizer::getOutputFootprint(
690669
691670PreservedAnalyses IRNormalizerPass::run (Function &F,
692671 FunctionAnalysisManager &AM) const {
693- IRNormalizer{} .runOnFunction (F);
672+ IRNormalizer (Options) .runOnFunction (F);
694673 PreservedAnalyses PA;
695674 PA.preserveSet <CFGAnalyses>();
696675 return PA;
0 commit comments