1//===-- ConstraintElimination.cpp - Eliminate conds using constraints. ----===//
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// Eliminate conditions based on constraints collected from dominating
10// conditions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Transforms/Scalar/ConstraintElimination.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/ScopeExit.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/Analysis/ConstraintSystem.h"
20#include "llvm/Analysis/GlobalsModRef.h"
21#include "llvm/Analysis/LoopInfo.h"
22#include "llvm/Analysis/MemoryBuiltins.h"
23#include "llvm/Analysis/OptimizationRemarkEmitter.h"
24#include "llvm/Analysis/ScalarEvolution.h"
25#include "llvm/Analysis/ScalarEvolutionExpressions.h"
26#include "llvm/Analysis/TargetLibraryInfo.h"
27#include "llvm/Analysis/ValueTracking.h"
28#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/DebugInfo.h"
30#include "llvm/IR/Dominators.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/IRBuilder.h"
33#include "llvm/IR/InstrTypes.h"
34#include "llvm/IR/Instructions.h"
35#include "llvm/IR/Module.h"
36#include "llvm/IR/PatternMatch.h"
37#include "llvm/IR/Verifier.h"
38#include "llvm/Pass.h"
39#include "llvm/Support/CommandLine.h"
40#include "llvm/Support/Debug.h"
41#include "llvm/Support/DebugCounter.h"
42#include "llvm/Support/MathExtras.h"
43#include "llvm/Transforms/Utils/Cloning.h"
44#include "llvm/Transforms/Utils/ValueMapper.h"
45
46#include <optional>
47#include <string>
48
49using namespace llvm;
50using namespace PatternMatch;
51
52#define DEBUG_TYPE "constraint-elimination"
53
54STATISTIC(NumCondsRemoved, "Number of instructions removed");
55DEBUG_COUNTER(EliminatedCounter, "conds-eliminated",
56 "Controls which conditions are eliminated");
57
58static cl::opt<unsigned>
59 MaxRows("constraint-elimination-max-rows", cl::init(Val: 500), cl::Hidden,
60 cl::desc("Maximum number of rows to keep in constraint system"));
61
62static cl::opt<bool> DumpReproducers(
63 "constraint-elimination-dump-reproducers", cl::init(Val: false), cl::Hidden,
64 cl::desc("Dump IR to reproduce successful transformations."));
65
66static int64_t MaxConstraintValue = std::numeric_limits<int64_t>::max();
67static int64_t MinSignedConstraintValue = std::numeric_limits<int64_t>::min();
68
69static Instruction *getContextInstForUse(Use &U) {
70 Instruction *UserI = cast<Instruction>(Val: U.getUser());
71 if (auto *Phi = dyn_cast<PHINode>(Val: UserI))
72 UserI = Phi->getIncomingBlock(U)->getTerminator();
73 return UserI;
74}
75
76namespace {
77/// Struct to express a condition of the form %Op0 Pred %Op1.
78struct ConditionTy {
79 CmpPredicate Pred;
80 Value *Op0 = nullptr;
81 Value *Op1 = nullptr;
82
83 ConditionTy() = default;
84 ConditionTy(CmpPredicate Pred, Value *Op0, Value *Op1)
85 : Pred(Pred), Op0(Op0), Op1(Op1) {}
86};
87
88/// Represents either
89/// * a condition that holds on entry to a block (=condition fact)
90/// * an assume (=assume fact)
91/// * a use of a compare instruction to simplify.
92/// It also tracks the Dominator DFS in and out numbers for each entry.
93struct FactOrCheck {
94 enum class EntryTy {
95 ConditionFact, /// A condition that holds on entry to a block.
96 InstFact, /// A fact that holds after Inst executed (e.g. an assume or
97 /// min/mix intrinsic.
98 InstCheck, /// An instruction to simplify (e.g. an overflow math
99 /// intrinsics).
100 UseCheck /// An use of a compare instruction to simplify.
101 };
102
103 union {
104 Instruction *Inst;
105 Use *U;
106 ConditionTy Cond;
107 };
108
109 /// A pre-condition that must hold for the current fact to be added to the
110 /// system.
111 ConditionTy DoesHold;
112
113 unsigned NumIn;
114 unsigned NumOut;
115 EntryTy Ty;
116
117 FactOrCheck(EntryTy Ty, DomTreeNode *DTN, Instruction *Inst)
118 : Inst(Inst), NumIn(DTN->getDFSNumIn()), NumOut(DTN->getDFSNumOut()),
119 Ty(Ty) {}
120
121 FactOrCheck(DomTreeNode *DTN, Use *U)
122 : U(U), NumIn(DTN->getDFSNumIn()), NumOut(DTN->getDFSNumOut()),
123 Ty(EntryTy::UseCheck) {}
124
125 FactOrCheck(DomTreeNode *DTN, CmpPredicate Pred, Value *Op0, Value *Op1,
126 ConditionTy Precond = {})
127 : Cond(Pred, Op0, Op1), DoesHold(Precond), NumIn(DTN->getDFSNumIn()),
128 NumOut(DTN->getDFSNumOut()), Ty(EntryTy::ConditionFact) {}
129
130 static FactOrCheck getConditionFact(DomTreeNode *DTN, CmpPredicate Pred,
131 Value *Op0, Value *Op1,
132 ConditionTy Precond = {}) {
133 return FactOrCheck(DTN, Pred, Op0, Op1, Precond);
134 }
135
136 static FactOrCheck getInstFact(DomTreeNode *DTN, Instruction *Inst) {
137 return FactOrCheck(EntryTy::InstFact, DTN, Inst);
138 }
139
140 static FactOrCheck getCheck(DomTreeNode *DTN, Use *U) {
141 return FactOrCheck(DTN, U);
142 }
143
144 static FactOrCheck getCheck(DomTreeNode *DTN, CallInst *CI) {
145 return FactOrCheck(EntryTy::InstCheck, DTN, CI);
146 }
147
148 bool isCheck() const {
149 return Ty == EntryTy::InstCheck || Ty == EntryTy::UseCheck;
150 }
151
152 Instruction *getContextInst() const {
153 assert(!isConditionFact());
154 if (Ty == EntryTy::UseCheck)
155 return getContextInstForUse(U&: *U);
156 return Inst;
157 }
158
159 Instruction *getInstructionToSimplify() const {
160 assert(isCheck());
161 if (Ty == EntryTy::InstCheck)
162 return Inst;
163 // The use may have been simplified to a constant already.
164 return dyn_cast<Instruction>(Val&: *U);
165 }
166
167 bool isConditionFact() const { return Ty == EntryTy::ConditionFact; }
168};
169
170/// Keep state required to build worklist.
171struct State {
172 DominatorTree &DT;
173 LoopInfo &LI;
174 ScalarEvolution &SE;
175 TargetLibraryInfo &TLI;
176 SmallVector<FactOrCheck, 64> WorkList;
177
178 State(DominatorTree &DT, LoopInfo &LI, ScalarEvolution &SE,
179 TargetLibraryInfo &TLI)
180 : DT(DT), LI(LI), SE(SE), TLI(TLI) {}
181
182 /// Process block \p BB and add known facts to work-list.
183 void addInfoFor(BasicBlock &BB);
184
185 /// Try to add facts for loop inductions (AddRecs) in EQ/NE compares
186 /// controlling the loop header.
187 void addInfoForInductions(BasicBlock &BB);
188
189 /// Returns true if we can add a known condition from BB to its successor
190 /// block Succ.
191 bool canAddSuccessor(BasicBlock &BB, BasicBlock *Succ) const {
192 return DT.dominates(BBE: BasicBlockEdge(&BB, Succ), BB: Succ);
193 }
194};
195
196class ConstraintInfo;
197
198struct StackEntry {
199 unsigned NumIn;
200 unsigned NumOut;
201 bool IsSigned = false;
202 /// Variables that can be removed from the system once the stack entry gets
203 /// removed.
204 SmallVector<Value *, 2> ValuesToRelease;
205
206 StackEntry(unsigned NumIn, unsigned NumOut, bool IsSigned,
207 SmallVector<Value *, 2> ValuesToRelease)
208 : NumIn(NumIn), NumOut(NumOut), IsSigned(IsSigned),
209 ValuesToRelease(std::move(ValuesToRelease)) {}
210};
211
212struct ConstraintTy {
213 SmallVector<int64_t, 8> Coefficients;
214 SmallVector<ConditionTy, 2> Preconditions;
215
216 bool IsSigned = false;
217
218 ConstraintTy() = default;
219
220 ConstraintTy(SmallVector<int64_t, 8> Coefficients, bool IsSigned, bool IsEq,
221 bool IsNe)
222 : Coefficients(std::move(Coefficients)), IsSigned(IsSigned), IsEq(IsEq),
223 IsNe(IsNe) {}
224
225 unsigned size() const { return Coefficients.size(); }
226
227 unsigned empty() const { return Coefficients.empty(); }
228
229 /// Returns true if all preconditions for this list of constraints are
230 /// satisfied given \p Info.
231 bool isValid(const ConstraintInfo &Info) const;
232
233 bool isEq() const { return IsEq; }
234
235 bool isNe() const { return IsNe; }
236
237 /// Check if the current constraint is implied by the given ConstraintSystem.
238 ///
239 /// \return true or false if the constraint is proven to be respectively true,
240 /// or false. When the constraint cannot be proven to be either true or false,
241 /// std::nullopt is returned.
242 std::optional<bool> isImpliedBy(const ConstraintSystem &CS) const;
243
244private:
245 bool IsEq = false;
246 bool IsNe = false;
247};
248
249/// Wrapper encapsulating separate constraint systems and corresponding value
250/// mappings for both unsigned and signed information. Facts are added to and
251/// conditions are checked against the corresponding system depending on the
252/// signed-ness of their predicates. While the information is kept separate
253/// based on signed-ness, certain conditions can be transferred between the two
254/// systems.
255class ConstraintInfo {
256
257 ConstraintSystem UnsignedCS;
258 ConstraintSystem SignedCS;
259
260 const DataLayout &DL;
261
262public:
263 ConstraintInfo(const DataLayout &DL, ArrayRef<Value *> FunctionArgs)
264 : UnsignedCS(FunctionArgs), SignedCS(FunctionArgs), DL(DL) {
265 auto &Value2Index = getValue2Index(Signed: false);
266 // Add Arg > -1 constraints to unsigned system for all function arguments.
267 for (Value *Arg : FunctionArgs) {
268 ConstraintTy VarPos(SmallVector<int64_t, 8>(Value2Index.size() + 1, 0),
269 false, false, false);
270 VarPos.Coefficients[Value2Index[Arg]] = -1;
271 UnsignedCS.addVariableRow(R: VarPos.Coefficients);
272 }
273 }
274
275 DenseMap<Value *, unsigned> &getValue2Index(bool Signed) {
276 return Signed ? SignedCS.getValue2Index() : UnsignedCS.getValue2Index();
277 }
278 const DenseMap<Value *, unsigned> &getValue2Index(bool Signed) const {
279 return Signed ? SignedCS.getValue2Index() : UnsignedCS.getValue2Index();
280 }
281
282 ConstraintSystem &getCS(bool Signed) {
283 return Signed ? SignedCS : UnsignedCS;
284 }
285 const ConstraintSystem &getCS(bool Signed) const {
286 return Signed ? SignedCS : UnsignedCS;
287 }
288
289 void popLastConstraint(bool Signed) { getCS(Signed).popLastConstraint(); }
290 void popLastNVariables(bool Signed, unsigned N) {
291 getCS(Signed).popLastNVariables(N);
292 }
293
294 bool doesHold(CmpInst::Predicate Pred, Value *A, Value *B) const;
295
296 void addFact(CmpInst::Predicate Pred, Value *A, Value *B, unsigned NumIn,
297 unsigned NumOut, SmallVectorImpl<StackEntry> &DFSInStack);
298
299 /// Turn a comparison of the form \p Op0 \p Pred \p Op1 into a vector of
300 /// constraints, using indices from the corresponding constraint system.
301 /// New variables that need to be added to the system are collected in
302 /// \p NewVariables.
303 ConstraintTy getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
304 SmallVectorImpl<Value *> &NewVariables,
305 bool ForceSignedSystem = false) const;
306
307 /// Turns a comparison of the form \p Op0 \p Pred \p Op1 into a vector of
308 /// constraints using getConstraint. Returns an empty constraint if the result
309 /// cannot be used to query the existing constraint system, e.g. because it
310 /// would require adding new variables. Also tries to convert signed
311 /// predicates to unsigned ones if possible to allow using the unsigned system
312 /// which increases the effectiveness of the signed <-> unsigned transfer
313 /// logic.
314 ConstraintTy getConstraintForSolving(CmpInst::Predicate Pred, Value *Op0,
315 Value *Op1) const;
316
317 /// Try to add information from \p A \p Pred \p B to the unsigned/signed
318 /// system if \p Pred is signed/unsigned.
319 void transferToOtherSystem(CmpInst::Predicate Pred, Value *A, Value *B,
320 unsigned NumIn, unsigned NumOut,
321 SmallVectorImpl<StackEntry> &DFSInStack);
322
323private:
324 /// Adds facts into constraint system. \p ForceSignedSystem can be set when
325 /// the \p Pred is eq/ne, and signed constraint system is used when it's
326 /// specified.
327 void addFactImpl(CmpInst::Predicate Pred, Value *A, Value *B, unsigned NumIn,
328 unsigned NumOut, SmallVectorImpl<StackEntry> &DFSInStack,
329 bool ForceSignedSystem);
330};
331
332/// Represents a (Coefficient * Variable) entry after IR decomposition.
333struct DecompEntry {
334 int64_t Coefficient;
335 Value *Variable;
336
337 DecompEntry(int64_t Coefficient, Value *Variable)
338 : Coefficient(Coefficient), Variable(Variable) {}
339};
340
341/// Represents an Offset + Coefficient1 * Variable1 + ... decomposition.
342struct Decomposition {
343 int64_t Offset = 0;
344 SmallVector<DecompEntry, 3> Vars;
345
346 Decomposition(int64_t Offset) : Offset(Offset) {}
347 Decomposition(Value *V) { Vars.emplace_back(Args: 1, Args&: V); }
348 Decomposition(int64_t Offset, ArrayRef<DecompEntry> Vars)
349 : Offset(Offset), Vars(Vars) {}
350
351 /// Add \p OtherOffset and return true if the operation overflows, i.e. the
352 /// new decomposition is invalid.
353 [[nodiscard]] bool add(int64_t OtherOffset) {
354 return AddOverflow(X: Offset, Y: OtherOffset, Result&: Offset);
355 }
356
357 /// Add \p Other and return true if the operation overflows, i.e. the new
358 /// decomposition is invalid.
359 [[nodiscard]] bool add(const Decomposition &Other) {
360 if (add(OtherOffset: Other.Offset))
361 return true;
362 append_range(C&: Vars, R: Other.Vars);
363 return false;
364 }
365
366 /// Subtract \p Other and return true if the operation overflows, i.e. the new
367 /// decomposition is invalid.
368 [[nodiscard]] bool sub(const Decomposition &Other) {
369 Decomposition Tmp = Other;
370 if (Tmp.mul(Factor: -1))
371 return true;
372 if (add(OtherOffset: Tmp.Offset))
373 return true;
374 append_range(C&: Vars, R&: Tmp.Vars);
375 return false;
376 }
377
378 /// Multiply all coefficients by \p Factor and return true if the operation
379 /// overflows, i.e. the new decomposition is invalid.
380 [[nodiscard]] bool mul(int64_t Factor) {
381 if (MulOverflow(X: Offset, Y: Factor, Result&: Offset))
382 return true;
383 for (auto &Var : Vars)
384 if (MulOverflow(X: Var.Coefficient, Y: Factor, Result&: Var.Coefficient))
385 return true;
386 return false;
387 }
388};
389
390// Variable and constant offsets for a chain of GEPs, with base pointer BasePtr.
391struct OffsetResult {
392 Value *BasePtr;
393 APInt ConstantOffset;
394 SmallMapVector<Value *, APInt, 4> VariableOffsets;
395 GEPNoWrapFlags NW;
396
397 OffsetResult() : BasePtr(nullptr), ConstantOffset(0, uint64_t(0)) {}
398
399 OffsetResult(GEPOperator &GEP, const DataLayout &DL)
400 : BasePtr(GEP.getPointerOperand()), NW(GEP.getNoWrapFlags()) {
401 ConstantOffset = APInt(DL.getIndexTypeSizeInBits(Ty: BasePtr->getType()), 0);
402 }
403};
404} // namespace
405
406// Try to collect variable and constant offsets for \p GEP, partly traversing
407// nested GEPs. Returns an OffsetResult with nullptr as BasePtr of collecting
408// the offset fails.
409static OffsetResult collectOffsets(GEPOperator &GEP, const DataLayout &DL) {
410 OffsetResult Result(GEP, DL);
411 unsigned BitWidth = Result.ConstantOffset.getBitWidth();
412 if (!GEP.collectOffset(DL, BitWidth, VariableOffsets&: Result.VariableOffsets,
413 ConstantOffset&: Result.ConstantOffset))
414 return {};
415
416 // If we have a nested GEP, check if we can combine the constant offset of the
417 // inner GEP with the outer GEP.
418 if (auto *InnerGEP = dyn_cast<GetElementPtrInst>(Val: Result.BasePtr)) {
419 SmallMapVector<Value *, APInt, 4> VariableOffsets2;
420 APInt ConstantOffset2(BitWidth, 0);
421 bool CanCollectInner = InnerGEP->collectOffset(
422 DL, BitWidth, VariableOffsets&: VariableOffsets2, ConstantOffset&: ConstantOffset2);
423 // TODO: Support cases with more than 1 variable offset.
424 if (!CanCollectInner || Result.VariableOffsets.size() > 1 ||
425 VariableOffsets2.size() > 1 ||
426 (Result.VariableOffsets.size() >= 1 && VariableOffsets2.size() >= 1)) {
427 // More than 1 variable index, use outer result.
428 return Result;
429 }
430 Result.BasePtr = InnerGEP->getPointerOperand();
431 Result.ConstantOffset += ConstantOffset2;
432 if (Result.VariableOffsets.size() == 0 && VariableOffsets2.size() == 1)
433 Result.VariableOffsets = std::move(VariableOffsets2);
434 Result.NW &= InnerGEP->getNoWrapFlags();
435 }
436 return Result;
437}
438
439static Decomposition decompose(Value *V,
440 SmallVectorImpl<ConditionTy> &Preconditions,
441 bool IsSigned, const DataLayout &DL);
442
443static bool canUseSExt(ConstantInt *CI) {
444 const APInt &Val = CI->getValue();
445 return Val.sgt(RHS: MinSignedConstraintValue) && Val.slt(RHS: MaxConstraintValue);
446}
447
448static Decomposition decomposeGEP(GEPOperator &GEP,
449 SmallVectorImpl<ConditionTy> &Preconditions,
450 bool IsSigned, const DataLayout &DL) {
451 // Do not reason about pointers where the index size is larger than 64 bits,
452 // as the coefficients used to encode constraints are 64 bit integers.
453 if (DL.getIndexTypeSizeInBits(Ty: GEP.getPointerOperand()->getType()) > 64)
454 return &GEP;
455
456 assert(!IsSigned && "The logic below only supports decomposition for "
457 "unsigned predicates at the moment.");
458 const auto &[BasePtr, ConstantOffset, VariableOffsets, NW] =
459 collectOffsets(GEP, DL);
460 // We support either plain gep nuw, or gep nusw with non-negative offset,
461 // which implies gep nuw.
462 if (!BasePtr || NW == GEPNoWrapFlags::none())
463 return &GEP;
464
465 // For a nuw-only GEP (nuw without nusw/inbounds), the offset must be
466 // interpreted as unsigned.
467 if (!NW.hasNoUnsignedSignedWrap() && ConstantOffset.isNegative())
468 return &GEP;
469
470 Decomposition Result(ConstantOffset.getSExtValue(), DecompEntry(1, BasePtr));
471 for (auto [Index, Scale] : VariableOffsets) {
472 auto IdxResult = decompose(V: Index, Preconditions, IsSigned, DL);
473 if (IdxResult.mul(Factor: Scale.getSExtValue()))
474 return &GEP;
475 if (Result.add(Other: IdxResult))
476 return &GEP;
477
478 if (!NW.hasNoUnsignedWrap()) {
479 // Try to prove nuw from nusw and nneg.
480 assert(NW.hasNoUnsignedSignedWrap() && "Must have nusw flag");
481 if (!isKnownNonNegative(V: Index, SQ: DL))
482 Preconditions.emplace_back(Args: CmpInst::ICMP_SGE, Args&: Index,
483 Args: ConstantInt::get(Ty: Index->getType(), V: 0));
484 }
485 }
486 return Result;
487}
488
489// Decomposes \p V into a constant offset + list of pairs { Coefficient,
490// Variable } where Coefficient * Variable. The sum of the constant offset and
491// pairs equals \p V.
492static Decomposition decompose(Value *V,
493 SmallVectorImpl<ConditionTy> &Preconditions,
494 bool IsSigned, const DataLayout &DL) {
495
496 auto MergeResults = [&Preconditions, IsSigned,
497 &DL](Value *A, Value *B,
498 bool IsSignedB) -> std::optional<Decomposition> {
499 auto ResA = decompose(V: A, Preconditions, IsSigned, DL);
500 auto ResB = decompose(V: B, Preconditions, IsSigned: IsSignedB, DL);
501 if (ResA.add(Other: ResB))
502 return std::nullopt;
503 return ResA;
504 };
505
506 Type *Ty = V->getType()->getScalarType();
507 if (Ty->isPointerTy() && !IsSigned) {
508 if (auto *GEP = dyn_cast<GEPOperator>(Val: V))
509 return decomposeGEP(GEP&: *GEP, Preconditions, IsSigned, DL);
510 if (isa<ConstantPointerNull>(Val: V))
511 return int64_t(0);
512
513 return V;
514 }
515
516 // Don't handle integers > 64 bit. Our coefficients are 64-bit large, so
517 // coefficient add/mul may wrap, while the operation in the full bit width
518 // would not.
519 if (!Ty->isIntegerTy() || Ty->getIntegerBitWidth() > 64)
520 return V;
521
522 // Decompose \p V used with a signed predicate.
523 if (IsSigned) {
524 if (auto *CI = dyn_cast<ConstantInt>(Val: V)) {
525 if (canUseSExt(CI))
526 return CI->getSExtValue();
527 }
528 Value *Op0;
529 Value *Op1;
530
531 if (match(V, P: m_SExt(Op: m_Value(V&: Op0))))
532 V = Op0;
533 else if (match(V, P: m_NNegZExt(Op: m_Value(V&: Op0)))) {
534 V = Op0;
535 } else if (match(V, P: m_NSWTrunc(Op: m_Value(V&: Op0)))) {
536 if (Op0->getType()->getScalarSizeInBits() <= 64)
537 V = Op0;
538 }
539
540 if (match(V, P: m_NSWAdd(L: m_Value(V&: Op0), R: m_Value(V&: Op1)))) {
541 if (auto Decomp = MergeResults(Op0, Op1, IsSigned))
542 return *Decomp;
543 return V;
544 }
545
546 if (match(V, P: m_NSWSub(L: m_Value(V&: Op0), R: m_Value(V&: Op1)))) {
547 auto ResA = decompose(V: Op0, Preconditions, IsSigned, DL);
548 auto ResB = decompose(V: Op1, Preconditions, IsSigned, DL);
549 if (!ResA.sub(Other: ResB))
550 return ResA;
551 return V;
552 }
553
554 ConstantInt *CI;
555 if (match(V, P: m_NSWMul(L: m_Value(V&: Op0), R: m_ConstantInt(CI))) && canUseSExt(CI)) {
556 auto Result = decompose(V: Op0, Preconditions, IsSigned, DL);
557 if (!Result.mul(Factor: CI->getSExtValue()))
558 return Result;
559 return V;
560 }
561
562 // (shl nsw x, shift) is (mul nsw x, (1<<shift)), with the exception of
563 // shift == bw-1.
564 if (match(V, P: m_NSWShl(L: m_Value(V&: Op0), R: m_ConstantInt(CI)))) {
565 uint64_t Shift = CI->getValue().getLimitedValue();
566 if (Shift < Ty->getIntegerBitWidth() - 1) {
567 assert(Shift < 64 && "Would overflow");
568 auto Result = decompose(V: Op0, Preconditions, IsSigned, DL);
569 if (!Result.mul(Factor: int64_t(1) << Shift))
570 return Result;
571 return V;
572 }
573 }
574
575 return V;
576 }
577
578 if (auto *CI = dyn_cast<ConstantInt>(Val: V)) {
579 if (CI->uge(Num: MaxConstraintValue))
580 return V;
581 return int64_t(CI->getZExtValue());
582 }
583
584 Value *Op0;
585 if (match(V, P: m_ZExt(Op: m_Value(V&: Op0)))) {
586 V = Op0;
587 } else if (match(V, P: m_SExt(Op: m_Value(V&: Op0)))) {
588 V = Op0;
589 Preconditions.emplace_back(Args: CmpInst::ICMP_SGE, Args&: Op0,
590 Args: ConstantInt::get(Ty: Op0->getType(), V: 0));
591 } else if (auto *Trunc = dyn_cast<TruncInst>(Val: V)) {
592 if (Trunc->getSrcTy()->getScalarSizeInBits() <= 64) {
593 if (Trunc->hasNoUnsignedWrap() || Trunc->hasNoSignedWrap()) {
594 V = Trunc->getOperand(i_nocapture: 0);
595 if (!Trunc->hasNoUnsignedWrap())
596 Preconditions.emplace_back(Args: CmpInst::ICMP_SGE, Args&: V,
597 Args: ConstantInt::get(Ty: V->getType(), V: 0));
598 }
599 }
600 }
601
602 Value *Op1;
603 ConstantInt *CI;
604 if (match(V, P: m_NUWAdd(L: m_Value(V&: Op0), R: m_Value(V&: Op1)))) {
605 if (auto Decomp = MergeResults(Op0, Op1, IsSigned))
606 return *Decomp;
607 return V;
608 }
609
610 if (match(V, P: m_Add(L: m_Value(V&: Op0), R: m_ConstantInt(CI))) && CI->isNegative() &&
611 canUseSExt(CI)) {
612 Preconditions.emplace_back(
613 Args: CmpInst::ICMP_UGE, Args&: Op0,
614 Args: ConstantInt::get(Ty: Op0->getType(), V: CI->getSExtValue() * -1));
615 if (auto Decomp = MergeResults(Op0, CI, true))
616 return *Decomp;
617 return V;
618 }
619
620 if (match(V, P: m_NSWAdd(L: m_Value(V&: Op0), R: m_Value(V&: Op1)))) {
621 if (!isKnownNonNegative(V: Op0, SQ: DL))
622 Preconditions.emplace_back(Args: CmpInst::ICMP_SGE, Args&: Op0,
623 Args: ConstantInt::get(Ty: Op0->getType(), V: 0));
624 if (!isKnownNonNegative(V: Op1, SQ: DL))
625 Preconditions.emplace_back(Args: CmpInst::ICMP_SGE, Args&: Op1,
626 Args: ConstantInt::get(Ty: Op1->getType(), V: 0));
627
628 if (auto Decomp = MergeResults(Op0, Op1, IsSigned))
629 return *Decomp;
630 return V;
631 }
632
633 // Decompose or as an add if there are no common bits between the operands.
634 if (match(V, P: m_DisjointOr(L: m_Value(V&: Op0), R: m_ConstantInt(CI)))) {
635 if (auto Decomp = MergeResults(Op0, CI, IsSigned))
636 return *Decomp;
637 return V;
638 }
639
640 if (match(V, P: m_NUWShl(L: m_Value(V&: Op1), R: m_ConstantInt(CI))) && canUseSExt(CI)) {
641 if (CI->getSExtValue() < 0 || CI->getSExtValue() >= 64)
642 return V;
643 auto Result = decompose(V: Op1, Preconditions, IsSigned, DL);
644 if (!Result.mul(Factor: int64_t{1} << CI->getSExtValue()))
645 return Result;
646 return V;
647 }
648
649 if (match(V, P: m_NUWMul(L: m_Value(V&: Op1), R: m_ConstantInt(CI))) && canUseSExt(CI) &&
650 (!CI->isNegative())) {
651 auto Result = decompose(V: Op1, Preconditions, IsSigned, DL);
652 if (!Result.mul(Factor: CI->getSExtValue()))
653 return Result;
654 return V;
655 }
656
657 if (match(V, P: m_NUWSub(L: m_Value(V&: Op0), R: m_Value(V&: Op1)))) {
658 auto ResA = decompose(V: Op0, Preconditions, IsSigned, DL);
659 auto ResB = decompose(V: Op1, Preconditions, IsSigned, DL);
660 if (!ResA.sub(Other: ResB))
661 return ResA;
662 return V;
663 }
664
665 return V;
666}
667
668ConstraintTy
669ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
670 SmallVectorImpl<Value *> &NewVariables,
671 bool ForceSignedSystem) const {
672 assert(NewVariables.empty() && "NewVariables must be empty when passed in");
673 assert((!ForceSignedSystem || CmpInst::isEquality(Pred)) &&
674 "signed system can only be forced on eq/ne");
675
676 bool IsEq = false;
677 bool IsNe = false;
678
679 // Try to convert Pred to one of ULE/ULT/SLE/SLT.
680 switch (Pred) {
681 case CmpInst::ICMP_UGT:
682 case CmpInst::ICMP_UGE:
683 case CmpInst::ICMP_SGT:
684 case CmpInst::ICMP_SGE: {
685 Pred = CmpInst::getSwappedPredicate(pred: Pred);
686 std::swap(a&: Op0, b&: Op1);
687 break;
688 }
689 case CmpInst::ICMP_EQ:
690 if (!ForceSignedSystem && match(V: Op1, P: m_Zero())) {
691 Pred = CmpInst::ICMP_ULE;
692 } else {
693 IsEq = true;
694 Pred = CmpInst::ICMP_ULE;
695 }
696 break;
697 case CmpInst::ICMP_NE:
698 if (!ForceSignedSystem && match(V: Op1, P: m_Zero())) {
699 Pred = CmpInst::getSwappedPredicate(pred: CmpInst::ICMP_UGT);
700 std::swap(a&: Op0, b&: Op1);
701 } else {
702 IsNe = true;
703 Pred = CmpInst::ICMP_ULE;
704 }
705 break;
706 default:
707 break;
708 }
709
710 if (Pred != CmpInst::ICMP_ULE && Pred != CmpInst::ICMP_ULT &&
711 Pred != CmpInst::ICMP_SLE && Pred != CmpInst::ICMP_SLT)
712 return {};
713
714 SmallVector<ConditionTy, 4> Preconditions;
715 bool IsSigned = ForceSignedSystem || CmpInst::isSigned(Pred);
716 auto &Value2Index = getValue2Index(Signed: IsSigned);
717 auto ADec = decompose(V: Op0->stripPointerCastsSameRepresentation(),
718 Preconditions, IsSigned, DL);
719 auto BDec = decompose(V: Op1->stripPointerCastsSameRepresentation(),
720 Preconditions, IsSigned, DL);
721 int64_t Offset1 = ADec.Offset;
722 int64_t Offset2 = BDec.Offset;
723 Offset1 *= -1;
724
725 auto &VariablesA = ADec.Vars;
726 auto &VariablesB = BDec.Vars;
727
728 // First try to look up \p V in Value2Index and NewVariables. Otherwise add a
729 // new entry to NewVariables.
730 SmallDenseMap<Value *, unsigned> NewIndexMap;
731 auto GetOrAddIndex = [&Value2Index, &NewVariables,
732 &NewIndexMap](Value *V) -> unsigned {
733 auto V2I = Value2Index.find(Val: V);
734 if (V2I != Value2Index.end())
735 return V2I->second;
736 auto [It, Inserted] = NewIndexMap.try_emplace(
737 Key: V, Args: Value2Index.size() + NewVariables.size() + 1);
738 if (Inserted)
739 NewVariables.push_back(Elt: V);
740 return It->second;
741 };
742
743 // Make sure all variables have entries in Value2Index or NewVariables.
744 for (const auto &KV : concat<DecompEntry>(Ranges&: VariablesA, Ranges&: VariablesB))
745 GetOrAddIndex(KV.Variable);
746
747 // Build result constraint, by first adding all coefficients from A and then
748 // subtracting all coefficients from B.
749 ConstraintTy Res(
750 SmallVector<int64_t, 8>(Value2Index.size() + NewVariables.size() + 1, 0),
751 IsSigned, IsEq, IsNe);
752 auto &R = Res.Coefficients;
753 for (const auto &KV : VariablesA)
754 R[GetOrAddIndex(KV.Variable)] += KV.Coefficient;
755
756 for (const auto &KV : VariablesB) {
757 auto &Coeff = R[GetOrAddIndex(KV.Variable)];
758 if (SubOverflow(X: Coeff, Y: KV.Coefficient, Result&: Coeff))
759 return {};
760 }
761
762 int64_t OffsetSum;
763 if (AddOverflow(X: Offset1, Y: Offset2, Result&: OffsetSum))
764 return {};
765 if (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT)
766 if (AddOverflow(X: OffsetSum, Y: int64_t(-1), Result&: OffsetSum))
767 return {};
768 R[0] = OffsetSum;
769 Res.Preconditions = std::move(Preconditions);
770
771 // Remove any (Coefficient, Variable) entry where the Coefficient is 0 for new
772 // variables.
773 while (!NewVariables.empty()) {
774 int64_t Last = R.back();
775 if (Last != 0)
776 break;
777 R.pop_back();
778 Value *RemovedV = NewVariables.pop_back_val();
779 NewIndexMap.erase(Val: RemovedV);
780 }
781
782 return Res;
783}
784
785ConstraintTy ConstraintInfo::getConstraintForSolving(CmpInst::Predicate Pred,
786 Value *Op0,
787 Value *Op1) const {
788 Constant *NullC = Constant::getNullValue(Ty: Op0->getType());
789 // Handle trivially true compares directly to avoid adding V UGE 0 constraints
790 // for all variables in the unsigned system.
791 if ((Pred == CmpInst::ICMP_ULE && Op0 == NullC) ||
792 (Pred == CmpInst::ICMP_UGE && Op1 == NullC)) {
793 auto &Value2Index = getValue2Index(Signed: false);
794 // Return constraint that's trivially true.
795 return ConstraintTy(SmallVector<int64_t, 8>(Value2Index.size(), 0), false,
796 false, false);
797 }
798
799 // If both operands are known to be non-negative, change signed predicates to
800 // unsigned ones. This increases the reasoning effectiveness in combination
801 // with the signed <-> unsigned transfer logic.
802 if (CmpInst::isSigned(Pred) &&
803 isKnownNonNegative(V: Op0, SQ: DL, /*Depth=*/MaxAnalysisRecursionDepth - 1) &&
804 isKnownNonNegative(V: Op1, SQ: DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
805 Pred = ICmpInst::getUnsignedPredicate(Pred);
806
807 SmallVector<Value *> NewVariables;
808 ConstraintTy R = getConstraint(Pred, Op0, Op1, NewVariables);
809 if (!NewVariables.empty())
810 return {};
811 return R;
812}
813
814bool ConstraintTy::isValid(const ConstraintInfo &Info) const {
815 return Coefficients.size() > 0 &&
816 all_of(Range: Preconditions, P: [&Info](const ConditionTy &C) {
817 return Info.doesHold(Pred: C.Pred, A: C.Op0, B: C.Op1);
818 });
819}
820
821std::optional<bool>
822ConstraintTy::isImpliedBy(const ConstraintSystem &CS) const {
823 bool IsConditionImplied = CS.isConditionImplied(R: Coefficients);
824
825 if (IsEq || IsNe) {
826 auto NegatedOrEqual = ConstraintSystem::negateOrEqual(R: Coefficients);
827 bool IsNegatedOrEqualImplied =
828 !NegatedOrEqual.empty() && CS.isConditionImplied(R: NegatedOrEqual);
829
830 // In order to check that `%a == %b` is true (equality), both conditions `%a
831 // >= %b` and `%a <= %b` must hold true. When checking for equality (`IsEq`
832 // is true), we return true if they both hold, false in the other cases.
833 if (IsConditionImplied && IsNegatedOrEqualImplied)
834 return IsEq;
835
836 auto Negated = ConstraintSystem::negate(R: Coefficients);
837 bool IsNegatedImplied = !Negated.empty() && CS.isConditionImplied(R: Negated);
838
839 auto StrictLessThan = ConstraintSystem::toStrictLessThan(R: Coefficients);
840 bool IsStrictLessThanImplied =
841 !StrictLessThan.empty() && CS.isConditionImplied(R: StrictLessThan);
842
843 // In order to check that `%a != %b` is true (non-equality), either
844 // condition `%a > %b` or `%a < %b` must hold true. When checking for
845 // non-equality (`IsNe` is true), we return true if one of the two holds,
846 // false in the other cases.
847 if (IsNegatedImplied || IsStrictLessThanImplied)
848 return IsNe;
849
850 return std::nullopt;
851 }
852
853 if (IsConditionImplied)
854 return true;
855
856 auto Negated = ConstraintSystem::negate(R: Coefficients);
857 auto IsNegatedImplied = !Negated.empty() && CS.isConditionImplied(R: Negated);
858 if (IsNegatedImplied)
859 return false;
860
861 // Neither the condition nor its negated holds, did not prove anything.
862 return std::nullopt;
863}
864
865bool ConstraintInfo::doesHold(CmpInst::Predicate Pred, Value *A,
866 Value *B) const {
867 auto R = getConstraintForSolving(Pred, Op0: A, Op1: B);
868 return R.isValid(Info: *this) &&
869 getCS(Signed: R.IsSigned).isConditionImplied(R: R.Coefficients);
870}
871
872void ConstraintInfo::transferToOtherSystem(
873 CmpInst::Predicate Pred, Value *A, Value *B, unsigned NumIn,
874 unsigned NumOut, SmallVectorImpl<StackEntry> &DFSInStack) {
875 auto IsKnownNonNegative = [this](Value *V) {
876 return doesHold(Pred: CmpInst::ICMP_SGE, A: V, B: ConstantInt::get(Ty: V->getType(), V: 0)) ||
877 isKnownNonNegative(V, SQ: DL, /*Depth=*/MaxAnalysisRecursionDepth - 1);
878 };
879 // Check if we can combine facts from the signed and unsigned systems to
880 // derive additional facts.
881 if (!A->getType()->isIntegerTy())
882 return;
883 // FIXME: This currently depends on the order we add facts. Ideally we
884 // would first add all known facts and only then try to add additional
885 // facts.
886 switch (Pred) {
887 default:
888 break;
889 case CmpInst::ICMP_ULT:
890 case CmpInst::ICMP_ULE:
891 // If B is a signed positive constant, then A >=s 0 and A <s (or <=s) B.
892 if (IsKnownNonNegative(B)) {
893 addFact(Pred: CmpInst::ICMP_SGE, A, B: ConstantInt::get(Ty: B->getType(), V: 0), NumIn,
894 NumOut, DFSInStack);
895 addFact(Pred: ICmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
896 DFSInStack);
897 }
898 break;
899 case CmpInst::ICMP_UGE:
900 case CmpInst::ICMP_UGT:
901 // If A is a signed positive constant, then B >=s 0 and A >s (or >=s) B.
902 if (IsKnownNonNegative(A)) {
903 addFact(Pred: CmpInst::ICMP_SGE, A: B, B: ConstantInt::get(Ty: B->getType(), V: 0), NumIn,
904 NumOut, DFSInStack);
905 addFact(Pred: ICmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
906 DFSInStack);
907 }
908 break;
909 case CmpInst::ICMP_SLT:
910 if (IsKnownNonNegative(A))
911 addFact(Pred: CmpInst::ICMP_ULT, A, B, NumIn, NumOut, DFSInStack);
912 break;
913 case CmpInst::ICMP_SGT: {
914 if (doesHold(Pred: CmpInst::ICMP_SGE, A: B, B: Constant::getAllOnesValue(Ty: B->getType())))
915 addFact(Pred: CmpInst::ICMP_UGE, A, B: ConstantInt::get(Ty: B->getType(), V: 0), NumIn,
916 NumOut, DFSInStack);
917 if (IsKnownNonNegative(B))
918 addFact(Pred: CmpInst::ICMP_UGT, A, B, NumIn, NumOut, DFSInStack);
919
920 break;
921 }
922 case CmpInst::ICMP_SGE:
923 if (IsKnownNonNegative(B))
924 addFact(Pred: CmpInst::ICMP_UGE, A, B, NumIn, NumOut, DFSInStack);
925 break;
926 }
927}
928
929#ifndef NDEBUG
930
931static void dumpConstraint(ArrayRef<int64_t> C,
932 const DenseMap<Value *, unsigned> &Value2Index) {
933 ConstraintSystem CS(Value2Index);
934 CS.addVariableRowFill(C);
935 CS.dump();
936}
937#endif
938
939void State::addInfoForInductions(BasicBlock &BB) {
940 auto *L = LI.getLoopFor(BB: &BB);
941 if (!L || L->getHeader() != &BB)
942 return;
943
944 Value *A;
945 Value *B;
946 CmpPredicate Pred;
947
948 if (!match(V: BB.getTerminator(),
949 P: m_Br(C: m_ICmp(Pred, L: m_Value(V&: A), R: m_Value(V&: B)), T: m_Value(), F: m_Value())))
950 return;
951 PHINode *PN = dyn_cast<PHINode>(Val: A);
952 if (!PN) {
953 Pred = CmpInst::getSwappedPredicate(pred: Pred);
954 std::swap(a&: A, b&: B);
955 PN = dyn_cast<PHINode>(Val: A);
956 }
957
958 if (!PN || PN->getParent() != &BB || PN->getNumIncomingValues() != 2 ||
959 !SE.isSCEVable(Ty: PN->getType()))
960 return;
961
962 BasicBlock *InLoopSucc = nullptr;
963 if (Pred == CmpInst::ICMP_NE)
964 InLoopSucc = cast<CondBrInst>(Val: BB.getTerminator())->getSuccessor(i: 0);
965 else if (Pred == CmpInst::ICMP_EQ)
966 InLoopSucc = cast<CondBrInst>(Val: BB.getTerminator())->getSuccessor(i: 1);
967 else
968 return;
969
970 if (!L->contains(BB: InLoopSucc) || !L->isLoopExiting(BB: &BB) || InLoopSucc == &BB)
971 return;
972
973 auto *AR = dyn_cast_or_null<SCEVAddRecExpr>(Val: SE.getSCEV(V: PN));
974 BasicBlock *LoopPred = L->getLoopPredecessor();
975 if (!AR || AR->getLoop() != L || !LoopPred)
976 return;
977
978 const SCEV *StartSCEV = AR->getStart();
979 Value *StartValue = nullptr;
980 if (auto *C = dyn_cast<SCEVConstant>(Val: StartSCEV)) {
981 StartValue = C->getValue();
982 } else {
983 StartValue = PN->getIncomingValueForBlock(BB: LoopPred);
984 assert(SE.getSCEV(StartValue) == StartSCEV && "inconsistent start value");
985 }
986
987 DomTreeNode *DTN = DT.getNode(BB: InLoopSucc);
988 auto IncUnsigned = SE.getMonotonicPredicateType(LHS: AR, Pred: CmpInst::ICMP_UGT);
989 auto IncSigned = SE.getMonotonicPredicateType(LHS: AR, Pred: CmpInst::ICMP_SGT);
990 bool MonotonicallyIncreasingUnsigned =
991 IncUnsigned == ScalarEvolution::MonotonicallyIncreasing;
992 bool MonotonicallyIncreasingSigned =
993 IncSigned == ScalarEvolution::MonotonicallyIncreasing;
994 // If SCEV guarantees that AR does not wrap, PN >= StartValue can be added
995 // unconditionally.
996 if (MonotonicallyIncreasingUnsigned)
997 WorkList.push_back(
998 Elt: FactOrCheck::getConditionFact(DTN, Pred: CmpInst::ICMP_UGE, Op0: PN, Op1: StartValue));
999 if (MonotonicallyIncreasingSigned)
1000 WorkList.push_back(
1001 Elt: FactOrCheck::getConditionFact(DTN, Pred: CmpInst::ICMP_SGE, Op0: PN, Op1: StartValue));
1002
1003 APInt StepOffset;
1004 if (auto *C = dyn_cast<SCEVConstant>(Val: AR->getStepRecurrence(SE)))
1005 StepOffset = C->getAPInt();
1006 else
1007 return;
1008
1009 // Make sure the bound B is loop-invariant.
1010 if (!L->isLoopInvariant(V: B))
1011 return;
1012
1013 // Handle negative steps.
1014 if (StepOffset.isNegative()) {
1015 // TODO: Extend to allow steps > -1.
1016 if (!(-StepOffset).isOne())
1017 return;
1018
1019 // AR may wrap.
1020 // Add StartValue >= PN conditional on B <= StartValue which guarantees that
1021 // the loop exits before wrapping with a step of -1.
1022 WorkList.push_back(Elt: FactOrCheck::getConditionFact(
1023 DTN, Pred: CmpInst::ICMP_UGE, Op0: StartValue, Op1: PN,
1024 Precond: ConditionTy(CmpInst::ICMP_ULE, B, StartValue)));
1025 WorkList.push_back(Elt: FactOrCheck::getConditionFact(
1026 DTN, Pred: CmpInst::ICMP_SGE, Op0: StartValue, Op1: PN,
1027 Precond: ConditionTy(CmpInst::ICMP_SLE, B, StartValue)));
1028 // Add PN > B conditional on B <= StartValue which guarantees that the loop
1029 // exits when reaching B with a step of -1.
1030 WorkList.push_back(Elt: FactOrCheck::getConditionFact(
1031 DTN, Pred: CmpInst::ICMP_UGT, Op0: PN, Op1: B,
1032 Precond: ConditionTy(CmpInst::ICMP_ULE, B, StartValue)));
1033 WorkList.push_back(Elt: FactOrCheck::getConditionFact(
1034 DTN, Pred: CmpInst::ICMP_SGT, Op0: PN, Op1: B,
1035 Precond: ConditionTy(CmpInst::ICMP_SLE, B, StartValue)));
1036 return;
1037 }
1038
1039 // Make sure AR either steps by 1 or that the value we compare against is a
1040 // GEP based on the same start value and all offsets are a multiple of the
1041 // step size, to guarantee that the induction will reach the value.
1042 if (StepOffset.isZero() || StepOffset.isNegative())
1043 return;
1044
1045 if (!StepOffset.isOne()) {
1046 // Check whether B-Start is known to be a multiple of StepOffset.
1047 const SCEV *BMinusStart = SE.getMinusSCEV(LHS: SE.getSCEV(V: B), RHS: StartSCEV);
1048 if (isa<SCEVCouldNotCompute>(Val: BMinusStart) ||
1049 !SE.getConstantMultiple(S: BMinusStart).urem(RHS: StepOffset).isZero())
1050 return;
1051 }
1052
1053 // AR may wrap. Add PN >= StartValue conditional on StartValue <= B which
1054 // guarantees that the loop exits before wrapping in combination with the
1055 // restrictions on B and the step above.
1056 if (!MonotonicallyIncreasingUnsigned)
1057 WorkList.push_back(Elt: FactOrCheck::getConditionFact(
1058 DTN, Pred: CmpInst::ICMP_UGE, Op0: PN, Op1: StartValue,
1059 Precond: ConditionTy(CmpInst::ICMP_ULE, StartValue, B)));
1060 if (!MonotonicallyIncreasingSigned)
1061 WorkList.push_back(Elt: FactOrCheck::getConditionFact(
1062 DTN, Pred: CmpInst::ICMP_SGE, Op0: PN, Op1: StartValue,
1063 Precond: ConditionTy(CmpInst::ICMP_SLE, StartValue, B)));
1064
1065 WorkList.push_back(Elt: FactOrCheck::getConditionFact(
1066 DTN, Pred: CmpInst::ICMP_ULT, Op0: PN, Op1: B,
1067 Precond: ConditionTy(CmpInst::ICMP_ULE, StartValue, B)));
1068 WorkList.push_back(Elt: FactOrCheck::getConditionFact(
1069 DTN, Pred: CmpInst::ICMP_SLT, Op0: PN, Op1: B,
1070 Precond: ConditionTy(CmpInst::ICMP_SLE, StartValue, B)));
1071
1072 // Try to add condition from header to the dedicated exit blocks. When exiting
1073 // either with EQ or NE in the header, we know that the induction value must
1074 // be u<= B, as other exits may only exit earlier.
1075 assert(!StepOffset.isNegative() && "induction must be increasing");
1076 assert((Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) &&
1077 "unsupported predicate");
1078 ConditionTy Precond = {CmpInst::ICMP_ULE, StartValue, B};
1079 SmallVector<BasicBlock *> ExitBBs;
1080 L->getExitBlocks(ExitBlocks&: ExitBBs);
1081 for (BasicBlock *EB : ExitBBs) {
1082 // Bail out on non-dedicated exits.
1083 if (DT.dominates(A: &BB, B: EB)) {
1084 WorkList.emplace_back(Args: FactOrCheck::getConditionFact(
1085 DTN: DT.getNode(BB: EB), Pred: CmpInst::ICMP_ULE, Op0: A, Op1: B, Precond));
1086 }
1087 }
1088}
1089
1090static bool getConstraintFromMemoryAccess(GetElementPtrInst &GEP,
1091 uint64_t AccessSize,
1092 CmpPredicate &Pred, Value *&A,
1093 Value *&B, const DataLayout &DL,
1094 const TargetLibraryInfo &TLI) {
1095 auto Offset = collectOffsets(GEP&: cast<GEPOperator>(Val&: GEP), DL);
1096 if (!Offset.NW.hasNoUnsignedWrap())
1097 return false;
1098
1099 if (Offset.VariableOffsets.size() != 1)
1100 return false;
1101
1102 uint64_t BitWidth = Offset.ConstantOffset.getBitWidth();
1103 auto &[Index, Scale] = Offset.VariableOffsets.front();
1104 // Bail out on non-canonical GEPs.
1105 if (Index->getType()->getScalarSizeInBits() != BitWidth)
1106 return false;
1107
1108 ObjectSizeOpts Opts;
1109 // Workaround for gep inbounds, ptr null, idx.
1110 Opts.NullIsUnknownSize = true;
1111 // Be conservative since we are not clear on whether an out of bounds access
1112 // to the padding is UB or not.
1113 Opts.RoundToAlign = true;
1114 std::optional<TypeSize> Size =
1115 getBaseObjectSize(Ptr: Offset.BasePtr, DL, TLI: &TLI, Opts);
1116 if (!Size || Size->isScalable())
1117 return false;
1118
1119 // Index * Scale + ConstOffset + AccessSize <= AllocSize
1120 // With nuw flag, we know that the index addition doesn't have unsigned wrap.
1121 // If (AllocSize - (ConstOffset + AccessSize)) wraps around, there is no valid
1122 // value for Index.
1123 APInt MaxIndex = (APInt(BitWidth, Size->getFixedValue() - AccessSize,
1124 /*isSigned=*/false, /*implicitTrunc=*/true) -
1125 Offset.ConstantOffset)
1126 .udiv(RHS: Scale);
1127 Pred = ICmpInst::ICMP_ULE;
1128 A = Index;
1129 B = ConstantInt::get(Ty: Index->getType(), V: MaxIndex);
1130 return true;
1131}
1132
1133void State::addInfoFor(BasicBlock &BB) {
1134 addInfoForInductions(BB);
1135 auto &DL = BB.getDataLayout();
1136
1137 Value *A, *B;
1138 CmpPredicate Pred;
1139 // True as long as the current instruction is guaranteed to execute.
1140 bool GuaranteedToExecute = true;
1141 // Queue conditions and assumes.
1142 for (Instruction &I : BB) {
1143 if (auto *Cmp = dyn_cast<ICmpInst>(Val: &I)) {
1144 for (Use &U : Cmp->uses()) {
1145 auto *UserI = getContextInstForUse(U);
1146 auto *DTN = DT.getNode(BB: UserI->getParent());
1147 if (!DTN)
1148 continue;
1149 WorkList.push_back(Elt: FactOrCheck::getCheck(DTN, U: &U));
1150 }
1151 continue;
1152 }
1153
1154 auto AddFactFromMemoryAccess = [&](Value *Ptr, Type *AccessType) {
1155 auto *GEP = dyn_cast<GetElementPtrInst>(Val: Ptr);
1156 if (!GEP)
1157 return;
1158 TypeSize AccessSize = DL.getTypeStoreSize(Ty: AccessType);
1159 if (!AccessSize.isFixed())
1160 return;
1161 if (GuaranteedToExecute) {
1162 if (getConstraintFromMemoryAccess(GEP&: *GEP, AccessSize: AccessSize.getFixedValue(),
1163 Pred, A, B, DL, TLI)) {
1164 // The memory access is guaranteed to execute when BB is entered,
1165 // hence the constraint holds on entry to BB.
1166 WorkList.emplace_back(Args: FactOrCheck::getConditionFact(
1167 DTN: DT.getNode(BB: I.getParent()), Pred, Op0: A, Op1: B));
1168 }
1169 } else {
1170 WorkList.emplace_back(
1171 Args: FactOrCheck::getInstFact(DTN: DT.getNode(BB: I.getParent()), Inst: &I));
1172 }
1173 };
1174
1175 if (auto *LI = dyn_cast<LoadInst>(Val: &I)) {
1176 if (!LI->isVolatile())
1177 AddFactFromMemoryAccess(LI->getPointerOperand(), LI->getAccessType());
1178 }
1179 if (auto *SI = dyn_cast<StoreInst>(Val: &I)) {
1180 if (!SI->isVolatile())
1181 AddFactFromMemoryAccess(SI->getPointerOperand(), SI->getAccessType());
1182 }
1183
1184 auto *II = dyn_cast<IntrinsicInst>(Val: &I);
1185 Intrinsic::ID ID = II ? II->getIntrinsicID() : Intrinsic::not_intrinsic;
1186 switch (ID) {
1187 case Intrinsic::assume: {
1188 if (!match(V: I.getOperand(i: 0), P: m_ICmpLike(Pred, L: m_Value(V&: A), R: m_Value(V&: B))))
1189 break;
1190 if (GuaranteedToExecute) {
1191 // The assume is guaranteed to execute when BB is entered, hence Cond
1192 // holds on entry to BB.
1193 WorkList.emplace_back(Args: FactOrCheck::getConditionFact(
1194 DTN: DT.getNode(BB: I.getParent()), Pred, Op0: A, Op1: B));
1195 } else {
1196 WorkList.emplace_back(
1197 Args: FactOrCheck::getInstFact(DTN: DT.getNode(BB: I.getParent()), Inst: &I));
1198 }
1199 break;
1200 }
1201 // Enqueue ssub_with_overflow for simplification.
1202 case Intrinsic::ssub_with_overflow:
1203 case Intrinsic::ucmp:
1204 case Intrinsic::scmp:
1205 WorkList.push_back(
1206 Elt: FactOrCheck::getCheck(DTN: DT.getNode(BB: &BB), CI: cast<CallInst>(Val: &I)));
1207 break;
1208 // Enqueue the intrinsics to add extra info.
1209 case Intrinsic::umin:
1210 case Intrinsic::umax:
1211 case Intrinsic::smin:
1212 case Intrinsic::smax:
1213 // TODO: handle llvm.abs as well
1214 WorkList.push_back(
1215 Elt: FactOrCheck::getCheck(DTN: DT.getNode(BB: &BB), CI: cast<CallInst>(Val: &I)));
1216 [[fallthrough]];
1217 case Intrinsic::uadd_sat:
1218 case Intrinsic::usub_sat:
1219 // TODO: Check if it is possible to instead only added the min/max facts
1220 // when simplifying uses of the min/max intrinsics.
1221 if (!isGuaranteedNotToBePoison(V: &I))
1222 break;
1223 [[fallthrough]];
1224 case Intrinsic::abs:
1225 WorkList.push_back(Elt: FactOrCheck::getInstFact(DTN: DT.getNode(BB: &BB), Inst: &I));
1226 break;
1227 }
1228
1229 // Add facts from unsigned division and remainder.
1230 // urem x, n: result < n and result <= x
1231 // udiv x, n: result <= x
1232 if (auto *BO = dyn_cast<BinaryOperator>(Val: &I)) {
1233 if ((BO->getOpcode() == Instruction::URem ||
1234 BO->getOpcode() == Instruction::UDiv) &&
1235 isGuaranteedNotToBePoison(V: BO))
1236 WorkList.push_back(Elt: FactOrCheck::getInstFact(DTN: DT.getNode(BB: &BB), Inst: BO));
1237 }
1238
1239 GuaranteedToExecute &= isGuaranteedToTransferExecutionToSuccessor(I: &I);
1240 }
1241
1242 if (auto *Switch = dyn_cast<SwitchInst>(Val: BB.getTerminator())) {
1243 for (auto &Case : Switch->cases()) {
1244 BasicBlock *Succ = Case.getCaseSuccessor();
1245 Value *V = Case.getCaseValue();
1246 if (!canAddSuccessor(BB, Succ))
1247 continue;
1248 WorkList.emplace_back(Args: FactOrCheck::getConditionFact(
1249 DTN: DT.getNode(BB: Succ), Pred: CmpInst::ICMP_EQ, Op0: Switch->getCondition(), Op1: V));
1250 }
1251 return;
1252 }
1253
1254 auto *Br = dyn_cast<CondBrInst>(Val: BB.getTerminator());
1255 if (!Br)
1256 return;
1257
1258 Value *Cond = Br->getCondition();
1259
1260 // If the condition is a chain of ORs/AND and the successor only has the
1261 // current block as predecessor, queue conditions for the successor.
1262 Value *Op0, *Op1;
1263 if (match(V: Cond, P: m_LogicalOr(L: m_Value(V&: Op0), R: m_Value(V&: Op1))) ||
1264 match(V: Cond, P: m_LogicalAnd(L: m_Value(V&: Op0), R: m_Value(V&: Op1)))) {
1265 bool IsOr = match(V: Cond, P: m_LogicalOr());
1266 bool IsAnd = match(V: Cond, P: m_LogicalAnd());
1267 // If there's a select that matches both AND and OR, we need to commit to
1268 // one of the options. Arbitrarily pick OR.
1269 if (IsOr && IsAnd)
1270 IsAnd = false;
1271
1272 BasicBlock *Successor = Br->getSuccessor(i: IsOr ? 1 : 0);
1273 if (canAddSuccessor(BB, Succ: Successor)) {
1274 SmallVector<Value *> CondWorkList;
1275 SmallPtrSet<Value *, 8> SeenCond;
1276 auto QueueValue = [&CondWorkList, &SeenCond](Value *V) {
1277 if (SeenCond.insert(Ptr: V).second)
1278 CondWorkList.push_back(Elt: V);
1279 };
1280 QueueValue(Op1);
1281 QueueValue(Op0);
1282 while (!CondWorkList.empty()) {
1283 Value *Cur = CondWorkList.pop_back_val();
1284 if (match(V: Cur, P: m_ICmpLike(Pred, L: m_Value(V&: A), R: m_Value(V&: B)))) {
1285 WorkList.emplace_back(Args: FactOrCheck::getConditionFact(
1286 DTN: DT.getNode(BB: Successor),
1287 Pred: IsOr ? CmpPredicate::getInverse(P: Pred) : Pred, Op0: A, Op1: B));
1288 continue;
1289 }
1290 if (IsOr && match(V: Cur, P: m_LogicalOr(L: m_Value(V&: Op0), R: m_Value(V&: Op1)))) {
1291 QueueValue(Op1);
1292 QueueValue(Op0);
1293 continue;
1294 }
1295 if (IsAnd && match(V: Cur, P: m_LogicalAnd(L: m_Value(V&: Op0), R: m_Value(V&: Op1)))) {
1296 QueueValue(Op1);
1297 QueueValue(Op0);
1298 continue;
1299 }
1300 }
1301 }
1302 return;
1303 }
1304
1305 if (!match(V: Br->getCondition(), P: m_ICmpLike(Pred, L: m_Value(V&: A), R: m_Value(V&: B))))
1306 return;
1307 if (canAddSuccessor(BB, Succ: Br->getSuccessor(i: 0)))
1308 WorkList.emplace_back(Args: FactOrCheck::getConditionFact(
1309 DTN: DT.getNode(BB: Br->getSuccessor(i: 0)), Pred, Op0: A, Op1: B));
1310 if (canAddSuccessor(BB, Succ: Br->getSuccessor(i: 1)))
1311 WorkList.emplace_back(Args: FactOrCheck::getConditionFact(
1312 DTN: DT.getNode(BB: Br->getSuccessor(i: 1)), Pred: CmpPredicate::getInverse(P: Pred), Op0: A, Op1: B));
1313}
1314
1315#ifndef NDEBUG
1316static void dumpUnpackedICmp(raw_ostream &OS, ICmpInst::Predicate Pred,
1317 Value *LHS, Value *RHS) {
1318 OS << "icmp " << Pred << ' ';
1319 LHS->printAsOperand(OS, /*PrintType=*/true);
1320 OS << ", ";
1321 RHS->printAsOperand(OS, /*PrintType=*/false);
1322}
1323#endif
1324
1325namespace {
1326/// Helper to keep track of a condition and if it should be treated as negated
1327/// for reproducer construction.
1328/// Pred == Predicate::BAD_ICMP_PREDICATE indicates that this entry is a
1329/// placeholder to keep the ReproducerCondStack in sync with DFSInStack.
1330struct ReproducerEntry {
1331 ICmpInst::Predicate Pred;
1332 Value *LHS;
1333 Value *RHS;
1334
1335 ReproducerEntry(ICmpInst::Predicate Pred, Value *LHS, Value *RHS)
1336 : Pred(Pred), LHS(LHS), RHS(RHS) {}
1337};
1338} // namespace
1339
1340/// Helper function to generate a reproducer function for simplifying \p Cond.
1341/// The reproducer function contains a series of @llvm.assume calls, one for
1342/// each condition in \p Stack. For each condition, the operand instruction are
1343/// cloned until we reach operands that have an entry in \p Value2Index. Those
1344/// will then be added as function arguments. \p DT is used to order cloned
1345/// instructions. The reproducer function will get added to \p M, if it is
1346/// non-null. Otherwise no reproducer function is generated.
1347static void generateReproducer(CmpInst *Cond, Module *M,
1348 ArrayRef<ReproducerEntry> Stack,
1349 ConstraintInfo &Info, DominatorTree &DT) {
1350 if (!M)
1351 return;
1352
1353 LLVMContext &Ctx = Cond->getContext();
1354
1355 LLVM_DEBUG(dbgs() << "Creating reproducer for " << *Cond << "\n");
1356
1357 ValueToValueMapTy Old2New;
1358 SmallVector<Value *> Args;
1359 SmallPtrSet<Value *, 8> Seen;
1360 // Traverse Cond and its operands recursively until we reach a value that's in
1361 // Value2Index or not an instruction, or not a operation that
1362 // ConstraintElimination can decompose. Such values will be considered as
1363 // external inputs to the reproducer, they are collected and added as function
1364 // arguments later.
1365 auto CollectArguments = [&](ArrayRef<Value *> Ops, bool IsSigned) {
1366 auto &Value2Index = Info.getValue2Index(Signed: IsSigned);
1367 SmallVector<Value *, 4> WorkList(Ops);
1368 while (!WorkList.empty()) {
1369 Value *V = WorkList.pop_back_val();
1370 if (!Seen.insert(Ptr: V).second)
1371 continue;
1372 if (Old2New.find(Val: V) != Old2New.end())
1373 continue;
1374 if (isa<Constant>(Val: V))
1375 continue;
1376
1377 auto *I = dyn_cast<Instruction>(Val: V);
1378 if (Value2Index.contains(Val: V) || !I ||
1379 !isa<CmpInst, BinaryOperator, GEPOperator, CastInst>(Val: V)) {
1380 Old2New[V] = V;
1381 Args.push_back(Elt: V);
1382 LLVM_DEBUG(dbgs() << " found external input " << *V << "\n");
1383 } else {
1384 append_range(C&: WorkList, R: I->operands());
1385 }
1386 }
1387 };
1388
1389 for (auto &Entry : Stack)
1390 if (Entry.Pred != ICmpInst::BAD_ICMP_PREDICATE)
1391 CollectArguments({Entry.LHS, Entry.RHS}, ICmpInst::isSigned(Pred: Entry.Pred));
1392 CollectArguments(Cond, ICmpInst::isSigned(Pred: Cond->getPredicate()));
1393
1394 SmallVector<Type *> ParamTys;
1395 for (auto *P : Args)
1396 ParamTys.push_back(Elt: P->getType());
1397
1398 FunctionType *FTy = FunctionType::get(Result: Cond->getType(), Params: ParamTys,
1399 /*isVarArg=*/false);
1400 Function *F = Function::Create(Ty: FTy, Linkage: Function::ExternalLinkage,
1401 N: Cond->getModule()->getName() +
1402 Cond->getFunction()->getName() + "repro",
1403 M);
1404 // Add arguments to the reproducer function for each external value collected.
1405 for (unsigned I = 0; I < Args.size(); ++I) {
1406 F->getArg(i: I)->setName(Args[I]->getName());
1407 Old2New[Args[I]] = F->getArg(i: I);
1408 }
1409
1410 BasicBlock *Entry = BasicBlock::Create(Context&: Ctx, Name: "entry", Parent: F);
1411 IRBuilder<> Builder(Entry);
1412 Builder.CreateRet(V: Builder.getTrue());
1413 Builder.SetInsertPoint(Entry->getTerminator());
1414
1415 // Clone instructions in \p Ops and their operands recursively until reaching
1416 // an value in Value2Index (external input to the reproducer). Update Old2New
1417 // mapping for the original and cloned instructions. Sort instructions to
1418 // clone by dominance, then insert the cloned instructions in the function.
1419 auto CloneInstructions = [&](ArrayRef<Value *> Ops, bool IsSigned) {
1420 SmallVector<Value *, 4> WorkList(Ops);
1421 SmallVector<Instruction *> ToClone;
1422 auto &Value2Index = Info.getValue2Index(Signed: IsSigned);
1423 while (!WorkList.empty()) {
1424 Value *V = WorkList.pop_back_val();
1425 if (Old2New.find(Val: V) != Old2New.end())
1426 continue;
1427
1428 auto *I = dyn_cast<Instruction>(Val: V);
1429 if (!Value2Index.contains(Val: V) && I) {
1430 Old2New[V] = nullptr;
1431 ToClone.push_back(Elt: I);
1432 append_range(C&: WorkList, R: I->operands());
1433 }
1434 }
1435
1436 sort(C&: ToClone,
1437 Comp: [&DT](Instruction *A, Instruction *B) { return DT.dominates(Def: A, User: B); });
1438 for (Instruction *I : ToClone) {
1439 Instruction *Cloned = I->clone();
1440 Old2New[I] = Cloned;
1441 Old2New[I]->setName(I->getName());
1442 Cloned->insertBefore(InsertPos: Builder.GetInsertPoint());
1443 Cloned->dropUnknownNonDebugMetadata();
1444 Cloned->setDebugLoc({});
1445 }
1446 };
1447
1448 // Materialize the assumptions for the reproducer using the entries in Stack.
1449 // That is, first clone the operands of the condition recursively until we
1450 // reach an external input to the reproducer and add them to the reproducer
1451 // function. Then add an ICmp for the condition (with the inverse predicate if
1452 // the entry is negated) and an assert using the ICmp.
1453 for (auto &Entry : Stack) {
1454 if (Entry.Pred == ICmpInst::BAD_ICMP_PREDICATE)
1455 continue;
1456
1457 LLVM_DEBUG(dbgs() << " Materializing assumption ";
1458 dumpUnpackedICmp(dbgs(), Entry.Pred, Entry.LHS, Entry.RHS);
1459 dbgs() << "\n");
1460 CloneInstructions({Entry.LHS, Entry.RHS}, CmpInst::isSigned(Pred: Entry.Pred));
1461
1462 auto *Cmp = Builder.CreateICmp(P: Entry.Pred, LHS: Entry.LHS, RHS: Entry.RHS);
1463 Builder.CreateAssumption(Cond: Cmp);
1464 }
1465
1466 // Finally, clone the condition to reproduce and remap instruction operands in
1467 // the reproducer using Old2New.
1468 CloneInstructions(Cond, CmpInst::isSigned(Pred: Cond->getPredicate()));
1469 Entry->getTerminator()->setOperand(i: 0, Val: Cond);
1470 remapInstructionsInBlocks(Blocks: {Entry}, VMap&: Old2New);
1471
1472 assert(!verifyFunction(*F, &dbgs()));
1473}
1474
1475static std::optional<bool> checkCondition(CmpInst::Predicate Pred, Value *A,
1476 Value *B, Instruction *CheckInst,
1477 ConstraintInfo &Info) {
1478 LLVM_DEBUG(dbgs() << "Checking " << *CheckInst << "\n");
1479
1480 auto R = Info.getConstraintForSolving(Pred, Op0: A, Op1: B);
1481 if (R.empty() || !R.isValid(Info)) {
1482 LLVM_DEBUG(dbgs() << " failed to decompose condition\n");
1483 return std::nullopt;
1484 }
1485
1486 auto &CSToUse = Info.getCS(Signed: R.IsSigned);
1487 if (auto ImpliedCondition = R.isImpliedBy(CS: CSToUse)) {
1488 if (!DebugCounter::shouldExecute(Counter&: EliminatedCounter))
1489 return std::nullopt;
1490
1491 LLVM_DEBUG({
1492 dbgs() << "Condition ";
1493 dumpUnpackedICmp(
1494 dbgs(), *ImpliedCondition ? Pred : CmpInst::getInversePredicate(Pred),
1495 A, B);
1496 dbgs() << " implied by dominating constraints\n";
1497 CSToUse.dump();
1498 });
1499 return ImpliedCondition;
1500 }
1501
1502 return std::nullopt;
1503}
1504
1505static bool checkAndReplaceCondition(
1506 ICmpInst *Cmp, ConstraintInfo &Info, unsigned NumIn, unsigned NumOut,
1507 Instruction *ContextInst, Module *ReproducerModule,
1508 ArrayRef<ReproducerEntry> ReproducerCondStack, DominatorTree &DT,
1509 SmallVectorImpl<Instruction *> &ToRemove) {
1510 auto ReplaceCmpWithConstant = [&](CmpInst *Cmp, bool IsTrue) {
1511 generateReproducer(Cond: Cmp, M: ReproducerModule, Stack: ReproducerCondStack, Info, DT);
1512 Constant *ConstantC = ConstantInt::getBool(
1513 Ty: CmpInst::makeCmpResultType(opnd_type: Cmp->getType()), V: IsTrue);
1514 bool Changed = Cmp->replaceUsesWithIf(New: ConstantC, ShouldReplace: [&](Use &U) {
1515 auto *UserI = getContextInstForUse(U);
1516 auto *DTN = DT.getNode(BB: UserI->getParent());
1517 if (!DTN || DTN->getDFSNumIn() < NumIn || DTN->getDFSNumOut() > NumOut)
1518 return false;
1519 if (UserI->getParent() == ContextInst->getParent() &&
1520 UserI->comesBefore(Other: ContextInst))
1521 return false;
1522
1523 // Conditions in an assume trivially simplify to true. Skip uses
1524 // in assume calls to not destroy the available information.
1525 auto *II = dyn_cast<IntrinsicInst>(Val: U.getUser());
1526 return !II || II->getIntrinsicID() != Intrinsic::assume;
1527 });
1528 NumCondsRemoved++;
1529
1530 // Update the debug value records that satisfy the same condition used
1531 // in replaceUsesWithIf.
1532 SmallVector<DbgVariableRecord *> DVRUsers;
1533 findDbgUsers(V: Cmp, DbgVariableRecords&: DVRUsers);
1534
1535 for (auto *DVR : DVRUsers) {
1536 auto *DTN = DT.getNode(BB: DVR->getParent());
1537 if (!DTN || DTN->getDFSNumIn() < NumIn || DTN->getDFSNumOut() > NumOut)
1538 continue;
1539
1540 auto *MarkedI = DVR->getInstruction();
1541 if (MarkedI->getParent() == ContextInst->getParent() &&
1542 MarkedI->comesBefore(Other: ContextInst))
1543 continue;
1544
1545 DVR->replaceVariableLocationOp(OldValue: Cmp, NewValue: ConstantC);
1546 }
1547
1548 if (Cmp->use_empty())
1549 ToRemove.push_back(Elt: Cmp);
1550
1551 return Changed;
1552 };
1553
1554 if (auto ImpliedCondition =
1555 checkCondition(Pred: Cmp->getPredicate(), A: Cmp->getOperand(i_nocapture: 0),
1556 B: Cmp->getOperand(i_nocapture: 1), CheckInst: Cmp, Info))
1557 return ReplaceCmpWithConstant(Cmp, *ImpliedCondition);
1558
1559 // When the predicate is samesign and unsigned, we can also make use of the
1560 // signed predicate information.
1561 if (Cmp->hasSameSign() && Cmp->isUnsigned())
1562 if (auto ImpliedCondition =
1563 checkCondition(Pred: Cmp->getSignedPredicate(), A: Cmp->getOperand(i_nocapture: 0),
1564 B: Cmp->getOperand(i_nocapture: 1), CheckInst: Cmp, Info))
1565 return ReplaceCmpWithConstant(Cmp, *ImpliedCondition);
1566
1567 return false;
1568}
1569
1570static bool checkAndReplaceMinMax(MinMaxIntrinsic *MinMax, ConstraintInfo &Info,
1571 SmallVectorImpl<Instruction *> &ToRemove) {
1572 auto ReplaceMinMaxWithOperand = [&](MinMaxIntrinsic *MinMax, bool UseLHS) {
1573 // TODO: generate reproducer for min/max.
1574 MinMax->replaceAllUsesWith(V: MinMax->getOperand(i_nocapture: UseLHS ? 0 : 1));
1575 ToRemove.push_back(Elt: MinMax);
1576 return true;
1577 };
1578
1579 ICmpInst::Predicate Pred =
1580 ICmpInst::getNonStrictPredicate(pred: MinMax->getPredicate());
1581 if (auto ImpliedCondition = checkCondition(
1582 Pred, A: MinMax->getOperand(i_nocapture: 0), B: MinMax->getOperand(i_nocapture: 1), CheckInst: MinMax, Info))
1583 return ReplaceMinMaxWithOperand(MinMax, *ImpliedCondition);
1584 if (auto ImpliedCondition = checkCondition(
1585 Pred, A: MinMax->getOperand(i_nocapture: 1), B: MinMax->getOperand(i_nocapture: 0), CheckInst: MinMax, Info))
1586 return ReplaceMinMaxWithOperand(MinMax, !*ImpliedCondition);
1587 return false;
1588}
1589
1590static bool checkAndReplaceCmp(CmpIntrinsic *I, ConstraintInfo &Info,
1591 SmallVectorImpl<Instruction *> &ToRemove) {
1592 Value *LHS = I->getOperand(i_nocapture: 0);
1593 Value *RHS = I->getOperand(i_nocapture: 1);
1594 if (checkCondition(Pred: I->getGTPredicate(), A: LHS, B: RHS, CheckInst: I, Info).value_or(u: false)) {
1595 I->replaceAllUsesWith(V: ConstantInt::get(Ty: I->getType(), V: 1));
1596 ToRemove.push_back(Elt: I);
1597 return true;
1598 }
1599 if (checkCondition(Pred: I->getLTPredicate(), A: LHS, B: RHS, CheckInst: I, Info).value_or(u: false)) {
1600 I->replaceAllUsesWith(V: ConstantInt::getSigned(Ty: I->getType(), V: -1));
1601 ToRemove.push_back(Elt: I);
1602 return true;
1603 }
1604 if (checkCondition(Pred: ICmpInst::ICMP_EQ, A: LHS, B: RHS, CheckInst: I, Info).value_or(u: false)) {
1605 I->replaceAllUsesWith(V: ConstantInt::get(Ty: I->getType(), V: 0));
1606 ToRemove.push_back(Elt: I);
1607 return true;
1608 }
1609 return false;
1610}
1611
1612static void
1613removeEntryFromStack(const StackEntry &E, ConstraintInfo &Info,
1614 Module *ReproducerModule,
1615 SmallVectorImpl<ReproducerEntry> &ReproducerCondStack,
1616 SmallVectorImpl<StackEntry> &DFSInStack) {
1617 Info.popLastConstraint(Signed: E.IsSigned);
1618 // Remove variables in the system that went out of scope.
1619 auto &Mapping = Info.getValue2Index(Signed: E.IsSigned);
1620 for (Value *V : E.ValuesToRelease)
1621 Mapping.erase(Val: V);
1622 Info.popLastNVariables(Signed: E.IsSigned, N: E.ValuesToRelease.size());
1623 DFSInStack.pop_back();
1624 if (ReproducerModule)
1625 ReproducerCondStack.pop_back();
1626}
1627
1628/// Check if either the first condition of an AND or OR is implied by the
1629/// (negated in case of OR) second condition or vice versa.
1630static bool checkOrAndOpImpliedByOther(
1631 FactOrCheck &CB, ConstraintInfo &Info, Module *ReproducerModule,
1632 SmallVectorImpl<ReproducerEntry> &ReproducerCondStack,
1633 SmallVectorImpl<StackEntry> &DFSInStack,
1634 SmallVectorImpl<Instruction *> &ToRemove) {
1635 Instruction *JoinOp = CB.getContextInst();
1636 if (JoinOp->use_empty())
1637 return false;
1638
1639 CmpInst *CmpToCheck = cast<CmpInst>(Val: CB.getInstructionToSimplify());
1640 unsigned OtherOpIdx = JoinOp->getOperand(i: 0) == CmpToCheck ? 1 : 0;
1641
1642 // Don't try to simplify the first condition of a select by the second, as
1643 // this may make the select more poisonous than the original one.
1644 // TODO: check if the first operand may be poison.
1645 if (OtherOpIdx != 0 && isa<SelectInst>(Val: JoinOp))
1646 return false;
1647
1648 unsigned OldSize = DFSInStack.size();
1649 llvm::scope_exit InfoRestorer([&]() {
1650 // Remove entries again.
1651 while (OldSize < DFSInStack.size()) {
1652 StackEntry E = DFSInStack.back();
1653 removeEntryFromStack(E, Info, ReproducerModule, ReproducerCondStack,
1654 DFSInStack);
1655 }
1656 });
1657 bool IsOr = match(V: JoinOp, P: m_LogicalOr());
1658 SmallVector<Value *, 4> Worklist({JoinOp->getOperand(i: OtherOpIdx)});
1659 // Do a traversal of the AND/OR tree to add facts from leaf compares.
1660 while (!Worklist.empty()) {
1661 Value *Val = Worklist.pop_back_val();
1662 Value *LHS, *RHS;
1663 CmpPredicate Pred;
1664 if (match(V: Val, P: m_ICmp(Pred, L: m_Value(V&: LHS), R: m_Value(V&: RHS)))) {
1665 // For OR, check if the negated condition implies CmpToCheck.
1666 if (IsOr)
1667 Pred = CmpInst::getInversePredicate(pred: Pred);
1668 // Optimistically add fact from the other compares in the AND/OR.
1669 Info.addFact(Pred, A: LHS, B: RHS, NumIn: CB.NumIn, NumOut: CB.NumOut, DFSInStack);
1670 continue;
1671 }
1672 if (IsOr ? match(V: Val, P: m_LogicalOr(L: m_Value(V&: LHS), R: m_Value(V&: RHS)))
1673 : match(V: Val, P: m_LogicalAnd(L: m_Value(V&: LHS), R: m_Value(V&: RHS)))) {
1674 Worklist.push_back(Elt: LHS);
1675 Worklist.push_back(Elt: RHS);
1676 }
1677 }
1678 if (OldSize == DFSInStack.size())
1679 return false;
1680
1681 // Check if the second condition can be simplified now.
1682 if (auto ImpliedCondition =
1683 checkCondition(Pred: CmpToCheck->getPredicate(), A: CmpToCheck->getOperand(i_nocapture: 0),
1684 B: CmpToCheck->getOperand(i_nocapture: 1), CheckInst: CmpToCheck, Info)) {
1685 if (IsOr == *ImpliedCondition)
1686 JoinOp->replaceAllUsesWith(
1687 V: ConstantInt::getBool(Ty: JoinOp->getType(), V: *ImpliedCondition));
1688 else
1689 JoinOp->replaceAllUsesWith(V: JoinOp->getOperand(i: OtherOpIdx));
1690 ToRemove.push_back(Elt: JoinOp);
1691 return true;
1692 }
1693
1694 return false;
1695}
1696
1697void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B,
1698 unsigned NumIn, unsigned NumOut,
1699 SmallVectorImpl<StackEntry> &DFSInStack) {
1700 addFactImpl(Pred, A, B, NumIn, NumOut, DFSInStack, ForceSignedSystem: false);
1701 // If the Pred is eq/ne, also add the fact to signed system.
1702 if (CmpInst::isEquality(pred: Pred))
1703 addFactImpl(Pred, A, B, NumIn, NumOut, DFSInStack, ForceSignedSystem: true);
1704}
1705
1706void ConstraintInfo::addFactImpl(CmpInst::Predicate Pred, Value *A, Value *B,
1707 unsigned NumIn, unsigned NumOut,
1708 SmallVectorImpl<StackEntry> &DFSInStack,
1709 bool ForceSignedSystem) {
1710 // If the constraint has a pre-condition, skip the constraint if it does not
1711 // hold.
1712 SmallVector<Value *> NewVariables;
1713 auto R = getConstraint(Pred, Op0: A, Op1: B, NewVariables, ForceSignedSystem);
1714
1715 // TODO: Support non-equality for facts as well.
1716 if (!R.isValid(Info: *this) || R.isNe())
1717 return;
1718
1719 LLVM_DEBUG(dbgs() << "Adding '"; dumpUnpackedICmp(dbgs(), Pred, A, B);
1720 dbgs() << "'\n");
1721 auto &CSToUse = getCS(Signed: R.IsSigned);
1722 if (R.Coefficients.empty())
1723 return;
1724
1725 bool Added = CSToUse.addVariableRowFill(R: R.Coefficients);
1726 if (!Added)
1727 return;
1728
1729 // If R has been added to the system, add the new variables and queue it for
1730 // removal once it goes out-of-scope.
1731 SmallVector<Value *, 2> ValuesToRelease;
1732 auto &Value2Index = getValue2Index(Signed: R.IsSigned);
1733 for (Value *V : NewVariables) {
1734 Value2Index.try_emplace(Key: V, Args: Value2Index.size() + 1);
1735 ValuesToRelease.push_back(Elt: V);
1736 }
1737
1738 LLVM_DEBUG({
1739 dbgs() << " constraint: ";
1740 dumpConstraint(R.Coefficients, getValue2Index(R.IsSigned));
1741 dbgs() << "\n";
1742 });
1743
1744 DFSInStack.emplace_back(Args&: NumIn, Args&: NumOut, Args&: R.IsSigned,
1745 Args: std::move(ValuesToRelease));
1746
1747 if (!R.IsSigned) {
1748 for (Value *V : NewVariables) {
1749 ConstraintTy VarPos(SmallVector<int64_t, 8>(Value2Index.size() + 1, 0),
1750 false, false, false);
1751 VarPos.Coefficients[Value2Index[V]] = -1;
1752 CSToUse.addVariableRow(R: VarPos.Coefficients);
1753 DFSInStack.emplace_back(Args&: NumIn, Args&: NumOut, Args&: R.IsSigned,
1754 Args: SmallVector<Value *, 2>());
1755 }
1756 }
1757
1758 if (R.isEq()) {
1759 // Also add the inverted constraint for equality constraints.
1760 for (auto &Coeff : R.Coefficients)
1761 Coeff *= -1;
1762 CSToUse.addVariableRowFill(R: R.Coefficients);
1763
1764 DFSInStack.emplace_back(Args&: NumIn, Args&: NumOut, Args&: R.IsSigned,
1765 Args: SmallVector<Value *, 2>());
1766 }
1767}
1768
1769static bool replaceSubOverflowUses(IntrinsicInst *II, Value *A, Value *B,
1770 SmallVectorImpl<Instruction *> &ToRemove) {
1771 bool Changed = false;
1772 IRBuilder<> Builder(II->getParent(), II->getIterator());
1773 Value *Sub = nullptr;
1774 for (User *U : make_early_inc_range(Range: II->users())) {
1775 if (match(V: U, P: m_ExtractValue<0>(V: m_Value()))) {
1776 if (!Sub)
1777 Sub = Builder.CreateSub(LHS: A, RHS: B);
1778 U->replaceAllUsesWith(V: Sub);
1779 Changed = true;
1780 } else if (match(V: U, P: m_ExtractValue<1>(V: m_Value()))) {
1781 U->replaceAllUsesWith(V: Builder.getFalse());
1782 Changed = true;
1783 } else
1784 continue;
1785
1786 if (U->use_empty()) {
1787 auto *I = cast<Instruction>(Val: U);
1788 ToRemove.push_back(Elt: I);
1789 I->setOperand(i: 0, Val: PoisonValue::get(T: II->getType()));
1790 Changed = true;
1791 }
1792 }
1793
1794 if (II->use_empty()) {
1795 II->eraseFromParent();
1796 Changed = true;
1797 }
1798 return Changed;
1799}
1800
1801static bool
1802tryToSimplifyOverflowMath(IntrinsicInst *II, ConstraintInfo &Info,
1803 SmallVectorImpl<Instruction *> &ToRemove) {
1804 auto DoesConditionHold = [](CmpInst::Predicate Pred, Value *A, Value *B,
1805 ConstraintInfo &Info) {
1806 auto R = Info.getConstraintForSolving(Pred, Op0: A, Op1: B);
1807 if (R.size() < 2 || !R.isValid(Info))
1808 return false;
1809
1810 auto &CSToUse = Info.getCS(Signed: R.IsSigned);
1811 return CSToUse.isConditionImplied(R: R.Coefficients);
1812 };
1813
1814 bool Changed = false;
1815 if (II->getIntrinsicID() == Intrinsic::ssub_with_overflow) {
1816 // If A s>= B && B s>= 0, ssub.with.overflow(a, b) should not overflow and
1817 // can be simplified to a regular sub.
1818 Value *A = II->getArgOperand(i: 0);
1819 Value *B = II->getArgOperand(i: 1);
1820 if (!DoesConditionHold(CmpInst::ICMP_SGE, A, B, Info) ||
1821 !DoesConditionHold(CmpInst::ICMP_SGE, B,
1822 ConstantInt::get(Ty: A->getType(), V: 0), Info))
1823 return false;
1824 Changed = replaceSubOverflowUses(II, A, B, ToRemove);
1825 }
1826 return Changed;
1827}
1828
1829static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
1830 ScalarEvolution &SE,
1831 OptimizationRemarkEmitter &ORE,
1832 TargetLibraryInfo &TLI) {
1833 bool Changed = false;
1834 DT.updateDFSNumbers();
1835 SmallVector<Value *> FunctionArgs(llvm::make_pointer_range(Range: F.args()));
1836 ConstraintInfo Info(F.getDataLayout(), FunctionArgs);
1837 State S(DT, LI, SE, TLI);
1838 std::unique_ptr<Module> ReproducerModule(
1839 DumpReproducers ? new Module(F.getName(), F.getContext()) : nullptr);
1840
1841 // First, collect conditions implied by branches and blocks with their
1842 // Dominator DFS in and out numbers.
1843 for (BasicBlock &BB : F) {
1844 if (!DT.getNode(BB: &BB))
1845 continue;
1846 S.addInfoFor(BB);
1847 }
1848
1849 // Next, sort worklist by dominance, so that dominating conditions to check
1850 // and facts come before conditions and facts dominated by them. If a
1851 // condition to check and a fact have the same numbers, conditional facts come
1852 // first. Assume facts and checks are ordered according to their relative
1853 // order in the containing basic block. Also make sure conditions with
1854 // constant operands come before conditions without constant operands. This
1855 // increases the effectiveness of the current signed <-> unsigned fact
1856 // transfer logic.
1857 stable_sort(Range&: S.WorkList, C: [](const FactOrCheck &A, const FactOrCheck &B) {
1858 auto HasNoConstOp = [](const FactOrCheck &B) {
1859 Value *V0 = B.isConditionFact() ? B.Cond.Op0 : B.Inst->getOperand(i: 0);
1860 Value *V1 = B.isConditionFact() ? B.Cond.Op1 : B.Inst->getOperand(i: 1);
1861 return !isa<ConstantInt>(Val: V0) && !isa<ConstantInt>(Val: V1);
1862 };
1863 // If both entries have the same In numbers, conditional facts come first.
1864 // Otherwise use the relative order in the basic block.
1865 if (A.NumIn == B.NumIn) {
1866 if (A.isConditionFact() && B.isConditionFact()) {
1867 bool NoConstOpA = HasNoConstOp(A);
1868 bool NoConstOpB = HasNoConstOp(B);
1869 return NoConstOpA < NoConstOpB;
1870 }
1871 if (A.isConditionFact())
1872 return true;
1873 if (B.isConditionFact())
1874 return false;
1875 auto *InstA = A.getContextInst();
1876 auto *InstB = B.getContextInst();
1877 return InstA->comesBefore(Other: InstB);
1878 }
1879 return A.NumIn < B.NumIn;
1880 });
1881
1882 SmallVector<Instruction *> ToRemove;
1883
1884 // Finally, process ordered worklist and eliminate implied conditions.
1885 SmallVector<StackEntry, 16> DFSInStack;
1886 SmallVector<ReproducerEntry> ReproducerCondStack;
1887 for (FactOrCheck &CB : S.WorkList) {
1888 // First, pop entries from the stack that are out-of-scope for CB. Remove
1889 // the corresponding entry from the constraint system.
1890 while (!DFSInStack.empty()) {
1891 auto &E = DFSInStack.back();
1892 LLVM_DEBUG(dbgs() << "Top of stack : " << E.NumIn << " " << E.NumOut
1893 << "\n");
1894 LLVM_DEBUG(dbgs() << "CB: " << CB.NumIn << " " << CB.NumOut << "\n");
1895 assert(E.NumIn <= CB.NumIn);
1896 if (CB.NumOut <= E.NumOut)
1897 break;
1898 LLVM_DEBUG({
1899 dbgs() << "Removing ";
1900 dumpConstraint(Info.getCS(E.IsSigned).getLastConstraint(),
1901 Info.getValue2Index(E.IsSigned));
1902 dbgs() << "\n";
1903 });
1904 removeEntryFromStack(E, Info, ReproducerModule: ReproducerModule.get(), ReproducerCondStack,
1905 DFSInStack);
1906 }
1907
1908 // For a block, check if any CmpInsts become known based on the current set
1909 // of constraints.
1910 if (CB.isCheck()) {
1911 Instruction *Inst = CB.getInstructionToSimplify();
1912 if (!Inst)
1913 continue;
1914 LLVM_DEBUG(dbgs() << "Processing condition to simplify: " << *Inst
1915 << "\n");
1916 if (auto *II = dyn_cast<WithOverflowInst>(Val: Inst)) {
1917 Changed |= tryToSimplifyOverflowMath(II, Info, ToRemove);
1918 } else if (auto *Cmp = dyn_cast<ICmpInst>(Val: Inst)) {
1919 bool Simplified = checkAndReplaceCondition(
1920 Cmp, Info, NumIn: CB.NumIn, NumOut: CB.NumOut, ContextInst: CB.getContextInst(),
1921 ReproducerModule: ReproducerModule.get(), ReproducerCondStack, DT&: S.DT, ToRemove);
1922 if (!Simplified &&
1923 match(V: CB.getContextInst(), P: m_LogicalOp(L: m_Value(), R: m_Value()))) {
1924 Simplified = checkOrAndOpImpliedByOther(
1925 CB, Info, ReproducerModule: ReproducerModule.get(), ReproducerCondStack, DFSInStack,
1926 ToRemove);
1927 }
1928 Changed |= Simplified;
1929 } else if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Val: Inst)) {
1930 Changed |= checkAndReplaceMinMax(MinMax, Info, ToRemove);
1931 } else if (auto *CmpIntr = dyn_cast<CmpIntrinsic>(Val: Inst)) {
1932 Changed |= checkAndReplaceCmp(I: CmpIntr, Info, ToRemove);
1933 }
1934 continue;
1935 }
1936
1937 auto AddFact = [&](CmpPredicate Pred, Value *A, Value *B) {
1938 LLVM_DEBUG(dbgs() << "Processing fact to add to the system: ";
1939 dumpUnpackedICmp(dbgs(), Pred, A, B); dbgs() << "\n");
1940 if (Info.getCS(Signed: CmpInst::isSigned(Pred)).size() > MaxRows) {
1941 LLVM_DEBUG(
1942 dbgs()
1943 << "Skip adding constraint because system has too many rows.\n");
1944 return;
1945 }
1946
1947 Info.addFact(Pred, A, B, NumIn: CB.NumIn, NumOut: CB.NumOut, DFSInStack);
1948 if (ReproducerModule && DFSInStack.size() > ReproducerCondStack.size())
1949 ReproducerCondStack.emplace_back(Args&: Pred, Args&: A, Args&: B);
1950
1951 if (ICmpInst::isRelational(P: Pred)) {
1952 // If samesign is present on the ICmp, simply flip the sign of the
1953 // predicate, transferring the information from the signed system to the
1954 // unsigned system, and viceversa.
1955 if (Pred.hasSameSign())
1956 Info.addFact(Pred: ICmpInst::getFlippedSignednessPredicate(Pred), A, B,
1957 NumIn: CB.NumIn, NumOut: CB.NumOut, DFSInStack);
1958 else
1959 Info.transferToOtherSystem(Pred, A, B, NumIn: CB.NumIn, NumOut: CB.NumOut,
1960 DFSInStack);
1961 }
1962
1963 if (ReproducerModule && DFSInStack.size() > ReproducerCondStack.size()) {
1964 // Add dummy entries to ReproducerCondStack to keep it in sync with
1965 // DFSInStack.
1966 for (unsigned I = 0,
1967 E = (DFSInStack.size() - ReproducerCondStack.size());
1968 I < E; ++I) {
1969 ReproducerCondStack.emplace_back(Args: ICmpInst::BAD_ICMP_PREDICATE,
1970 Args: nullptr, Args: nullptr);
1971 }
1972 }
1973 };
1974
1975 CmpPredicate Pred;
1976 if (!CB.isConditionFact()) {
1977 Value *X;
1978 if (match(V: CB.Inst, P: m_Intrinsic<Intrinsic::abs>(Op0: m_Value(V&: X)))) {
1979 // If is_int_min_poison is true then we may assume llvm.abs >= 0.
1980 if (cast<ConstantInt>(Val: CB.Inst->getOperand(i: 1))->isOne())
1981 AddFact(CmpInst::ICMP_SGE, CB.Inst,
1982 ConstantInt::get(Ty: CB.Inst->getType(), V: 0));
1983 AddFact(CmpInst::ICMP_SGE, CB.Inst, X);
1984 continue;
1985 }
1986
1987 if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Val: CB.Inst)) {
1988 Pred = ICmpInst::getNonStrictPredicate(pred: MinMax->getPredicate());
1989 AddFact(Pred, MinMax, MinMax->getLHS());
1990 AddFact(Pred, MinMax, MinMax->getRHS());
1991 continue;
1992 }
1993 if (auto *USatI = dyn_cast<SaturatingInst>(Val: CB.Inst)) {
1994 switch (USatI->getIntrinsicID()) {
1995 default:
1996 llvm_unreachable("Unexpected intrinsic.");
1997 case Intrinsic::uadd_sat:
1998 AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getLHS());
1999 AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getRHS());
2000 break;
2001 case Intrinsic::usub_sat:
2002 AddFact(ICmpInst::ICMP_ULE, USatI, USatI->getLHS());
2003 break;
2004 }
2005 continue;
2006 }
2007
2008 if (auto *BO = dyn_cast<BinaryOperator>(Val: CB.Inst)) {
2009 if (BO->getOpcode() == Instruction::URem) {
2010 // urem x, n: result < n (remainder is always less than divisor)
2011 AddFact(CmpInst::ICMP_ULT, BO, BO->getOperand(i_nocapture: 1));
2012 // urem x, n: result <= x (remainder is at most the dividend)
2013 AddFact(CmpInst::ICMP_ULE, BO, BO->getOperand(i_nocapture: 0));
2014 continue;
2015 }
2016 if (BO->getOpcode() == Instruction::UDiv) {
2017 // udiv x, n: result <= x (quotient is at most the dividend)
2018 AddFact(CmpInst::ICMP_ULE, BO, BO->getOperand(i_nocapture: 0));
2019 continue;
2020 }
2021 }
2022
2023 auto &DL = F.getDataLayout();
2024 auto AddFactsAboutIndices = [&](Value *Ptr, Type *AccessType) {
2025 CmpPredicate Pred;
2026 Value *A, *B;
2027 if (getConstraintFromMemoryAccess(
2028 GEP&: *cast<GetElementPtrInst>(Val: Ptr),
2029 AccessSize: DL.getTypeStoreSize(Ty: AccessType).getFixedValue(), Pred, A, B, DL,
2030 TLI))
2031 AddFact(Pred, A, B);
2032 };
2033
2034 if (auto *LI = dyn_cast<LoadInst>(Val: CB.Inst)) {
2035 AddFactsAboutIndices(LI->getPointerOperand(), LI->getAccessType());
2036 continue;
2037 }
2038 if (auto *SI = dyn_cast<StoreInst>(Val: CB.Inst)) {
2039 AddFactsAboutIndices(SI->getPointerOperand(), SI->getAccessType());
2040 continue;
2041 }
2042 }
2043
2044 Value *A = nullptr, *B = nullptr;
2045 if (CB.isConditionFact()) {
2046 Pred = CB.Cond.Pred;
2047 A = CB.Cond.Op0;
2048 B = CB.Cond.Op1;
2049 if (CB.DoesHold.Pred != CmpInst::BAD_ICMP_PREDICATE &&
2050 !Info.doesHold(Pred: CB.DoesHold.Pred, A: CB.DoesHold.Op0, B: CB.DoesHold.Op1)) {
2051 LLVM_DEBUG({
2052 dbgs() << "Not adding fact ";
2053 dumpUnpackedICmp(dbgs(), Pred, A, B);
2054 dbgs() << " because precondition ";
2055 dumpUnpackedICmp(dbgs(), CB.DoesHold.Pred, CB.DoesHold.Op0,
2056 CB.DoesHold.Op1);
2057 dbgs() << " does not hold.\n";
2058 });
2059 continue;
2060 }
2061 } else {
2062 bool Matched = match(V: CB.Inst, P: m_Intrinsic<Intrinsic::assume>(Op0: m_ICmpLike(
2063 Pred, L: m_Value(V&: A), R: m_Value(V&: B))));
2064 (void)Matched;
2065 assert(Matched &&
2066 "Must have an assume intrinsic with a icmp like operand");
2067 }
2068 AddFact(Pred, A, B);
2069 }
2070
2071 if (ReproducerModule && !ReproducerModule->functions().empty()) {
2072 std::string S;
2073 raw_string_ostream StringS(S);
2074 ReproducerModule->print(OS&: StringS, AAW: nullptr);
2075 OptimizationRemark Rem(DEBUG_TYPE, "Reproducer", &F);
2076 Rem << ore::NV("module") << S;
2077 ORE.emit(OptDiag&: Rem);
2078 }
2079
2080#ifndef NDEBUG
2081 unsigned SignedEntries =
2082 count_if(DFSInStack, [](const StackEntry &E) { return E.IsSigned; });
2083 assert(Info.getCS(false).size() - FunctionArgs.size() ==
2084 DFSInStack.size() - SignedEntries &&
2085 "updates to CS and DFSInStack are out of sync");
2086 assert(Info.getCS(true).size() == SignedEntries &&
2087 "updates to CS and DFSInStack are out of sync");
2088#endif
2089
2090 for (Instruction *I : ToRemove)
2091 I->eraseFromParent();
2092 return Changed;
2093}
2094
2095PreservedAnalyses ConstraintEliminationPass::run(Function &F,
2096 FunctionAnalysisManager &AM) {
2097 auto &DT = AM.getResult<DominatorTreeAnalysis>(IR&: F);
2098 auto &LI = AM.getResult<LoopAnalysis>(IR&: F);
2099 auto &SE = AM.getResult<ScalarEvolutionAnalysis>(IR&: F);
2100 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(IR&: F);
2101 auto &TLI = AM.getResult<TargetLibraryAnalysis>(IR&: F);
2102 if (!eliminateConstraints(F, DT, LI, SE, ORE, TLI))
2103 return PreservedAnalyses::all();
2104
2105 PreservedAnalyses PA;
2106 PA.preserve<DominatorTreeAnalysis>();
2107 PA.preserve<LoopAnalysis>();
2108 PA.preserve<ScalarEvolutionAnalysis>();
2109 PA.preserveSet<CFGAnalyses>();
2110 return PA;
2111}
2112