xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
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 <cassert>
47 #include <cstdint>
48 #include <functional>
49 #include <tuple>
50 #include <type_traits>
51 #include <utility>
52 
53 namespace llvm {
54 class AssumptionCache;
55 class DataLayout;
56 class DominatorTree;
57 class LLVMContext;
58 } // namespace llvm
59 
60 using namespace llvm;
61 
62 #define DEBUG_TYPE "instcombine"
63 
64 STATISTIC(NegatorTotalNegationsAttempted,
65           "Negator: Number of negations attempted to be sinked");
66 STATISTIC(NegatorNumTreesNegated,
67           "Negator: Number of negations successfully sinked");
68 STATISTIC(NegatorMaxDepthVisited, "Negator: Maximal traversal depth ever "
69                                   "reached while attempting to sink negation");
70 STATISTIC(NegatorTimesDepthLimitReached,
71           "Negator: How many times did the traversal depth limit was reached "
72           "during sinking");
73 STATISTIC(
74     NegatorNumValuesVisited,
75     "Negator: Total number of values visited during attempts to sink negation");
76 STATISTIC(NegatorNumNegationsFoundInCache,
77           "Negator: How many negations did we retrieve/reuse from cache");
78 STATISTIC(NegatorMaxTotalValuesVisited,
79           "Negator: Maximal number of values ever visited while attempting to "
80           "sink negation");
81 STATISTIC(NegatorNumInstructionsCreatedTotal,
82           "Negator: Number of new negated instructions created, total");
83 STATISTIC(NegatorMaxInstructionsCreated,
84           "Negator: Maximal number of new instructions created during negation "
85           "attempt");
86 STATISTIC(NegatorNumInstructionsNegatedSuccess,
87           "Negator: Number of new negated instructions created in successful "
88           "negation sinking attempts");
89 
90 DEBUG_COUNTER(NegatorCounter, "instcombine-negator",
91               "Controls Negator transformations in InstCombine pass");
92 
93 static cl::opt<bool>
94     NegatorEnabled("instcombine-negator-enabled", cl::init(true),
95                    cl::desc("Should we attempt to sink negations?"));
96 
97 static cl::opt<unsigned>
98     NegatorMaxDepth("instcombine-negator-max-depth",
99                     cl::init(NegatorDefaultMaxDepth),
100                     cl::desc("What is the maximal lookup depth when trying to "
101                              "check for viability of negation sinking."));
102 
103 Negator::Negator(LLVMContext &C, const DataLayout &DL_, AssumptionCache &AC_,
104                  const DominatorTree &DT_, bool IsTrulyNegation_)
105     : Builder(C, TargetFolder(DL_),
106               IRBuilderCallbackInserter([&](Instruction *I) {
107                 ++NegatorNumInstructionsCreatedTotal;
108                 NewInstructions.push_back(I);
109               })),
110       DL(DL_), AC(AC_), DT(DT_), IsTrulyNegation(IsTrulyNegation_) {}
111 
112 #if LLVM_ENABLE_STATS
113 Negator::~Negator() {
114   NegatorMaxTotalValuesVisited.updateMax(NumValuesVisitedInThisNegator);
115 }
116 #endif
117 
118 // Due to the InstCombine's worklist management, there are no guarantees that
119 // each instruction we'll encounter has been visited by InstCombine already.
120 // In particular, most importantly for us, that means we have to canonicalize
121 // constants to RHS ourselves, since that is helpful sometimes.
122 std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
123   assert(I->getNumOperands() == 2 && "Only for binops!");
124   std::array<Value *, 2> Ops{I->getOperand(0), I->getOperand(1)};
125   if (I->isCommutative() && InstCombiner::getComplexity(I->getOperand(0)) <
126                                 InstCombiner::getComplexity(I->getOperand(1)))
127     std::swap(Ops[0], Ops[1]);
128   return Ops;
129 }
130 
131 // FIXME: can this be reworked into a worklist-based algorithm while preserving
132 // the depth-first, early bailout traversal?
133 LLVM_NODISCARD Value *Negator::visitImpl(Value *V, unsigned Depth) {
134   // -(undef) -> undef.
135   if (match(V, m_Undef()))
136     return V;
137 
138   // In i1, negation can simply be ignored.
139   if (V->getType()->isIntOrIntVectorTy(1))
140     return V;
141 
142   Value *X;
143 
144   // -(-(X)) -> X.
145   if (match(V, m_Neg(m_Value(X))))
146     return X;
147 
148   // Integral constants can be freely negated.
149   if (match(V, m_AnyIntegralConstant()))
150     return ConstantExpr::getNeg(cast<Constant>(V), /*HasNUW=*/false,
151                                 /*HasNSW=*/false);
152 
153   // If we have a non-instruction, then give up.
154   if (!isa<Instruction>(V))
155     return nullptr;
156 
157   // If we have started with a true negation (i.e. `sub 0, %y`), then if we've
158   // got instruction that does not require recursive reasoning, we can still
159   // negate it even if it has other uses, without increasing instruction count.
160   if (!V->hasOneUse() && !IsTrulyNegation)
161     return nullptr;
162 
163   auto *I = cast<Instruction>(V);
164   unsigned BitWidth = I->getType()->getScalarSizeInBits();
165 
166   // We must preserve the insertion point and debug info that is set in the
167   // builder at the time this function is called.
168   InstCombiner::BuilderTy::InsertPointGuard Guard(Builder);
169   // And since we are trying to negate instruction I, that tells us about the
170   // insertion point and the debug info that we need to keep.
171   Builder.SetInsertPoint(I);
172 
173   // In some cases we can give the answer without further recursion.
174   switch (I->getOpcode()) {
175   case Instruction::Add: {
176     std::array<Value *, 2> Ops = getSortedOperandsOfBinOp(I);
177     // `inc` is always negatible.
178     if (match(Ops[1], m_One()))
179       return Builder.CreateNot(Ops[0], I->getName() + ".neg");
180     break;
181   }
182   case Instruction::Xor:
183     // `not` is always negatible.
184     if (match(I, m_Not(m_Value(X))))
185       return Builder.CreateAdd(X, ConstantInt::get(X->getType(), 1),
186                                I->getName() + ".neg");
187     break;
188   case Instruction::AShr:
189   case Instruction::LShr: {
190     // Right-shift sign bit smear is negatible.
191     const APInt *Op1Val;
192     if (match(I->getOperand(1), m_APInt(Op1Val)) && *Op1Val == BitWidth - 1) {
193       Value *BO = I->getOpcode() == Instruction::AShr
194                       ? Builder.CreateLShr(I->getOperand(0), I->getOperand(1))
195                       : Builder.CreateAShr(I->getOperand(0), I->getOperand(1));
196       if (auto *NewInstr = dyn_cast<Instruction>(BO)) {
197         NewInstr->copyIRFlags(I);
198         NewInstr->setName(I->getName() + ".neg");
199       }
200       return BO;
201     }
202     // While we could negate exact arithmetic shift:
203     //   ashr exact %x, C  -->   sdiv exact i8 %x, -1<<C
204     // iff C != 0 and C u< bitwidth(%x), we don't want to,
205     // because division is *THAT* much worse than a shift.
206     break;
207   }
208   case Instruction::SExt:
209   case Instruction::ZExt:
210     // `*ext` of i1 is always negatible
211     if (I->getOperand(0)->getType()->isIntOrIntVectorTy(1))
212       return I->getOpcode() == Instruction::SExt
213                  ? Builder.CreateZExt(I->getOperand(0), I->getType(),
214                                       I->getName() + ".neg")
215                  : Builder.CreateSExt(I->getOperand(0), I->getType(),
216                                       I->getName() + ".neg");
217     break;
218   case Instruction::Select: {
219     // If both arms of the select are constants, we don't need to recurse.
220     // Therefore, this transform is not limited by uses.
221     auto *Sel = cast<SelectInst>(I);
222     Constant *TrueC, *FalseC;
223     if (match(Sel->getTrueValue(), m_ImmConstant(TrueC)) &&
224         match(Sel->getFalseValue(), m_ImmConstant(FalseC))) {
225       Constant *NegTrueC = ConstantExpr::getNeg(TrueC);
226       Constant *NegFalseC = ConstantExpr::getNeg(FalseC);
227       return Builder.CreateSelect(Sel->getCondition(), NegTrueC, NegFalseC,
228                                   I->getName() + ".neg", /*MDFrom=*/I);
229     }
230     break;
231   }
232   default:
233     break; // Other instructions require recursive reasoning.
234   }
235 
236   if (I->getOpcode() == Instruction::Sub &&
237       (I->hasOneUse() || match(I->getOperand(0), m_ImmConstant()))) {
238     // `sub` is always negatible.
239     // However, only do this either if the old `sub` doesn't stick around, or
240     // it was subtracting from a constant. Otherwise, this isn't profitable.
241     return Builder.CreateSub(I->getOperand(1), I->getOperand(0),
242                              I->getName() + ".neg");
243   }
244 
245   // Some other cases, while still don't require recursion,
246   // are restricted to the one-use case.
247   if (!V->hasOneUse())
248     return nullptr;
249 
250   switch (I->getOpcode()) {
251   case Instruction::SDiv:
252     // `sdiv` is negatible if divisor is not undef/INT_MIN/1.
253     // While this is normally not behind a use-check,
254     // let's consider division to be special since it's costly.
255     if (auto *Op1C = dyn_cast<Constant>(I->getOperand(1))) {
256       if (!Op1C->containsUndefOrPoisonElement() &&
257           Op1C->isNotMinSignedValue() && Op1C->isNotOneValue()) {
258         Value *BO =
259             Builder.CreateSDiv(I->getOperand(0), ConstantExpr::getNeg(Op1C),
260                                I->getName() + ".neg");
261         if (auto *NewInstr = dyn_cast<Instruction>(BO))
262           NewInstr->setIsExact(I->isExact());
263         return BO;
264       }
265     }
266     break;
267   }
268 
269   // Rest of the logic is recursive, so if it's time to give up then it's time.
270   if (Depth > NegatorMaxDepth) {
271     LLVM_DEBUG(dbgs() << "Negator: reached maximal allowed traversal depth in "
272                       << *V << ". Giving up.\n");
273     ++NegatorTimesDepthLimitReached;
274     return nullptr;
275   }
276 
277   switch (I->getOpcode()) {
278   case Instruction::Freeze: {
279     // `freeze` is negatible if its operand is negatible.
280     Value *NegOp = negate(I->getOperand(0), Depth + 1);
281     if (!NegOp) // Early return.
282       return nullptr;
283     return Builder.CreateFreeze(NegOp, I->getName() + ".neg");
284   }
285   case Instruction::PHI: {
286     // `phi` is negatible if all the incoming values are negatible.
287     auto *PHI = cast<PHINode>(I);
288     SmallVector<Value *, 4> NegatedIncomingValues(PHI->getNumOperands());
289     for (auto I : zip(PHI->incoming_values(), NegatedIncomingValues)) {
290       if (!(std::get<1>(I) =
291                 negate(std::get<0>(I), Depth + 1))) // Early return.
292         return nullptr;
293     }
294     // All incoming values are indeed negatible. Create negated PHI node.
295     PHINode *NegatedPHI = Builder.CreatePHI(
296         PHI->getType(), PHI->getNumOperands(), PHI->getName() + ".neg");
297     for (auto I : zip(NegatedIncomingValues, PHI->blocks()))
298       NegatedPHI->addIncoming(std::get<0>(I), std::get<1>(I));
299     return NegatedPHI;
300   }
301   case Instruction::Select: {
302     if (isKnownNegation(I->getOperand(1), I->getOperand(2))) {
303       // Of one hand of select is known to be negation of another hand,
304       // just swap the hands around.
305       auto *NewSelect = cast<SelectInst>(I->clone());
306       // Just swap the operands of the select.
307       NewSelect->swapValues();
308       // Don't swap prof metadata, we didn't change the branch behavior.
309       NewSelect->setName(I->getName() + ".neg");
310       Builder.Insert(NewSelect);
311       return NewSelect;
312     }
313     // `select` is negatible if both hands of `select` are negatible.
314     Value *NegOp1 = negate(I->getOperand(1), Depth + 1);
315     if (!NegOp1) // Early return.
316       return nullptr;
317     Value *NegOp2 = negate(I->getOperand(2), Depth + 1);
318     if (!NegOp2)
319       return nullptr;
320     // Do preserve the metadata!
321     return Builder.CreateSelect(I->getOperand(0), NegOp1, NegOp2,
322                                 I->getName() + ".neg", /*MDFrom=*/I);
323   }
324   case Instruction::ShuffleVector: {
325     // `shufflevector` is negatible if both operands are negatible.
326     auto *Shuf = cast<ShuffleVectorInst>(I);
327     Value *NegOp0 = negate(I->getOperand(0), Depth + 1);
328     if (!NegOp0) // Early return.
329       return nullptr;
330     Value *NegOp1 = negate(I->getOperand(1), Depth + 1);
331     if (!NegOp1)
332       return nullptr;
333     return Builder.CreateShuffleVector(NegOp0, NegOp1, Shuf->getShuffleMask(),
334                                        I->getName() + ".neg");
335   }
336   case Instruction::ExtractElement: {
337     // `extractelement` is negatible if source operand is negatible.
338     auto *EEI = cast<ExtractElementInst>(I);
339     Value *NegVector = negate(EEI->getVectorOperand(), Depth + 1);
340     if (!NegVector) // Early return.
341       return nullptr;
342     return Builder.CreateExtractElement(NegVector, EEI->getIndexOperand(),
343                                         I->getName() + ".neg");
344   }
345   case Instruction::InsertElement: {
346     // `insertelement` is negatible if both the source vector and
347     // element-to-be-inserted are negatible.
348     auto *IEI = cast<InsertElementInst>(I);
349     Value *NegVector = negate(IEI->getOperand(0), Depth + 1);
350     if (!NegVector) // Early return.
351       return nullptr;
352     Value *NegNewElt = negate(IEI->getOperand(1), Depth + 1);
353     if (!NegNewElt) // Early return.
354       return nullptr;
355     return Builder.CreateInsertElement(NegVector, NegNewElt, IEI->getOperand(2),
356                                        I->getName() + ".neg");
357   }
358   case Instruction::Trunc: {
359     // `trunc` is negatible if its operand is negatible.
360     Value *NegOp = negate(I->getOperand(0), Depth + 1);
361     if (!NegOp) // Early return.
362       return nullptr;
363     return Builder.CreateTrunc(NegOp, I->getType(), I->getName() + ".neg");
364   }
365   case Instruction::Shl: {
366     // `shl` is negatible if the first operand is negatible.
367     if (Value *NegOp0 = negate(I->getOperand(0), Depth + 1))
368       return Builder.CreateShl(NegOp0, I->getOperand(1), I->getName() + ".neg");
369     // Otherwise, `shl %x, C` can be interpreted as `mul %x, 1<<C`.
370     auto *Op1C = dyn_cast<Constant>(I->getOperand(1));
371     if (!Op1C) // Early return.
372       return nullptr;
373     return Builder.CreateMul(
374         I->getOperand(0),
375         ConstantExpr::getShl(Constant::getAllOnesValue(Op1C->getType()), Op1C),
376         I->getName() + ".neg");
377   }
378   case Instruction::Or: {
379     if (!haveNoCommonBitsSet(I->getOperand(0), I->getOperand(1), DL, &AC, I,
380                              &DT))
381       return nullptr; // Don't know how to handle `or` in general.
382     std::array<Value *, 2> Ops = getSortedOperandsOfBinOp(I);
383     // `or`/`add` are interchangeable when operands have no common bits set.
384     // `inc` is always negatible.
385     if (match(Ops[1], m_One()))
386       return Builder.CreateNot(Ops[0], I->getName() + ".neg");
387     // Else, just defer to Instruction::Add handling.
388     LLVM_FALLTHROUGH;
389   }
390   case Instruction::Add: {
391     // `add` is negatible if both of its operands are negatible.
392     SmallVector<Value *, 2> NegatedOps, NonNegatedOps;
393     for (Value *Op : I->operands()) {
394       // Can we sink the negation into this operand?
395       if (Value *NegOp = negate(Op, Depth + 1)) {
396         NegatedOps.emplace_back(NegOp); // Successfully negated operand!
397         continue;
398       }
399       // Failed to sink negation into this operand. IFF we started from negation
400       // and we manage to sink negation into one operand, we can still do this.
401       if (!IsTrulyNegation)
402         return nullptr;
403       NonNegatedOps.emplace_back(Op); // Just record which operand that was.
404     }
405     assert((NegatedOps.size() + NonNegatedOps.size()) == 2 &&
406            "Internal consistency check failed.");
407     // Did we manage to sink negation into both of the operands?
408     if (NegatedOps.size() == 2) // Then we get to keep the `add`!
409       return Builder.CreateAdd(NegatedOps[0], NegatedOps[1],
410                                I->getName() + ".neg");
411     assert(IsTrulyNegation && "We should have early-exited then.");
412     // Completely failed to sink negation?
413     if (NonNegatedOps.size() == 2)
414       return nullptr;
415     // 0-(a+b) --> (-a)-b
416     return Builder.CreateSub(NegatedOps[0], NonNegatedOps[0],
417                              I->getName() + ".neg");
418   }
419   case Instruction::Xor: {
420     std::array<Value *, 2> Ops = getSortedOperandsOfBinOp(I);
421     // `xor` is negatible if one of its operands is invertible.
422     // FIXME: InstCombineInverter? But how to connect Inverter and Negator?
423     if (auto *C = dyn_cast<Constant>(Ops[1])) {
424       Value *Xor = Builder.CreateXor(Ops[0], ConstantExpr::getNot(C));
425       return Builder.CreateAdd(Xor, ConstantInt::get(Xor->getType(), 1),
426                                I->getName() + ".neg");
427     }
428     return nullptr;
429   }
430   case Instruction::Mul: {
431     std::array<Value *, 2> Ops = getSortedOperandsOfBinOp(I);
432     // `mul` is negatible if one of its operands is negatible.
433     Value *NegatedOp, *OtherOp;
434     // First try the second operand, in case it's a constant it will be best to
435     // just invert it instead of sinking the `neg` deeper.
436     if (Value *NegOp1 = negate(Ops[1], Depth + 1)) {
437       NegatedOp = NegOp1;
438       OtherOp = Ops[0];
439     } else if (Value *NegOp0 = negate(Ops[0], Depth + 1)) {
440       NegatedOp = NegOp0;
441       OtherOp = Ops[1];
442     } else
443       // Can't negate either of them.
444       return nullptr;
445     return Builder.CreateMul(NegatedOp, OtherOp, I->getName() + ".neg");
446   }
447   default:
448     return nullptr; // Don't know, likely not negatible for free.
449   }
450 
451   llvm_unreachable("Can't get here. We always return from switch.");
452 }
453 
454 LLVM_NODISCARD Value *Negator::negate(Value *V, unsigned Depth) {
455   NegatorMaxDepthVisited.updateMax(Depth);
456   ++NegatorNumValuesVisited;
457 
458 #if LLVM_ENABLE_STATS
459   ++NumValuesVisitedInThisNegator;
460 #endif
461 
462 #ifndef NDEBUG
463   // We can't ever have a Value with such an address.
464   Value *Placeholder = reinterpret_cast<Value *>(static_cast<uintptr_t>(-1));
465 #endif
466 
467   // Did we already try to negate this value?
468   auto NegationsCacheIterator = NegationsCache.find(V);
469   if (NegationsCacheIterator != NegationsCache.end()) {
470     ++NegatorNumNegationsFoundInCache;
471     Value *NegatedV = NegationsCacheIterator->second;
472     assert(NegatedV != Placeholder && "Encountered a cycle during negation.");
473     return NegatedV;
474   }
475 
476 #ifndef NDEBUG
477   // We did not find a cached result for negation of V. While there,
478   // let's temporairly cache a placeholder value, with the idea that if later
479   // during negation we fetch it from cache, we'll know we're in a cycle.
480   NegationsCache[V] = Placeholder;
481 #endif
482 
483   // No luck. Try negating it for real.
484   Value *NegatedV = visitImpl(V, Depth);
485   // And cache the (real) result for the future.
486   NegationsCache[V] = NegatedV;
487 
488   return NegatedV;
489 }
490 
491 LLVM_NODISCARD Optional<Negator::Result> Negator::run(Value *Root) {
492   Value *Negated = negate(Root, /*Depth=*/0);
493   if (!Negated) {
494     // We must cleanup newly-inserted instructions, to avoid any potential
495     // endless combine looping.
496     for (Instruction *I : llvm::reverse(NewInstructions))
497       I->eraseFromParent();
498     return llvm::None;
499   }
500   return std::make_pair(ArrayRef<Instruction *>(NewInstructions), Negated);
501 }
502 
503 LLVM_NODISCARD Value *Negator::Negate(bool LHSIsZero, Value *Root,
504                                       InstCombinerImpl &IC) {
505   ++NegatorTotalNegationsAttempted;
506   LLVM_DEBUG(dbgs() << "Negator: attempting to sink negation into " << *Root
507                     << "\n");
508 
509   if (!NegatorEnabled || !DebugCounter::shouldExecute(NegatorCounter))
510     return nullptr;
511 
512   Negator N(Root->getContext(), IC.getDataLayout(), IC.getAssumptionCache(),
513             IC.getDominatorTree(), LHSIsZero);
514   Optional<Result> Res = N.run(Root);
515   if (!Res) { // Negation failed.
516     LLVM_DEBUG(dbgs() << "Negator: failed to sink negation into " << *Root
517                       << "\n");
518     return nullptr;
519   }
520 
521   LLVM_DEBUG(dbgs() << "Negator: successfully sunk negation into " << *Root
522                     << "\n         NEW: " << *Res->second << "\n");
523   ++NegatorNumTreesNegated;
524 
525   // We must temporarily unset the 'current' insertion point and DebugLoc of the
526   // InstCombine's IRBuilder so that it won't interfere with the ones we have
527   // already specified when producing negated instructions.
528   InstCombiner::BuilderTy::InsertPointGuard Guard(IC.Builder);
529   IC.Builder.ClearInsertionPoint();
530   IC.Builder.SetCurrentDebugLocation(DebugLoc());
531 
532   // And finally, we must add newly-created instructions into the InstCombine's
533   // worklist (in a proper order!) so it can attempt to combine them.
534   LLVM_DEBUG(dbgs() << "Negator: Propagating " << Res->first.size()
535                     << " instrs to InstCombine\n");
536   NegatorMaxInstructionsCreated.updateMax(Res->first.size());
537   NegatorNumInstructionsNegatedSuccess += Res->first.size();
538 
539   // They are in def-use order, so nothing fancy, just insert them in order.
540   for (Instruction *I : Res->first)
541     IC.Builder.Insert(I, I->getName());
542 
543   // And return the new root.
544   return Res->second;
545 }
546