LLVM: include/llvm/Support/KnownBits.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14#ifndef LLVM_SUPPORT_KNOWNBITS_H

15#define LLVM_SUPPORT_KNOWNBITS_H

16

19#include

20

21namespace llvm {

22

23

24struct KnownBits {

27

28private:

29

32

33

35

36public:

37

39

40

42

43

46 "Zero and One should have the same width!");

47 return Zero.getBitWidth();

48 }

49

50

52

53

57

58

59

64

65

67

68

70 return Zero.isSignBitSet() && One.isSignBitSet();

71 }

72

73

75 Zero.clearAllBits();

76 One.clearAllBits();

77 }

78

79

80 bool isZero() const { return Zero.isAllOnes(); }

81

82

84

85

87 Zero.setAllBits();

88 One.clearAllBits();

89 }

90

91

93 Zero.clearAllBits();

94 One.setAllBits();

95 }

96

97

98

100 Zero.setAllBits();

101 One.setAllBits();

102 }

103

104

106

107

109

110

112

113

115 return Zero.isSignBitSet() && One.isZero();

116 }

117

118

120 One.setSignBit();

121 }

122

123

125 Zero.setSignBit();

126 }

127

128

130

131 return One;

132 }

133

134

136

138

139 if (Zero.isSignBitClear())

141 return Min;

142 }

143

144

149

150

152

154

155 if (One.isSignBitClear())

156 Max.clearSignBit();

157 return Max;

158 }

159

160

164

165

166

170

171

178

179

183

184

185

193

194

195

203

204

205

213

214

215

217

218

219 void insertBits(const KnownBits &SubBits, unsigned BitPosition) {

220 Zero.insertBits(SubBits.Zero, BitPosition);

221 One.insertBits(SubBits.One, BitPosition);

222 }

223

224

225 KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const {

226 return KnownBits(Zero.extractBits(NumBits, BitPosition),

227 One.extractBits(NumBits, BitPosition));

228 }

229

230

231

232

233 KnownBits concat(const KnownBits &Lo) const {

235 }

236

237

238

240

241

243

244

246

247

249

250

252

253

254

263

264

265

266

267

268

272

273

275

276

278

279

281

282

284

285

287

288

292

293

294

295

299

300

304

305

306

307

308

309

310

314

315

316

317

318

319

320

324

325

327 return (LHS.Zero | RHS.Zero).isAllOnes();

328 }

329

330

334

335

339

340

341

345

346

347 static KnownBits add(const KnownBits &LHS, const KnownBits &RHS,

348 bool NSW = false, bool NUW = false) {

350 }

351

352

353 static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS,

354 bool NSW = false, bool NUW = false) {

356 }

357

358

361

362

365

366

369

370

373

374

377

378

381

382

385

386

389

390

392 bool NoUndefSelfMultiply = false);

393

394

396

397

399

400

402 bool Exact = false);

403

404

406 bool Exact = false);

407

408

410

411

413

414

416

417

419

420

422

423

425

426

428

429

431

432

433

435 bool NUW = false, bool NSW = false,

436 bool ShAmtNonZero = false);

437

438

439

441 bool ShAmtNonZero = false, bool Exact = false);

442

443

444

446 bool ShAmtNonZero = false, bool Exact = false);

447

448

451

452

455

456

459

460

463

464

467

468

471

472

475

476

479

480

483

484

487

488

490

491

493

494

496

497

499 Zero <<= ShAmt;

500 One <<= ShAmt;

501 return *this;

502 }

503

504

506 Zero.lshrInPlace(ShAmt);

507 One.lshrInPlace(ShAmt);

508 return *this;

509 }

510

511

513

517

521

522

523

525

526

527

529

533

535

537

538#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)

540#endif

541

542private:

543

544

546};

547

552

555 return std::move(RHS);

556}

557

562

565 return std::move(RHS);

566}

567

572

575 return std::move(RHS);

576}

577

582

583}

584

585#endif

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

This file implements a class to represent arbitrary precision integral constant values and operations...

#define LLVM_DUMP_METHOD

Mark debug helper function definitions like dump() that should not be stripped from debug builds.

Class for arbitrary precision integers.

void setBitsFrom(unsigned loBit)

Set the top bits starting from loBit.

void setSignBit()

Set the sign bit to 1.

This class implements an extremely fast bulk output stream that can only output to a stream.

@ C

The default llvm calling convention, compatible with C.

This is an optimization pass for GlobalISel generic memory operations.

APInt operator&(APInt a, const APInt &b)

APInt operator^(APInt a, const APInt &b)

raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)

constexpr unsigned BitWidth

OutputIt move(R &&Range, OutputIt Out)

Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.

APInt operator|(APInt a, const APInt &b)

Implement std::hash so that hash_code can be used in STL containers.

Definition KnownBits.h:24

static KnownBits makeConstant(const APInt &C)

Create known bits from a known constant.

Definition KnownBits.h:301

static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)

Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)

unsigned countMaxTrailingOnes() const

Returns the maximum number of trailing one bits possible.

Definition KnownBits.h:277

static LLVM_ABI std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)

