IRSimilarityIdentifier.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. //===- IRSimilarityIdentifier.h - Find similarity in a module --------------==//
  2. //
  3. // 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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // \file
  10. // Interface file for the IRSimilarityIdentifier for identifying similarities in
  11. // IR including the IRInstructionMapper, which maps an Instruction to unsigned
  12. // integers.
  13. //
  14. // Two sequences of instructions are called "similar" if they perform the same
  15. // series of operations for all inputs.
  16. //
  17. // \code
  18. // %1 = add i32 %a, 10
  19. // %2 = add i32 %a, %1
  20. // %3 = icmp slt icmp %1, %2
  21. // \endcode
  22. //
  23. // and
  24. //
  25. // \code
  26. // %1 = add i32 11, %a
  27. // %2 = sub i32 %a, %1
  28. // %3 = icmp sgt icmp %2, %1
  29. // \endcode
  30. //
  31. // ultimately have the same result, even if the inputs, and structure are
  32. // slightly different.
  33. //
  34. // For instructions, we do not worry about operands that do not have fixed
  35. // semantic meaning to the program. We consider the opcode that the instruction
  36. // has, the types, parameters, and extra information such as the function name,
  37. // or comparison predicate. These are used to create a hash to map instructions
  38. // to integers to be used in similarity matching in sequences of instructions
  39. //
  40. // Terminology:
  41. // An IRSimilarityCandidate is a region of IRInstructionData (wrapped
  42. // Instructions), usually used to denote a region of similarity has been found.
  43. //
  44. // A SimilarityGroup is a set of IRSimilarityCandidates that are structurally
  45. // similar to one another.
  46. //
  47. //===----------------------------------------------------------------------===//
  48. #ifndef LLVM_ANALYSIS_IRSIMILARITYIDENTIFIER_H
  49. #define LLVM_ANALYSIS_IRSIMILARITYIDENTIFIER_H
  50. #include "llvm/IR/InstVisitor.h"
  51. #include "llvm/IR/Instructions.h"
  52. #include "llvm/IR/Module.h"
  53. #include "llvm/IR/PassManager.h"
  54. #include "llvm/Pass.h"
  55. #include "llvm/Support/Allocator.h"
  56. namespace llvm {
  57. namespace IRSimilarity {
  58. struct IRInstructionDataList;
  59. /// This represents what is and is not supported when finding similarity in
  60. /// Instructions.
  61. ///
  62. /// Legal Instructions are considered when looking at similarity between
  63. /// Instructions.
  64. ///
  65. /// Illegal Instructions cannot be considered when looking for similarity
  66. /// between Instructions. They act as boundaries between similarity regions.
  67. ///
  68. /// Invisible Instructions are skipped over during analysis.
  69. // TODO: Shared with MachineOutliner
  70. enum InstrType { Legal, Illegal, Invisible };
  71. /// This provides the utilities for hashing an Instruction to an unsigned
  72. /// integer. Two IRInstructionDatas produce the same hash value when their
  73. /// underlying Instructions perform the same operation (even if they don't have
  74. /// the same input operands.)
  75. /// As a more concrete example, consider the following:
  76. ///
  77. /// \code
  78. /// %add1 = add i32 %a, %b
  79. /// %add2 = add i32 %c, %d
  80. /// %add3 = add i64 %e, %f
  81. /// \endcode
  82. ///
  83. // Then the IRInstructionData wrappers for these Instructions may be hashed like
  84. /// so:
  85. ///
  86. /// \code
  87. /// ; These two adds have the same types and operand types, so they hash to the
  88. /// ; same number.
  89. /// %add1 = add i32 %a, %b ; Hash: 1
  90. /// %add2 = add i32 %c, %d ; Hash: 1
  91. /// ; This add produces an i64. This differentiates it from %add1 and %add2. So,
  92. /// ; it hashes to a different number.
  93. /// %add3 = add i64 %e, %f; Hash: 2
  94. /// \endcode
  95. ///
  96. ///
  97. /// This hashing scheme will be used to represent the program as a very long
  98. /// string. This string can then be placed in a data structure which can be used
  99. /// for similarity queries.
  100. ///
  101. /// TODO: Handle types of Instructions which can be equal even with different
  102. /// operands. (E.g. comparisons with swapped predicates.)
  103. /// TODO: Handle CallInsts, which are only checked for function type
  104. /// by \ref isSameOperationAs.
  105. /// TODO: Handle GetElementPtrInsts, as some of the operands have to be the
  106. /// exact same, and some do not.
  107. struct IRInstructionData : ilist_node<IRInstructionData> {
  108. /// The source Instruction that is being wrapped.
  109. Instruction *Inst = nullptr;
  110. /// The values of the operands in the Instruction.
  111. SmallVector<Value *, 4> OperVals;
  112. /// The legality of the wrapped instruction. This is informed by InstrType,
  113. /// and is used when checking when two instructions are considered similar.
  114. /// If either instruction is not legal, the instructions are automatically not
  115. /// considered similar.
  116. bool Legal;
  117. /// This is only relevant if we are wrapping a CmpInst where we needed to
  118. /// change the predicate of a compare instruction from a greater than form
  119. /// to a less than form. It is None otherwise.
  120. Optional<CmpInst::Predicate> RevisedPredicate;
  121. /// Gather the information that is difficult to gather for an Instruction, or
  122. /// is changed. i.e. the operands of an Instruction and the Types of those
  123. /// operands. This extra information allows for similarity matching to make
  124. /// assertions that allow for more flexibility when checking for whether an
  125. /// Instruction performs the same operation.
  126. IRInstructionData(Instruction &I, bool Legality, IRInstructionDataList &IDL);
  127. /// Get the predicate that the compare instruction is using for hashing the
  128. /// instruction. the IRInstructionData must be wrapping a CmpInst.
  129. CmpInst::Predicate getPredicate() const;
  130. /// A function that swaps the predicates to their less than form if they are
  131. /// in a greater than form. Otherwise, the predicate is unchanged.
  132. ///
  133. /// \param CI - The comparison operation to find a consistent preidcate for.
  134. /// \return the consistent comparison predicate.
  135. static CmpInst::Predicate predicateForConsistency(CmpInst *CI);
  136. /// Hashes \p Value based on its opcode, types, and operand types.
  137. /// Two IRInstructionData instances produce the same hash when they perform
  138. /// the same operation.
  139. ///
  140. /// As a simple example, consider the following instructions.
  141. ///
  142. /// \code
  143. /// %add1 = add i32 %x1, %y1
  144. /// %add2 = add i32 %x2, %y2
  145. ///
  146. /// %sub = sub i32 %x1, %y1
  147. ///
  148. /// %add_i64 = add i64 %x2, %y2
  149. /// \endcode
  150. ///
  151. /// Because the first two adds operate the same types, and are performing the
  152. /// same action, they will be hashed to the same value.
  153. ///
  154. /// However, the subtraction instruction is not the same as an addition, and
  155. /// will be hashed to a different value.
  156. ///
  157. /// Finally, the last add has a different type compared to the first two add
  158. /// instructions, so it will also be hashed to a different value that any of
  159. /// the previous instructions.
  160. ///
  161. /// \param [in] ID - The IRInstructionData instance to be hashed.
  162. /// \returns A hash_value of the IRInstructionData.
  163. friend hash_code hash_value(const IRInstructionData &ID) {
  164. SmallVector<Type *, 4> OperTypes;
  165. for (Value *V : ID.OperVals)
  166. OperTypes.push_back(V->getType());
  167. if (isa<CmpInst>(ID.Inst))
  168. return llvm::hash_combine(
  169. llvm::hash_value(ID.Inst->getOpcode()),
  170. llvm::hash_value(ID.Inst->getType()),
  171. llvm::hash_value(ID.getPredicate()),
  172. llvm::hash_combine_range(OperTypes.begin(), OperTypes.end()));
  173. else if (CallInst *CI = dyn_cast<CallInst>(ID.Inst))
  174. return llvm::hash_combine(
  175. llvm::hash_value(ID.Inst->getOpcode()),
  176. llvm::hash_value(ID.Inst->getType()),
  177. llvm::hash_value(CI->getCalledFunction()->getName().str()),
  178. llvm::hash_combine_range(OperTypes.begin(), OperTypes.end()));
  179. return llvm::hash_combine(
  180. llvm::hash_value(ID.Inst->getOpcode()),
  181. llvm::hash_value(ID.Inst->getType()),
  182. llvm::hash_combine_range(OperTypes.begin(), OperTypes.end()));
  183. }
  184. IRInstructionDataList *IDL = nullptr;
  185. };
  186. struct IRInstructionDataList : simple_ilist<IRInstructionData> {};
  187. /// Compare one IRInstructionData class to another IRInstructionData class for
  188. /// whether they are performing a the same operation, and can mapped to the
  189. /// same value. For regular instructions if the hash value is the same, then
  190. /// they will also be close.
  191. ///
  192. /// \param A - The first IRInstructionData class to compare
  193. /// \param B - The second IRInstructionData class to compare
  194. /// \returns true if \p A and \p B are similar enough to be mapped to the same
  195. /// value.
  196. bool isClose(const IRInstructionData &A, const IRInstructionData &B);
  197. struct IRInstructionDataTraits : DenseMapInfo<IRInstructionData *> {
  198. static inline IRInstructionData *getEmptyKey() { return nullptr; }
  199. static inline IRInstructionData *getTombstoneKey() {
  200. return reinterpret_cast<IRInstructionData *>(-1);
  201. }
  202. static unsigned getHashValue(const IRInstructionData *E) {
  203. using llvm::hash_value;
  204. assert(E && "IRInstructionData is a nullptr?");
  205. return hash_value(*E);
  206. }
  207. static bool isEqual(const IRInstructionData *LHS,
  208. const IRInstructionData *RHS) {
  209. if (RHS == getEmptyKey() || RHS == getTombstoneKey() ||
  210. LHS == getEmptyKey() || LHS == getTombstoneKey())
  211. return LHS == RHS;
  212. assert(LHS && RHS && "nullptr should have been caught by getEmptyKey?");
  213. return isClose(*LHS, *RHS);
  214. }
  215. };
  216. /// Helper struct for converting the Instructions in a Module into a vector of
  217. /// unsigned integers. This vector of unsigned integers can be thought of as a
  218. /// "numeric string". This numeric string can then be queried by, for example,
  219. /// data structures that find repeated substrings.
  220. ///
  221. /// This hashing is done per BasicBlock in the module. To hash Instructions
  222. /// based off of their operations, each Instruction is wrapped in an
  223. /// IRInstructionData struct. The unsigned integer for an IRInstructionData
  224. /// depends on:
  225. /// - The hash provided by the IRInstructionData.
  226. /// - Which member of InstrType the IRInstructionData is classified as.
  227. // See InstrType for more details on the possible classifications, and how they
  228. // manifest in the numeric string.
  229. ///
  230. /// The numeric string for an individual BasicBlock is terminated by an unique
  231. /// unsigned integer. This prevents data structures which rely on repetition
  232. /// from matching across BasicBlocks. (For example, the SuffixTree.)
  233. /// As a concrete example, if we have the following two BasicBlocks:
  234. /// \code
  235. /// bb0:
  236. /// %add1 = add i32 %a, %b
  237. /// %add2 = add i32 %c, %d
  238. /// %add3 = add i64 %e, %f
  239. /// bb1:
  240. /// %sub = sub i32 %c, %d
  241. /// \endcode
  242. /// We may hash the Instructions like this (via IRInstructionData):
  243. /// \code
  244. /// bb0:
  245. /// %add1 = add i32 %a, %b ; Hash: 1
  246. /// %add2 = add i32 %c, %d; Hash: 1
  247. /// %add3 = add i64 %e, %f; Hash: 2
  248. /// bb1:
  249. /// %sub = sub i32 %c, %d; Hash: 3
  250. /// %add4 = add i32 %c, %d ; Hash: 1
  251. /// \endcode
  252. /// And produce a "numeric string representation" like so:
  253. /// 1, 1, 2, unique_integer_1, 3, 1, unique_integer_2
  254. ///
  255. /// TODO: This is very similar to the MachineOutliner, and should be
  256. /// consolidated into the same interface.
  257. struct IRInstructionMapper {
  258. /// The starting illegal instruction number to map to.
  259. ///
  260. /// Set to -3 for compatibility with DenseMapInfo<unsigned>.
  261. unsigned IllegalInstrNumber = static_cast<unsigned>(-3);
  262. /// The next available integer to assign to a legal Instruction to.
  263. unsigned LegalInstrNumber = 0;
  264. /// Correspondence from IRInstructionData to unsigned integers.
  265. DenseMap<IRInstructionData *, unsigned, IRInstructionDataTraits>
  266. InstructionIntegerMap;
  267. /// Set if we added an illegal number in the previous step.
  268. /// Since each illegal number is unique, we only need one of them between
  269. /// each range of legal numbers. This lets us make sure we don't add more
  270. /// than one illegal number per range.
  271. bool AddedIllegalLastTime = false;
  272. /// Marks whether we found a illegal instruction in the previous step.
  273. bool CanCombineWithPrevInstr = false;
  274. /// Marks whether we have found a set of instructions that is long enough
  275. /// to be considered for similarity.
  276. bool HaveLegalRange = false;
  277. /// This allocator pointer is in charge of holding on to the IRInstructionData
  278. /// so it is not deallocated until whatever external tool is using it is done
  279. /// with the information.
  280. SpecificBumpPtrAllocator<IRInstructionData> *InstDataAllocator = nullptr;
  281. /// This allocator pointer is in charge of creating the IRInstructionDataList
  282. /// so it is not deallocated until whatever external tool is using it is done
  283. /// with the information.
  284. SpecificBumpPtrAllocator<IRInstructionDataList> *IDLAllocator = nullptr;
  285. /// Get an allocated IRInstructionData struct using the InstDataAllocator.
  286. ///
  287. /// \param I - The Instruction to wrap with IRInstructionData.
  288. /// \param Legality - A boolean value that is true if the instruction is to
  289. /// be considered for similarity, and false if not.
  290. /// \param IDL - The InstructionDataList that the IRInstructionData is
  291. /// inserted into.
  292. /// \returns An allocated IRInstructionData struct.
  293. IRInstructionData *allocateIRInstructionData(Instruction &I, bool Legality,
  294. IRInstructionDataList &IDL);
  295. /// Get an allocated IRInstructionDataList object using the IDLAllocator.
  296. ///
  297. /// \returns An allocated IRInstructionDataList object.
  298. IRInstructionDataList *allocateIRInstructionDataList();
  299. IRInstructionDataList *IDL = nullptr;
  300. /// Maps the Instructions in a BasicBlock \p BB to legal or illegal integers
  301. /// determined by \p InstrType. Two Instructions are mapped to the same value
  302. /// if they are close as defined by the InstructionData class above.
  303. ///
  304. /// \param [in] BB - The BasicBlock to be mapped to integers.
  305. /// \param [in,out] InstrList - Vector of IRInstructionData to append to.
  306. /// \param [in,out] IntegerMapping - Vector of unsigned integers to append to.
  307. void convertToUnsignedVec(BasicBlock &BB,
  308. std::vector<IRInstructionData *> &InstrList,
  309. std::vector<unsigned> &IntegerMapping);
  310. /// Maps an Instruction to a legal integer.
  311. ///
  312. /// \param [in] It - The Instruction to be mapped to an integer.
  313. /// \param [in,out] IntegerMappingForBB - Vector of unsigned integers to
  314. /// append to.
  315. /// \param [in,out] InstrListForBB - Vector of InstructionData to append to.
  316. /// \returns The integer \p It was mapped to.
  317. unsigned mapToLegalUnsigned(BasicBlock::iterator &It,
  318. std::vector<unsigned> &IntegerMappingForBB,
  319. std::vector<IRInstructionData *> &InstrListForBB);
  320. /// Maps an Instruction to an illegal integer.
  321. ///
  322. /// \param [in] It - The \p Instruction to be mapped to an integer.
  323. /// \param [in,out] IntegerMappingForBB - Vector of unsigned integers to
  324. /// append to.
  325. /// \param [in,out] InstrListForBB - Vector of IRInstructionData to append to.
  326. /// \param End - true if creating a dummy IRInstructionData at the end of a
  327. /// basic block.
  328. /// \returns The integer \p It was mapped to.
  329. unsigned mapToIllegalUnsigned(
  330. BasicBlock::iterator &It, std::vector<unsigned> &IntegerMappingForBB,
  331. std::vector<IRInstructionData *> &InstrListForBB, bool End = false);
  332. IRInstructionMapper(SpecificBumpPtrAllocator<IRInstructionData> *IDA,
  333. SpecificBumpPtrAllocator<IRInstructionDataList> *IDLA)
  334. : InstDataAllocator(IDA), IDLAllocator(IDLA) {
  335. // Make sure that the implementation of DenseMapInfo<unsigned> hasn't
  336. // changed.
  337. assert(DenseMapInfo<unsigned>::getEmptyKey() == static_cast<unsigned>(-1) &&
  338. "DenseMapInfo<unsigned>'s empty key isn't -1!");
  339. assert(DenseMapInfo<unsigned>::getTombstoneKey() ==
  340. static_cast<unsigned>(-2) &&
  341. "DenseMapInfo<unsigned>'s tombstone key isn't -2!");
  342. IDL = new (IDLAllocator->Allocate())
  343. IRInstructionDataList();
  344. }
  345. /// Custom InstVisitor to classify different instructions for whether it can
  346. /// be analyzed for similarity.
  347. struct InstructionClassification
  348. : public InstVisitor<InstructionClassification, InstrType> {
  349. InstructionClassification() {}
  350. // TODO: Determine a scheme to resolve when the label is similar enough.
  351. InstrType visitBranchInst(BranchInst &BI) { return Illegal; }
  352. // TODO: Determine a scheme to resolve when the labels are similar enough.
  353. InstrType visitPHINode(PHINode &PN) { return Illegal; }
  354. // TODO: Handle allocas.
  355. InstrType visitAllocaInst(AllocaInst &AI) { return Illegal; }
  356. // We exclude variable argument instructions since variable arguments
  357. // requires extra checking of the argument list.
  358. InstrType visitVAArgInst(VAArgInst &VI) { return Illegal; }
  359. // We exclude all exception handling cases since they are so context
  360. // dependent.
  361. InstrType visitLandingPadInst(LandingPadInst &LPI) { return Illegal; }
  362. InstrType visitFuncletPadInst(FuncletPadInst &FPI) { return Illegal; }
  363. // DebugInfo should be included in the regions, but should not be
  364. // analyzed for similarity as it has no bearing on the outcome of the
  365. // program.
  366. InstrType visitDbgInfoIntrinsic(DbgInfoIntrinsic &DII) { return Invisible; }
  367. // TODO: Handle specific intrinsics.
  368. InstrType visitIntrinsicInst(IntrinsicInst &II) { return Illegal; }
  369. // We only allow call instructions where the function has a name and
  370. // is not an indirect call.
  371. InstrType visitCallInst(CallInst &CI) {
  372. Function *F = CI.getCalledFunction();
  373. if (!F || CI.isIndirectCall() || !F->hasName())
  374. return Illegal;
  375. return Legal;
  376. }
  377. // TODO: We do not current handle similarity that changes the control flow.
  378. InstrType visitInvokeInst(InvokeInst &II) { return Illegal; }
  379. // TODO: We do not current handle similarity that changes the control flow.
  380. InstrType visitCallBrInst(CallBrInst &CBI) { return Illegal; }
  381. // TODO: Handle interblock similarity.
  382. InstrType visitTerminator(Instruction &I) { return Illegal; }
  383. InstrType visitInstruction(Instruction &I) { return Legal; }
  384. };
  385. /// Maps an Instruction to a member of InstrType.
  386. InstructionClassification InstClassifier;
  387. };
  388. /// This is a class that wraps a range of IRInstructionData from one point to
  389. /// another in the vector of IRInstructionData, which is a region of the
  390. /// program. It is also responsible for defining the structure within this
  391. /// region of instructions.
  392. ///
  393. /// The structure of a region is defined through a value numbering system
  394. /// assigned to each unique value in a region at the creation of the
  395. /// IRSimilarityCandidate.
  396. ///
  397. /// For example, for each Instruction we add a mapping for each new
  398. /// value seen in that Instruction.
  399. /// IR: Mapping Added:
  400. /// %add1 = add i32 %a, c1 %add1 -> 3, %a -> 1, c1 -> 2
  401. /// %add2 = add i32 %a, %1 %add2 -> 4
  402. /// %add3 = add i32 c2, c1 %add3 -> 6, c2 -> 5
  403. ///
  404. /// We can compare IRSimilarityCandidates against one another.
  405. /// The \ref isSimilar function compares each IRInstructionData against one
  406. /// another and if we have the same sequences of IRInstructionData that would
  407. /// create the same hash, we have similar IRSimilarityCandidates.
  408. ///
  409. /// We can also compare the structure of IRSimilarityCandidates. If we can
  410. /// create a mapping of registers in the region contained by one
  411. /// IRSimilarityCandidate to the region contained by different
  412. /// IRSimilarityCandidate, they can be considered structurally similar.
  413. ///
  414. /// IRSimilarityCandidate1: IRSimilarityCandidate2:
  415. /// %add1 = add i32 %a, %b %add1 = add i32 %d, %e
  416. /// %add2 = add i32 %a, %c %add2 = add i32 %d, %f
  417. /// %add3 = add i32 c1, c2 %add3 = add i32 c3, c4
  418. ///
  419. /// Can have the following mapping from candidate to candidate of:
  420. /// %a -> %d, %b -> %e, %c -> %f, c1 -> c3, c2 -> c4
  421. /// and can be considered similar.
  422. ///
  423. /// IRSimilarityCandidate1: IRSimilarityCandidate2:
  424. /// %add1 = add i32 %a, %b %add1 = add i32 %d, c4
  425. /// %add2 = add i32 %a, %c %add2 = add i32 %d, %f
  426. /// %add3 = add i32 c1, c2 %add3 = add i32 c3, c4
  427. ///
  428. /// We cannot create the same mapping since the use of c4 is not used in the
  429. /// same way as %b or c2.
  430. class IRSimilarityCandidate {
  431. private:
  432. /// The start index of this IRSimilarityCandidate in the instruction list.
  433. unsigned StartIdx = 0;
  434. /// The number of instructions in this IRSimilarityCandidate.
  435. unsigned Len = 0;
  436. /// The first instruction in this IRSimilarityCandidate.
  437. IRInstructionData *FirstInst = nullptr;
  438. /// The last instruction in this IRSimilarityCandidate.
  439. IRInstructionData *LastInst = nullptr;
  440. /// Global Value Numbering structures
  441. /// @{
  442. /// Stores the mapping of the value to the number assigned to it in the
  443. /// IRSimilarityCandidate.
  444. DenseMap<Value *, unsigned> ValueToNumber;
  445. /// Stores the mapping of the number to the value assigned this number.
  446. DenseMap<unsigned, Value *> NumberToValue;
  447. /// @}
  448. public:
  449. /// \param StartIdx - The starting location of the region.
  450. /// \param Len - The length of the region.
  451. /// \param FirstInstIt - The starting IRInstructionData of the region.
  452. /// \param LastInstIt - The ending IRInstructionData of the region.
  453. IRSimilarityCandidate(unsigned StartIdx, unsigned Len,
  454. IRInstructionData *FirstInstIt,
  455. IRInstructionData *LastInstIt);
  456. /// \param A - The first IRInstructionCandidate to compare.
  457. /// \param B - The second IRInstructionCandidate to compare.
  458. /// \returns True when every IRInstructionData in \p A is similar to every
  459. /// IRInstructionData in \p B.
  460. static bool isSimilar(const IRSimilarityCandidate &A,
  461. const IRSimilarityCandidate &B);
  462. /// \param A - The first IRInstructionCandidate to compare.
  463. /// \param B - The second IRInstructionCandidate to compare.
  464. /// \returns True when every IRInstructionData in \p A is structurally similar
  465. /// to \p B.
  466. static bool compareStructure(const IRSimilarityCandidate &A,
  467. const IRSimilarityCandidate &B);
  468. struct OperandMapping {
  469. /// The IRSimilarityCandidate that holds the instruction the OperVals were
  470. /// pulled from.
  471. const IRSimilarityCandidate &IRSC;
  472. /// The operand values to be analyzed.
  473. ArrayRef<Value *> &OperVals;
  474. /// The current mapping of global value numbers from one IRSimilarityCandidate
  475. /// to another IRSimilarityCandidate.
  476. DenseMap<unsigned, DenseSet<unsigned>> &ValueNumberMapping;
  477. };
  478. /// Compare the operands in \p A and \p B and check that the current mapping
  479. /// of global value numbers from \p A to \p B and \p B to \A is consistent.
  480. ///
  481. /// \param A - The first IRInstructionCandidate, operand values, and current
  482. /// operand mappings to compare.
  483. /// \param B - The second IRInstructionCandidate, operand values, and current
  484. /// operand mappings to compare.
  485. /// \returns true if the IRSimilarityCandidates operands are compatible.
  486. static bool compareNonCommutativeOperandMapping(OperandMapping A,
  487. OperandMapping B);
  488. /// Compare the operands in \p A and \p B and check that the current mapping
  489. /// of global value numbers from \p A to \p B and \p B to \A is consistent
  490. /// given that the operands are commutative.
  491. ///
  492. /// \param A - The first IRInstructionCandidate, operand values, and current
  493. /// operand mappings to compare.
  494. /// \param B - The second IRInstructionCandidate, operand values, and current
  495. /// operand mappings to compare.
  496. /// \returns true if the IRSimilarityCandidates operands are compatible.
  497. static bool compareCommutativeOperandMapping(OperandMapping A,
  498. OperandMapping B);
  499. /// Compare the start and end indices of the two IRSimilarityCandidates for
  500. /// whether they overlap. If the start instruction of one
  501. /// IRSimilarityCandidate is less than the end instruction of the other, and
  502. /// the start instruction of one is greater than the start instruction of the
  503. /// other, they overlap.
  504. ///
  505. /// \returns true if the IRSimilarityCandidates do not have overlapping
  506. /// instructions.
  507. static bool overlap(const IRSimilarityCandidate &A,
  508. const IRSimilarityCandidate &B);
  509. /// \returns the number of instructions in this Candidate.
  510. unsigned getLength() const { return Len; }
  511. /// \returns the start index of this IRSimilarityCandidate.
  512. unsigned getStartIdx() const { return StartIdx; }
  513. /// \returns the end index of this IRSimilarityCandidate.
  514. unsigned getEndIdx() const { return StartIdx + Len - 1; }
  515. /// \returns The first IRInstructionData.
  516. IRInstructionData *front() const { return FirstInst; }
  517. /// \returns The last IRInstructionData.
  518. IRInstructionData *back() const { return LastInst; }
  519. /// \returns The first Instruction.
  520. Instruction *frontInstruction() { return FirstInst->Inst; }
  521. /// \returns The last Instruction
  522. Instruction *backInstruction() { return LastInst->Inst; }
  523. /// \returns The BasicBlock the IRSimilarityCandidate starts in.
  524. BasicBlock *getStartBB() { return FirstInst->Inst->getParent(); }
  525. /// \returns The BasicBlock the IRSimilarityCandidate ends in.
  526. BasicBlock *getEndBB() { return LastInst->Inst->getParent(); }
  527. /// \returns The Function that the IRSimilarityCandidate is located in.
  528. Function *getFunction() { return getStartBB()->getParent(); }
  529. /// Finds the positive number associated with \p V if it has been mapped.
  530. /// \param [in] V - the Value to find.
  531. /// \returns The positive number corresponding to the value.
  532. /// \returns None if not present.
  533. Optional<unsigned> getGVN(Value *V) {
  534. assert(V != nullptr && "Value is a nullptr?");
  535. DenseMap<Value *, unsigned>::iterator VNIt = ValueToNumber.find(V);
  536. if (VNIt == ValueToNumber.end())
  537. return None;
  538. return VNIt->second;
  539. }
  540. /// Finds the Value associate with \p Num if it exists.
  541. /// \param [in] Num - the number to find.
  542. /// \returns The Value associated with the number.
  543. /// \returns None if not present.
  544. Optional<Value *> fromGVN(unsigned Num) {
  545. DenseMap<unsigned, Value *>::iterator VNIt = NumberToValue.find(Num);
  546. if (VNIt == NumberToValue.end())
  547. return None;
  548. assert(VNIt->second != nullptr && "Found value is a nullptr!");
  549. return VNIt->second;
  550. }
  551. /// \param RHS -The IRSimilarityCandidate to compare against
  552. /// \returns true if the IRSimilarityCandidate is occurs after the
  553. /// IRSimilarityCandidate in the program.
  554. bool operator<(const IRSimilarityCandidate &RHS) const {
  555. return getStartIdx() > RHS.getStartIdx();
  556. }
  557. using iterator = IRInstructionDataList::iterator;
  558. iterator begin() const { return iterator(front()); }
  559. iterator end() const { return std::next(iterator(back())); }
  560. };
  561. typedef std::vector<IRSimilarityCandidate> SimilarityGroup;
  562. typedef std::vector<SimilarityGroup> SimilarityGroupList;
  563. /// This class puts all the pieces of the IRInstructionData,
  564. /// IRInstructionMapper, IRSimilarityCandidate together.
  565. ///
  566. /// It first feeds the Module or vector of Modules into the IRInstructionMapper,
  567. /// and puts all the mapped instructions into a single long list of
  568. /// IRInstructionData.
  569. ///
  570. /// The list of unsigned integers is given to the Suffix Tree or similar data
  571. /// structure to find repeated subsequences. We construct an
  572. /// IRSimilarityCandidate for each instance of the subsequence. We compare them
  573. /// against one another since These repeated subsequences can have different
  574. /// structure. For each different kind of structure found, we create a
  575. /// similarity group.
  576. ///
  577. /// If we had four IRSimilarityCandidates A, B, C, and D where A, B and D are
  578. /// structurally similar to one another, while C is different we would have two
  579. /// SimilarityGroups:
  580. ///
  581. /// SimilarityGroup 1: SimilarityGroup 2
  582. /// A, B, D C
  583. ///
  584. /// A list of the different similarity groups is then returned after
  585. /// analyzing the module.
  586. class IRSimilarityIdentifier {
  587. public:
  588. IRSimilarityIdentifier()
  589. : Mapper(&InstDataAllocator, &InstDataListAllocator) {}
  590. /// \param M the module to find similarity in.
  591. explicit IRSimilarityIdentifier(Module &M)
  592. : Mapper(&InstDataAllocator, &InstDataListAllocator) {
  593. findSimilarity(M);
  594. }
  595. private:
  596. /// Map the instructions in the module to unsigned integers, using mapping
  597. /// already present in the Mapper if possible.
  598. ///
  599. /// \param [in] M Module - To map to integers.
  600. /// \param [in,out] InstrList - The vector to append IRInstructionData to.
  601. /// \param [in,out] IntegerMapping - The vector to append integers to.
  602. void populateMapper(Module &M, std::vector<IRInstructionData *> &InstrList,
  603. std::vector<unsigned> &IntegerMapping);
  604. /// Map the instructions in the modules vector to unsigned integers, using
  605. /// mapping already present in the mapper if possible.
  606. ///
  607. /// \param [in] Modules - The list of modules to use to populate the mapper
  608. /// \param [in,out] InstrList - The vector to append IRInstructionData to.
  609. /// \param [in,out] IntegerMapping - The vector to append integers to.
  610. void populateMapper(ArrayRef<std::unique_ptr<Module>> &Modules,
  611. std::vector<IRInstructionData *> &InstrList,
  612. std::vector<unsigned> &IntegerMapping);
  613. /// Find the similarity candidates in \p InstrList and corresponding
  614. /// \p UnsignedVec
  615. ///
  616. /// \param [in,out] InstrList - The vector to append IRInstructionData to.
  617. /// \param [in,out] IntegerMapping - The vector to append integers to.
  618. /// candidates found in the program.
  619. void findCandidates(std::vector<IRInstructionData *> &InstrList,
  620. std::vector<unsigned> &IntegerMapping);
  621. public:
  622. // Find the IRSimilarityCandidates in the \p Modules and group by structural
  623. // similarity in a SimilarityGroup, each group is returned in a
  624. // SimilarityGroupList.
  625. //
  626. // \param [in] Modules - the modules to analyze.
  627. // \returns The groups of similarity ranges found in the modules.
  628. SimilarityGroupList &
  629. findSimilarity(ArrayRef<std::unique_ptr<Module>> Modules);
  630. // Find the IRSimilarityCandidates in the given Module grouped by structural
  631. // similarity in a SimilarityGroup, contained inside a SimilarityGroupList.
  632. //
  633. // \param [in] M - the module to analyze.
  634. // \returns The groups of similarity ranges found in the module.
  635. SimilarityGroupList &findSimilarity(Module &M);
  636. // Clears \ref SimilarityCandidates if it is already filled by a previous run.
  637. void resetSimilarityCandidates() {
  638. // If we've already analyzed a Module or set of Modules, so we must clear
  639. // the SimilarityCandidates to make sure we do not have only old values
  640. // hanging around.
  641. if (SimilarityCandidates.hasValue())
  642. SimilarityCandidates->clear();
  643. else
  644. SimilarityCandidates = SimilarityGroupList();
  645. }
  646. // \returns The groups of similarity ranges found in the most recently passed
  647. // set of modules.
  648. Optional<SimilarityGroupList> &getSimilarity() {
  649. return SimilarityCandidates;
  650. }
  651. private:
  652. /// The allocator for IRInstructionData.
  653. SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
  654. /// The allocator for IRInstructionDataLists.
  655. SpecificBumpPtrAllocator<IRInstructionDataList> InstDataListAllocator;
  656. /// Map Instructions to unsigned integers and wraps the Instruction in an
  657. /// instance of IRInstructionData.
  658. IRInstructionMapper Mapper;
  659. /// The SimilarityGroups found with the most recent run of \ref
  660. /// findSimilarity. None if there is no recent run.
  661. Optional<SimilarityGroupList> SimilarityCandidates;
  662. };
  663. } // end namespace IRSimilarity
  664. /// An analysis pass based on legacy pass manager that runs and returns
  665. /// IRSimilarityIdentifier run on the Module.
  666. class IRSimilarityIdentifierWrapperPass : public ModulePass {
  667. std::unique_ptr<IRSimilarity::IRSimilarityIdentifier> IRSI;
  668. public:
  669. static char ID;
  670. IRSimilarityIdentifierWrapperPass();
  671. IRSimilarity::IRSimilarityIdentifier &getIRSI() { return *IRSI; }
  672. const IRSimilarity::IRSimilarityIdentifier &getIRSI() const { return *IRSI; }
  673. bool doInitialization(Module &M) override;
  674. bool doFinalization(Module &M) override;
  675. bool runOnModule(Module &M) override;
  676. void getAnalysisUsage(AnalysisUsage &AU) const override {
  677. AU.setPreservesAll();
  678. }
  679. };
  680. /// An analysis pass that runs and returns the IRSimilarityIdentifier run on the
  681. /// Module.
  682. class IRSimilarityAnalysis : public AnalysisInfoMixin<IRSimilarityAnalysis> {
  683. public:
  684. typedef IRSimilarity::IRSimilarityIdentifier Result;
  685. Result run(Module &M, ModuleAnalysisManager &);
  686. private:
  687. friend AnalysisInfoMixin<IRSimilarityAnalysis>;
  688. static AnalysisKey Key;
  689. };
  690. /// Printer pass that uses \c IRSimilarityAnalysis.
  691. class IRSimilarityAnalysisPrinterPass
  692. : public PassInfoMixin<IRSimilarityAnalysisPrinterPass> {
  693. raw_ostream &OS;
  694. public:
  695. explicit IRSimilarityAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {}
  696. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
  697. };
  698. } // end namespace llvm
  699. #endif // LLVM_ANALYSIS_IRSIMILARITYIDENTIFIER_H