|
46 | 46 | #include <tuple>
|
47 | 47 | #include <utility>
|
48 | 48 |
|
| 49 | +namespace llvm { |
| 50 | +class AssumptionCache; |
| 51 | +class DataLayout; |
| 52 | +class DominatorTree; |
| 53 | +class LLVMContext; |
| 54 | +} // namespace llvm |
| 55 | + |
49 | 56 | using namespace llvm;
|
50 | 57 |
|
51 | 58 | #define DEBUG_TYPE "instcombine"
|
@@ -87,13 +94,14 @@ static cl::opt<unsigned>
|
87 | 94 | cl::desc("What is the maximal lookup depth when trying to "
|
88 | 95 | "check for viability of negation sinking."));
|
89 | 96 |
|
90 |
| -Negator::Negator(LLVMContext &C, const DataLayout &DL, bool IsTrulyNegation_) |
91 |
| - : Builder(C, TargetFolder(DL), |
| 97 | +Negator::Negator(LLVMContext &C, const DataLayout &DL_, AssumptionCache &AC_, |
| 98 | + const DominatorTree &DT_, bool IsTrulyNegation_) |
| 99 | + : Builder(C, TargetFolder(DL_), |
92 | 100 | IRBuilderCallbackInserter([&](Instruction *I) {
|
93 | 101 | ++NegatorNumInstructionsCreatedTotal;
|
94 | 102 | NewInstructions.push_back(I);
|
95 | 103 | })),
|
96 |
| - IsTrulyNegation(IsTrulyNegation_) {} |
| 104 | + DL(DL_), AC(AC_), DT(DT_), IsTrulyNegation(IsTrulyNegation_) {} |
97 | 105 |
|
98 | 106 | #if LLVM_ENABLE_STATS
|
99 | 107 | Negator::~Negator() {
|
@@ -301,6 +309,16 @@ LLVM_NODISCARD Value *Negator::visit(Value *V, unsigned Depth) {
|
301 | 309 | return nullptr;
|
302 | 310 | return Builder.CreateShl(NegOp0, I->getOperand(1), I->getName() + ".neg");
|
303 | 311 | }
|
| 312 | + case Instruction::Or: |
| 313 | + if (!haveNoCommonBitsSet(I->getOperand(0), I->getOperand(1), DL, &AC, I, |
| 314 | + &DT)) |
| 315 | + return nullptr; // Don't know how to handle `or` in general. |
| 316 | + // `or`/`add` are interchangeable when operands have no common bits set. |
| 317 | + // `inc` is always negatible. |
| 318 | + if (match(I->getOperand(1), m_One())) |
| 319 | + return Builder.CreateNot(I->getOperand(0), I->getName() + ".neg"); |
| 320 | + // Else, just defer to Instruction::Add handling. |
| 321 | + LLVM_FALLTHROUGH; |
304 | 322 | case Instruction::Add: {
|
305 | 323 | // `add` is negatible if both of its operands are negatible.
|
306 | 324 | Value *NegOp0 = visit(I->getOperand(0), Depth + 1);
|
@@ -364,7 +382,8 @@ LLVM_NODISCARD Value *Negator::Negate(bool LHSIsZero, Value *Root,
|
364 | 382 | if (!NegatorEnabled || !DebugCounter::shouldExecute(NegatorCounter))
|
365 | 383 | return nullptr;
|
366 | 384 |
|
367 |
| - Negator N(Root->getContext(), IC.getDataLayout(), LHSIsZero); |
| 385 | + Negator N(Root->getContext(), IC.getDataLayout(), IC.getAssumptionCache(), |
| 386 | + IC.getDominatorTree(), LHSIsZero); |
368 | 387 | Optional<Result> Res = N.run(Root);
|
369 | 388 | if (!Res) { // Negation failed.
|
370 | 389 | LLVM_DEBUG(dbgs() << "Negator: failed to sink negation into " << *Root
|
|
0 commit comments