Determine if these known bits always give the same ICMP_EQ result.

KnownBits anyextOrTrunc(unsigned BitWidth) const

Return known bits for an "any" extension or truncation of the value we're tracking.

Definition KnownBits.h:186

LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const

Return known bits for a in-register sign extension of the value we're tracking.

static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)

Compute known bits from zero-extended multiply-hi.

unsigned countMinSignBits() const

Returns the number of times the sign bit is replicated into the other bits.

Definition KnownBits.h:255

static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)

Compute known bits for smax(LHS, RHS).

bool isNonNegative() const

Returns true if this value is known to be non-negative.

Definition KnownBits.h:108

bool isZero() const

Returns true if value is all zero.

Definition KnownBits.h:80

LLVM_ABI KnownBits blsi() const

Compute known bits for X & -X, which has only the lowest bit set of X set.

void makeNonNegative()

Make this value non-negative.

Definition KnownBits.h:124

static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)

Compute knownbits resulting from llvm.usub.sat(LHS, RHS)

unsigned countMinLeadingOnes() const

Returns the minimum number of leading one bits.

Definition KnownBits.h:251

unsigned countMinTrailingZeros() const

Returns the minimum number of trailing zero bits.

Definition KnownBits.h:242

static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)

Compute known bits for ashr(LHS, RHS).

static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)

Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)

static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)

Compute known bits for urem(LHS, RHS).

bool isUnknown() const

Returns true if we don't know any bits.

Definition KnownBits.h:66

unsigned countMaxTrailingZeros() const

Returns the maximum number of trailing zero bits possible.

Definition KnownBits.h:274

static LLVM_ABI std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)

Determine if these known bits always give the same ICMP_NE result.

LLVM_ABI KnownBits makeGE(const APInt &Val) const

Return KnownBits based on this, but updated given that the underlying value is known to be greater th...

APInt getSignedMaxValue() const

Return the maximal signed value possible given these KnownBits.

Definition KnownBits.h:151

LLVM_ABI KnownBits blsmsk() const

Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...

void makeNegative()

Make this value negative.

Definition KnownBits.h:119

KnownBits & operator>>=(unsigned ShAmt)

Shift known bits right by ShAmt. Shifted in bits are unknown.

Definition KnownBits.h:505

void setAllConflict()

Make all bits known to be both zero and one.

Definition KnownBits.h:99

KnownBits trunc(unsigned BitWidth) const

Return known bits for a truncation of the value we're tracking.

Definition KnownBits.h:161

KnownBits byteSwap() const

Definition KnownBits.h:514

bool hasConflict() const

Returns true if there is conflicting information.

Definition KnownBits.h:51

static LLVM_ABI std::optional< bool > sge(const KnownBits &LHS, const KnownBits &RHS)

Determine if these known bits always give the same ICMP_SGE result.

unsigned countMaxPopulation() const

Returns the maximum number of bits that could be one.

Definition KnownBits.h:289

void setAllZero()

Make all bits known to be zero and discard any previous information.

Definition KnownBits.h:86

KnownBits reverseBits() const

Definition KnownBits.h:518

LLVM_ABI KnownBits & operator|=(const KnownBits &RHS)

Update known bits based on ORing with RHS.

LLVM_ABI void print(raw_ostream &OS) const

KnownBits concat(const KnownBits &Lo) const

Concatenate the bits from Lo onto the bottom of *this.

Definition KnownBits.h:233

unsigned getBitWidth() const

Get the bit width of this value.

Definition KnownBits.h:44

static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)

Compute known bits for umax(LHS, RHS).

KnownBits zext(unsigned BitWidth) const

Return known bits for a zero extension of the value we're tracking.

Definition KnownBits.h:172

bool isConstant() const

Returns true if we know the value of all bits.

Definition KnownBits.h:54

void resetAll()

Resets the known state of all bits.

Definition KnownBits.h:74

LLVM_DUMP_METHOD void dump() const

KnownBits unionWith(const KnownBits &RHS) const

Returns KnownBits information that is known to be true for either this or RHS or both.

Definition KnownBits.h:321

static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)

Compute known bits for lshr(LHS, RHS).

bool isSignUnknown() const

Returns true if we don't know the sign bit.

Definition KnownBits.h:69

bool isNonZero() const

Returns true if this value is known to be non-zero.

Definition KnownBits.h:111

static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)

Compute known bits for abdu(LHS, RHS).

bool operator==(const KnownBits &Other) const

Definition KnownBits.h:530

KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const

Return a subset of the known bits from [bitPosition,bitPosition+numBits).

Definition KnownBits.h:225

unsigned countMaxActiveBits() const

Returns the maximum number of bits needed to represent all possible unsigned values with these known ...

Definition KnownBits.h:296

static LLVM_ABI KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)

Compute knownbits resulting from APIntOps::avgFloorU.

KnownBits intersectWith(const KnownBits &RHS) const

Returns KnownBits information that is known to be true for both this and RHS.

Definition KnownBits.h:311

KnownBits sext(unsigned BitWidth) const

Return known bits for a sign extension of the value we're tracking.

Definition KnownBits.h:180

