Skip to content

Commit c24e9e3

Browse files
artagnonpfusik
andauthored
[HashRecognize] Strip ValueEvolution (#148620)
The ValueEvolution logic is deeply flawed, and checking that zero-bits are shifted can be exploited for miscompiles. In an effort to redo HashRecognize with a pattern-matching based approach, extract and fix the core logic of ValueEvolution, and strip it completely, showing that none of the tests rely on the KnownBits computation of ValueEvolution. Co-authored-by: Piotr Fusik <[email protected]>
1 parent ed3597e commit c24e9e3

File tree

4 files changed

+323
-273
lines changed

4 files changed

+323
-273
lines changed

llvm/include/llvm/Analysis/HashRecognize.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,12 @@
2020
#include "llvm/Analysis/ScalarEvolution.h"
2121
#include "llvm/IR/PassManager.h"
2222
#include "llvm/IR/Value.h"
23-
#include "llvm/Support/KnownBits.h"
2423
#include <variant>
2524

2625
namespace llvm {
2726

2827
class LPMUpdater;
2928

30-
/// A tuple of bits that are expected to be zero, number N of them expected to
31-
/// be zero, with a boolean indicating whether it's the top or bottom N bits
32-
/// expected to be zero.
33-
using ErrBits = std::tuple<KnownBits, unsigned, bool>;
34-
3529
/// A custom std::array with 256 entries, that also has a print function.
3630
struct CRCTable : public std::array<APInt, 256> {
3731
void print(raw_ostream &OS) const;
@@ -85,7 +79,7 @@ class HashRecognize {
8579
HashRecognize(const Loop &L, ScalarEvolution &SE);
8680

8781
// The main analysis entry points.
88-
std::variant<PolynomialInfo, ErrBits, StringRef> recognizeCRC() const;
82+
std::variant<PolynomialInfo, StringRef> recognizeCRC() const;
8983
std::optional<PolynomialInfo> getResult() const;
9084

9185
// Auxilary entry point after analysis to interleave the generating polynomial

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,6 +2283,14 @@ m_ZExtOrSExtOrSelf(const OpTy &Op) {
22832283
return m_CombineOr(m_ZExtOrSExt(Op), Op);
22842284
}
22852285

2286+
template <typename OpTy>
2287+
inline match_combine_or<match_combine_or<CastInst_match<OpTy, ZExtInst>,
2288+
CastInst_match<OpTy, TruncInst>>,
2289+
OpTy>
2290+
m_ZExtOrTruncOrSelf(const OpTy &Op) {
2291+
return m_CombineOr(m_CombineOr(m_ZExt(Op), m_Trunc(Op)), Op);
2292+
}
2293+
22862294
template <typename OpTy>
22872295
inline CastInst_match<OpTy, UIToFPInst> m_UIToFP(const OpTy &Op) {
22882296
return CastInst_match<OpTy, UIToFPInst>(Op);

0 commit comments

Comments
 (0)