1 //===- InstCombineNegator.cpp -----------------------------------*- C++ -*-===//
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 // This file implements sinking of negation into expression trees,
10 // as long as that can be done without increasing instruction count.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "InstCombineInternal.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/Twine.h"
25 #include "llvm/ADT/iterator_range.h"
26 #include "llvm/Analysis/TargetFolder.h"
27 #include "llvm/Analysis/ValueTracking.h"
28 #include "llvm/IR/Constant.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/DebugLoc.h"
31 #include "llvm/IR/IRBuilder.h"
32 #include "llvm/IR/Instruction.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/PatternMatch.h"
35 #include "llvm/IR/Type.h"
36 #include "llvm/IR/Use.h"
37 #include "llvm/IR/User.h"
38 #include "llvm/IR/Value.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Compiler.h"
42 #include "llvm/Support/DebugCounter.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include "llvm/Transforms/InstCombine/InstCombiner.h"
46 #include <functional>
47 #include <tuple>
48 #include <type_traits>
49 #include <utility>
50 
51 namespace llvm {
52 class AssumptionCache;
53 class DataLayout;
54 class DominatorTree;
55 class LLVMContext;
56 } // namespace llvm
57 
58 using namespace llvm;
59 
60 #define DEBUG_TYPE "instcombine"
61 
62 STATISTIC(NegatorTotalNegationsAttempted,
63           "Negator: Number of negations attempted to be sinked");
64 STATISTIC(NegatorNumTreesNegated,
65           "Negator: Number of negations successfully sinked");
66 STATISTIC(NegatorMaxDepthVisited, "Negator: Maximal traversal depth ever "
67                                   "reached while attempting to sink negation");
68 STATISTIC(NegatorTimesDepthLimitReached,
69           "Negator: How many times did the traversal depth limit was reached "
70           "during sinking");
71 STATISTIC(
72     NegatorNumValuesVisited,
73     "Negator: Total number of values visited during attempts to sink negation");
74 STATISTIC(NegatorNumNegationsFoundInCache,
75           "Negator: How many negations did we retrieve/reuse from cache");
76 STATISTIC(NegatorMaxTotalValuesVisited,
77           "Negator: Maximal number of values ever visited while attempting to "
78           "sink negation");
79 STATISTIC(NegatorNumInstructionsCreatedTotal,
80           "Negator: Number of new negated instructions created, total");
81 STATISTIC(NegatorMaxInstructionsCreated,
82           "Negator: Maximal number of new instructions created during negation "
83           "attempt");
84 STATISTIC(NegatorNumInstructionsNegatedSuccess,
85           "Negator: Number of new negated instructions created in successful "
86           "negation sinking attempts");
87 
88 DEBUG_COUNTER(NegatorCounter, "instcombine-negator",
89               "Controls Negator transformations in InstCombine pass");
90 
91 static cl::opt<bool>
92     NegatorEnabled("instcombine-negator-enabled", cl::init(true),
93                    cl::desc("Should we attempt to sink negations?"));
94 
95 static cl::opt<unsigned>
96     NegatorMaxDepth("instcombine-negator-max-depth",
97                     cl::init(NegatorDefaultMaxDepth),
98                     cl::desc("What is the maximal lookup depth when trying to "
99                              "check for viability of negation sinking."));
100 
101 Negator::Negator(LLVMContext &C, const DataLayout &DL_, AssumptionCache &AC_,
102                  const DominatorTree &DT_, bool IsTrulyNegation_)
103     : Builder(C, TargetFolder(DL_),
104               IRBuilderCallbackInserter([&](Instruction *I) {
105                 ++NegatorNumInstructionsCreatedTotal;
106                 NewInstructions.push_back(I);
107               })),
108       DL(DL_), AC(AC_), DT(DT_), IsTrulyNegation(IsTrulyNegation_) {}
109 
110 #if LLVM_ENABLE_STATS
111 Negator::~Negator() {
112   NegatorMaxTotalValuesVisited.updateMax(NumValuesVisitedInThisNegator);
113 }
114 #endif
115 
116 // FIXME: can this be reworked into a worklist-based algorithm while preserving
117 // the depth-first, early bailout traversal?
118 LLVM_NODISCARD Value *Negator::visitImpl(Value *V, unsigned Depth) {
119   // -(undef) -> undef.
120   if (match(V, m_Undef()))
121     return V;
122 
123   // In i1, negation can simply be ignored.
124   if (V->getType()->isIntOrIntVectorTy(1))
125     return V;
126 
127   Value *X;
128 
129   // -(-(X)) -> X.
130   if (match(V, m_Neg(m_Value(X))))
131     return X;
132 
133   // Integral constants can be freely negated.
134   if (match(V, m_AnyIntegralConstant()))
135     return ConstantExpr::getNeg(cast<Constant>(V), /*HasNUW=*/false,
136                                 /*HasNSW=*/false);
137 
138   // If we have a non-instruction, then give up.
139   if (!isa<Instruction>(V))
140     return nullptr;
141 
142   // If we have started with a true negation (i.e. `sub 0, %y`), then if we've
143   // got instruction that does not require recursive reasoning, we can still
144   // negate it even if it has other uses, without increasing instruction count.
145   if (!V->hasOneUse() && !IsTrulyNegation)
146     return nullptr;
147 
148   auto *I = cast<Instruction>(V);
149   unsigned BitWidth = I->getType()->getScalarSizeInBits();
150 
151   // We must preserve the insertion point and debug info that is set in the
152   // builder at the time this function is called.
153   InstCombiner::BuilderTy::InsertPointGuard Guard(Builder);
154   // And since we are trying to negate instruction I, that tells us about the
155   // insertion point and the debug info that we need to keep.
156   Builder.SetInsertPoint(I);
157 
158   // In some cases we can give the answer without further recursion.
159   switch (I->getOpcode()) {
160   case Instruction::Add:
161     // `inc` is always negatible.
162     if (match(I->getOperand(1), m_One()))
163       return Builder.CreateNot(I->getOperand(0), I->getName() + ".neg");
164     break;
165   case Instruction::Xor:
166     // `not` is always negatible.
167     if (match(I, m_Not(m_Value(X))))
168       return Builder.CreateAdd(X, ConstantInt::get(X->getType(), 1),
169                                I->getName() + ".neg");
170     break;
171   case Instruction::AShr:
172   case Instruction::LShr: {
173     // Right-shift sign bit smear is negatible.
174     const APInt *Op1Val;
175     if (match(I->getOperand(1), m_APInt(Op1Val)) && *Op1Val == BitWidth - 1) {
176       Value *BO = I->getOpcode() == Instruction::AShr
177                       ? Builder.CreateLShr(I->getOperand(0), I->getOperand(1))
178                       : Builder.CreateAShr(I->getOperand(0), I->getOperand(1));
179       if (auto *NewInstr = dyn_cast<Instruction>(BO)) {
180         NewInstr->copyIRFlags(I);
181         NewInstr->setName(I->getName() + ".neg");
182       }
183       return BO;
184     }
185     break;
186   }
187   case Instruction::SExt:
188   case Instruction::ZExt:
189     // `*ext` of i1 is always negatible
190     if (I->getOperand(0)->getType()->isIntOrIntVectorTy(1))
191       return I->getOpcode() == Instruction::SExt
192                  ? Builder.CreateZExt(I->getOperand(0), I->getType(),
193                                       I->getName() + ".neg")
194                  : Builder.CreateSExt(I->getOperand(0), I->getType(),
195                                       I->getName() + ".neg");
196     break;
197   default:
198     break; // Other instructions require recursive reasoning.
199   }
200 
201   // Some other cases, while still don't require recursion,
202   // are restricted to the one-use case.
203   if (!V->hasOneUse())
204     return nullptr;
205 
206   switch (I->getOpcode()) {
207   case Instruction::Sub:
208     // `sub` is always negatible.
209     // But if the old `sub` sticks around, even thought we don't increase
210     // instruction count, this is a likely regression since we increased
211     // live-range of *both* of the operands, which might lead to more spilling.
212     return Builder.CreateSub(I->getOperand(1), I->getOperand(0),
213                              I->getName() + ".neg");
214   case Instruction::SDiv:
215     // `sdiv` is negatible if divisor is not undef/INT_MIN/1.
216     // While this is normally not behind a use-check,
217     // let's consider division to be special since it's costly.
218     if (auto *Op1C = dyn_cast<Constant>(I->getOperand(1))) {
219       if (!Op1C->containsUndefElement() && Op1C->isNotMinSignedValue() &&
220           Op1C->isNotOneValue()) {
221         Value *BO =
222             Builder.CreateSDiv(I->getOperand(0), ConstantExpr::getNeg(Op1C),
223                                I->getName() + ".neg");
224         if (auto *NewInstr = dyn_cast<Instruction>(BO))
225           NewInstr->setIsExact(I->isExact());
226         return BO;
227       }
228     }
229     break;
230   }
231 
232   // Rest of the logic is recursive, so if it's time to give up then it's time.
233   if (Depth > NegatorMaxDepth) {
234     LLVM_DEBUG(dbgs() << "Negator: reached maximal allowed traversal depth in "
235                       << *V << ". Giving up.\n");
236     ++NegatorTimesDepthLimitReached;
237     return nullptr;
238   }
239 
240   switch (I->getOpcode()) {
241   case Instruction::PHI: {
242     // `phi` is negatible if all the incoming values are negatible.
243     auto *PHI = cast<PHINode>(I);
244     SmallVector<Value *, 4> NegatedIncomingValues(PHI->getNumOperands());
245     for (auto I : zip(PHI->incoming_values(), NegatedIncomingValues)) {
246       if (!(std::get<1>(I) =
247                 negate(std::get<0>(I), Depth + 1))) // Early return.
248         return nullptr;
249     }
250     // All incoming values are indeed negatible. Create negated PHI node.
251     PHINode *NegatedPHI = Builder.CreatePHI(
252         PHI->getType(), PHI->getNumOperands(), PHI->getName() + ".neg");
253     for (auto I : zip(NegatedIncomingValues, PHI->blocks()))
254       NegatedPHI->addIncoming(std::get<0>(I), std::get<1>(I));
255     return NegatedPHI;
256   }
257   case Instruction::Select: {
258     {
259       // `abs`/`nabs` is always negatible.
260       Value *LHS, *RHS;
261       SelectPatternFlavor SPF =
262           matchSelectPattern(I, LHS, RHS, /*CastOp=*/nullptr, Depth).Flavor;
263       if (SPF == SPF_ABS || SPF == SPF_NABS) {
264         auto *NewSelect = cast<SelectInst>(I->clone());
265         // Just swap the operands of the select.
266         NewSelect->swapValues();
267         // Don't swap prof metadata, we didn't change the branch behavior.
268         NewSelect->setName(I->getName() + ".neg");
269         Builder.Insert(NewSelect);
270         return NewSelect;
271       }
272     }
273     // `select` is negatible if both hands of `select` are negatible.
274     Value *NegOp1 = negate(I->getOperand(1), Depth + 1);
275     if (!NegOp1) // Early return.
276       return nullptr;
277     Value *NegOp2 = negate(I->getOperand(2), Depth + 1);
278     if (!NegOp2)
279       return nullptr;
280     // Do preserve the metadata!
281     return Builder.CreateSelect(I->getOperand(0), NegOp1, NegOp2,
282                                 I->getName() + ".neg", /*MDFrom=*/I);
283   }
284   case Instruction::ShuffleVector: {
285     // `shufflevector` is negatible if both operands are negatible.
286     auto *Shuf = cast<ShuffleVectorInst>(I);
287     Value *NegOp0 = negate(I->getOperand(0), Depth + 1);
288     if (!NegOp0) // Early return.
289       return nullptr;
290     Value *NegOp1 = negate(I->getOperand(1), Depth + 1);
291     if (!NegOp1)
292       return nullptr;
293     return Builder.CreateShuffleVector(NegOp0, NegOp1, Shuf->getShuffleMask(),
294                                        I->getName() + ".neg");
295   }
296   case Instruction::ExtractElement: {
297     // `extractelement` is negatible if source operand is negatible.
298     auto *EEI = cast<ExtractElementInst>(I);
299     Value *NegVector = negate(EEI->getVectorOperand(), Depth + 1);
300     if (!NegVector) // Early return.
301       return nullptr;
302     return Builder.CreateExtractElement(NegVector, EEI->getIndexOperand(),
303                                         I->getName() + ".neg");
304   }
305   case Instruction::InsertElement: {
306     // `insertelement` is negatible if both the source vector and
307     // element-to-be-inserted are negatible.
308     auto *IEI = cast<InsertElementInst>(I);
309     Value *NegVector = negate(IEI->getOperand(0), Depth + 1);
310     if (!NegVector) // Early return.
311       return nullptr;
312     Value *NegNewElt = negate(IEI->getOperand(1), Depth + 1);
313     if (!NegNewElt) // Early return.
314       return nullptr;
315     return Builder.CreateInsertElement(NegVector, NegNewElt, IEI->getOperand(2),
316                                        I->getName() + ".neg");
317   }
318   case Instruction::Trunc: {
319     // `trunc` is negatible if its operand is negatible.
320     Value *NegOp = negate(I->getOperand(0), Depth + 1);
321     if (!NegOp) // Early return.
322       return nullptr;
323     return Builder.CreateTrunc(NegOp, I->getType(), I->getName() + ".neg");
324   }
325   case Instruction::Shl: {
326     // `shl` is negatible if the first operand is negatible.
327     Value *NegOp0 = negate(I->getOperand(0), Depth + 1);
328     if (!NegOp0) // Early return.
329       return nullptr;
330     return Builder.CreateShl(NegOp0, I->getOperand(1), I->getName() + ".neg");
331   }
332   case Instruction::Or:
333     if (!haveNoCommonBitsSet(I->getOperand(0), I->getOperand(1), DL, &AC, I,
334                              &DT))
335       return nullptr; // Don't know how to handle `or` in general.
336     // `or`/`add` are interchangeable when operands have no common bits set.
337     // `inc` is always negatible.
338     if (match(I->getOperand(1), m_One()))
339       return Builder.CreateNot(I->getOperand(0), I->getName() + ".neg");
340     // Else, just defer to Instruction::Add handling.
341     LLVM_FALLTHROUGH;
342   case Instruction::Add: {
343     // `add` is negatible if both of its operands are negatible.
344     Value *NegOp0 = negate(I->getOperand(0), Depth + 1);
345     if (!NegOp0) // Early return.
346       return nullptr;
347     Value *NegOp1 = negate(I->getOperand(1), Depth + 1);
348     if (!NegOp1)
349       return nullptr;
350     return Builder.CreateAdd(NegOp0, NegOp1, I->getName() + ".neg");
351   }
352   case Instruction::Xor:
353     // `xor` is negatible if one of its operands is invertible.
354     // FIXME: InstCombineInverter? But how to connect Inverter and Negator?
355     if (auto *C = dyn_cast<Constant>(I->getOperand(1))) {
356       Value *Xor = Builder.CreateXor(I->getOperand(0), ConstantExpr::getNot(C));
357       return Builder.CreateAdd(Xor, ConstantInt::get(Xor->getType(), 1),
358                                I->getName() + ".neg");
359     }
360     return nullptr;
361   case Instruction::Mul: {
362     // `mul` is negatible if one of its operands is negatible.
363     Value *NegatedOp, *OtherOp;
364     // First try the second operand, in case it's a constant it will be best to
365     // just invert it instead of sinking the `neg` deeper.
366     if (Value *NegOp1 = negate(I->getOperand(1), Depth + 1)) {
367       NegatedOp = NegOp1;
368       OtherOp = I->getOperand(0);
369     } else if (Value *NegOp0 = negate(I->getOperand(0), Depth + 1)) {
370       NegatedOp = NegOp0;
371       OtherOp = I->getOperand(1);
372     } else
373       // Can't negate either of them.
374       return nullptr;
375     return Builder.CreateMul(NegatedOp, OtherOp, I->getName() + ".neg");
376   }
377   default:
378     return nullptr; // Don't know, likely not negatible for free.
379   }
380 
381   llvm_unreachable("Can't get here. We always return from switch.");
382 }
383 
384 LLVM_NODISCARD Value *Negator::negate(Value *V, unsigned Depth) {
385   NegatorMaxDepthVisited.updateMax(Depth);
386   ++NegatorNumValuesVisited;
387 
388 #if LLVM_ENABLE_STATS
389   ++NumValuesVisitedInThisNegator;
390 #endif
391 
392 #ifndef NDEBUG
393   // We can't ever have a Value with such an address.
394   Value *Placeholder = reinterpret_cast<Value *>(static_cast<uintptr_t>(-1));
395 #endif
396 
397   // Did we already try to negate this value?
398   auto NegationsCacheIterator = NegationsCache.find(V);
399   if (NegationsCacheIterator != NegationsCache.end()) {
400     ++NegatorNumNegationsFoundInCache;
401     Value *NegatedV = NegationsCacheIterator->second;
402     assert(NegatedV != Placeholder && "Encountered a cycle during negation.");
403     return NegatedV;
404   }
405 
406 #ifndef NDEBUG
407   // We did not find a cached result for negation of V. While there,
408   // let's temporairly cache a placeholder value, with the idea that if later
409   // during negation we fetch it from cache, we'll know we're in a cycle.
410   NegationsCache[V] = Placeholder;
411 #endif
412 
413   // No luck. Try negating it for real.
414   Value *NegatedV = visitImpl(V, Depth);
415   // And cache the (real) result for the future.
416   NegationsCache[V] = NegatedV;
417 
418   return NegatedV;
419 }
420 
421 LLVM_NODISCARD Optional<Negator::Result> Negator::run(Value *Root) {
422   Value *Negated = negate(Root, /*Depth=*/0);
423   if (!Negated) {
424     // We must cleanup newly-inserted instructions, to avoid any potential
425     // endless combine looping.
426     llvm::for_each(llvm::reverse(NewInstructions),
427                    [&](Instruction *I) { I->eraseFromParent(); });
428     return llvm::None;
429   }
430   return std::make_pair(ArrayRef<Instruction *>(NewInstructions), Negated);
431 }
432 
433 LLVM_NODISCARD Value *Negator::Negate(bool LHSIsZero, Value *Root,
434                                       InstCombinerImpl &IC) {
435   ++NegatorTotalNegationsAttempted;
436   LLVM_DEBUG(dbgs() << "Negator: attempting to sink negation into " << *Root
437                     << "\n");
438 
439   if (!NegatorEnabled || !DebugCounter::shouldExecute(NegatorCounter))
440     return nullptr;
441 
442   Negator N(Root->getContext(), IC.getDataLayout(), IC.getAssumptionCache(),
443             IC.getDominatorTree(), LHSIsZero);
444   Optional<Result> Res = N.run(Root);
445   if (!Res) { // Negation failed.
446     LLVM_DEBUG(dbgs() << "Negator: failed to sink negation into " << *Root
447                       << "\n");
448     return nullptr;
449   }
450 
451   LLVM_DEBUG(dbgs() << "Negator: successfully sunk negation into " << *Root
452                     << "\n         NEW: " << *Res->second << "\n");
453   ++NegatorNumTreesNegated;
454 
455   // We must temporarily unset the 'current' insertion point and DebugLoc of the
456   // InstCombine's IRBuilder so that it won't interfere with the ones we have
457   // already specified when producing negated instructions.
458   InstCombiner::BuilderTy::InsertPointGuard Guard(IC.Builder);
459   IC.Builder.ClearInsertionPoint();
460   IC.Builder.SetCurrentDebugLocation(DebugLoc());
461 
462   // And finally, we must add newly-created instructions into the InstCombine's
463   // worklist (in a proper order!) so it can attempt to combine them.
464   LLVM_DEBUG(dbgs() << "Negator: Propagating " << Res->first.size()
465                     << " instrs to InstCombine\n");
466   NegatorMaxInstructionsCreated.updateMax(Res->first.size());
467   NegatorNumInstructionsNegatedSuccess += Res->first.size();
468 
469   // They are in def-use order, so nothing fancy, just insert them in order.
470   llvm::for_each(Res->first,
471                  [&](Instruction *I) { IC.Builder.Insert(I, I->getName()); });
472 
473   // And return the new root.
474   return Res->second;
475 }
476