unsigned countMinTrailingOnes() const

Returns the minimum number of trailing one bits.

Definition KnownBits.h:245

static LLVM_ABI KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)

Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.

static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)

Compute knownbits resulting from addition of LHS and RHS.

Definition KnownBits.h:347

KnownBits & operator<<=(unsigned ShAmt)

Shift known bits left by ShAmt. Shift in bits are unknown.

Definition KnownBits.h:498

KnownBits zextOrTrunc(unsigned BitWidth) const

Return known bits for a zero extension or truncation of the value we're tracking.

Definition KnownBits.h:196

unsigned countMinLeadingZeros() const

Returns the minimum number of leading zero bits.

Definition KnownBits.h:248

APInt getMaxValue() const

Return the maximal unsigned value possible given these KnownBits.

Definition KnownBits.h:145

static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)

Compute known bits for abds(LHS, RHS).

static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)

Compute known bits for smin(LHS, RHS).

LLVM_ABI KnownBits & operator&=(const KnownBits &RHS)

Update known bits based on ANDing with RHS.

static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)

Compute known bits from sign-extended multiply-hi.

static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)

Compute known bits for srem(LHS, RHS).

static LLVM_ABI std::optional< bool > ugt(const KnownBits &LHS, const KnownBits &RHS)

Determine if these known bits always give the same ICMP_UGT result.

static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)

Compute known bits for udiv(LHS, RHS).

static LLVM_ABI std::optional< bool > slt(const KnownBits &LHS, const KnownBits &RHS)

Determine if these known bits always give the same ICMP_SLT result.

APInt getMinValue() const

Return the minimal unsigned value possible given these KnownBits.

Definition KnownBits.h:129

static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)

Compute known bits resulting from adding LHS and RHS.

bool isStrictlyPositive() const

Returns true if this value is known to be positive.

Definition KnownBits.h:114

static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)

Compute known bits for sdiv(LHS, RHS).

static LLVM_ABI std::optional< bool > ult(const KnownBits &LHS, const KnownBits &RHS)

Determine if these known bits always give the same ICMP_ULT result.

static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)

Compute knownbits resulting from APIntOps::avgFloorS.

bool operator!=(const KnownBits &Other) const

Definition KnownBits.h:534

static LLVM_ABI std::optional< bool > ule(const KnownBits &LHS, const KnownBits &RHS)

Determine if these known bits always give the same ICMP_ULE result.

static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)

Return true if LHS and RHS have no common bits set.

Definition KnownBits.h:326

bool isNegative() const

Returns true if this value is known to be negative.

Definition KnownBits.h:105

static LLVM_ABI KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)

Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.

static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)

Compute knownbits resulting from subtraction of LHS and RHS.

Definition KnownBits.h:353

unsigned countMaxLeadingZeros() const

Returns the maximum number of leading zero bits possible.

Definition KnownBits.h:280

void setAllOnes()

Make all bits known to be one and discard any previous information.

Definition KnownBits.h:92

void insertBits(const KnownBits &SubBits, unsigned BitPosition)

Insert the bits from a smaller known bits starting at bitPosition.

Definition KnownBits.h:219

static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)

Compute knownbits resulting from APIntOps::avgCeilU.

APInt One

Definition KnownBits.h:26

static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)

Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)

static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)

Compute known bits resulting from multiplying LHS and RHS.

KnownBits anyext(unsigned BitWidth) const

Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...

Definition KnownBits.h:167

APInt Zero

Definition KnownBits.h:25

LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const

Compute known bits for the absolute value.

static LLVM_ABI std::optional< bool > sle(const KnownBits &LHS, const KnownBits &RHS)

Determine if these known bits always give the same ICMP_SLE result.

unsigned countMaxSignificantBits() const

Returns the maximum number of bits needed to represent all possible signed values with these known bi...

Definition KnownBits.h:269

static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)

Determine if these known bits always give the same ICMP_SGT result.

unsigned countMinPopulation() const

Returns the number of bits known to be one.

Definition KnownBits.h:286

static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)

Determine if these known bits always give the same ICMP_UGE result.

KnownBits(unsigned BitWidth)

Create a known bits object of BitWidth bits initialized to unknown.

Definition KnownBits.h:41

unsigned countMaxLeadingOnes() const

Returns the maximum number of leading one bits possible.

Definition KnownBits.h:283

APInt getSignedMinValue() const

Return the minimal signed value possible given these KnownBits.

Definition KnownBits.h:135

LLVM_ABI KnownBits & operator^=(const KnownBits &RHS)

Update known bits based on XORing with RHS.

static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)

Compute known bits for shl(LHS, RHS).

static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)

Compute known bits for umin(LHS, RHS).

bool isAllOnes() const

Returns true if value is all one bits.

Definition KnownBits.h:83

KnownBits sextOrTrunc(unsigned BitWidth) const

Return known bits for a sign extension or truncation of the value we're tracking.

Definition KnownBits.h:206

static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)

Compute knownbits resulting from APIntOps::avgCeilS.

const APInt & getConstant() const

Returns the value when all bits have a known value.

Definition KnownBits.h:60