1 //===- IROutliner.cpp -- Outline Similar Regions ----------------*- 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 /// \file
10 // Implementation for the IROutliner which is used by the IROutliner Pass.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/IPO/IROutliner.h"
15 #include "llvm/Analysis/IRSimilarityIdentifier.h"
16 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
17 #include "llvm/Analysis/TargetTransformInfo.h"
18 #include "llvm/IR/Attributes.h"
19 #include "llvm/IR/PassManager.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Transforms/IPO.h"
24 #include <map>
25 #include <set>
26 #include <vector>
27 
28 #define DEBUG_TYPE "iroutliner"
29 
30 using namespace llvm;
31 using namespace IRSimilarity;
32 
33 // This is a debug option to test small pieces of code to ensure that outlining
34 // works correctly.
35 static cl::opt<bool> NoCostModel(
36     "ir-outlining-no-cost", cl::init(false), cl::ReallyHidden,
37     cl::desc("Debug option to outline greedily, without restriction that "
38              "calculated benefit outweighs cost"));
39 
40 /// The OutlinableGroup holds all the overarching information for outlining
41 /// a set of regions that are structurally similar to one another, such as the
42 /// types of the overall function, the output blocks, the sets of stores needed
43 /// and a list of the different regions. This information is used in the
44 /// deduplication of extracted regions with the same structure.
45 struct OutlinableGroup {
46   /// The sections that could be outlined
47   std::vector<OutlinableRegion *> Regions;
48 
49   /// The argument types for the function created as the overall function to
50   /// replace the extracted function for each region.
51   std::vector<Type *> ArgumentTypes;
52   /// The FunctionType for the overall function.
53   FunctionType *OutlinedFunctionType = nullptr;
54   /// The Function for the collective overall function.
55   Function *OutlinedFunction = nullptr;
56 
57   /// Flag for whether we should not consider this group of OutlinableRegions
58   /// for extraction.
59   bool IgnoreGroup = false;
60 
61   /// The return block for the overall function.
62   BasicBlock *EndBB = nullptr;
63 
64   /// A set containing the different GVN store sets needed. Each array contains
65   /// a sorted list of the different values that need to be stored into output
66   /// registers.
67   DenseSet<ArrayRef<unsigned>> OutputGVNCombinations;
68 
69   /// Flag for whether the \ref ArgumentTypes have been defined after the
70   /// extraction of the first region.
71   bool InputTypesSet = false;
72 
73   /// The number of input values in \ref ArgumentTypes.  Anything after this
74   /// index in ArgumentTypes is an output argument.
75   unsigned NumAggregateInputs = 0;
76 
77   /// The number of instructions that will be outlined by extracting \ref
78   /// Regions.
79   unsigned Benefit = 0;
80   /// The number of added instructions needed for the outlining of the \ref
81   /// Regions.
82   unsigned Cost = 0;
83 
84   /// The argument that needs to be marked with the swifterr attribute.  If not
85   /// needed, there is no value.
86   Optional<unsigned> SwiftErrorArgument;
87 
88   /// For the \ref Regions, we look at every Value.  If it is a constant,
89   /// we check whether it is the same in Region.
90   ///
91   /// \param [in,out] NotSame contains the global value numbers where the
92   /// constant is not always the same, and must be passed in as an argument.
93   void findSameConstants(DenseSet<unsigned> &NotSame);
94 
95   /// For the regions, look at each set of GVN stores needed and account for
96   /// each combination.  Add an argument to the argument types if there is
97   /// more than one combination.
98   ///
99   /// \param [in] M - The module we are outlining from.
100   void collectGVNStoreSets(Module &M);
101 };
102 
103 /// Move the contents of \p SourceBB to before the last instruction of \p
104 /// TargetBB.
105 /// \param SourceBB - the BasicBlock to pull Instructions from.
106 /// \param TargetBB - the BasicBlock to put Instruction into.
107 static void moveBBContents(BasicBlock &SourceBB, BasicBlock &TargetBB) {
108   BasicBlock::iterator BBCurr, BBEnd, BBNext;
109   for (BBCurr = SourceBB.begin(), BBEnd = SourceBB.end(); BBCurr != BBEnd;
110        BBCurr = BBNext) {
111     BBNext = std::next(BBCurr);
112     BBCurr->moveBefore(TargetBB, TargetBB.end());
113   }
114 }
115 
116 void OutlinableRegion::splitCandidate() {
117   assert(!CandidateSplit && "Candidate already split!");
118 
119   Instruction *StartInst = (*Candidate->begin()).Inst;
120   Instruction *EndInst = (*Candidate->end()).Inst;
121   assert(StartInst && EndInst && "Expected a start and end instruction?");
122   StartBB = StartInst->getParent();
123   PrevBB = StartBB;
124 
125   // The basic block gets split like so:
126   // block:                 block:
127   //   inst1                  inst1
128   //   inst2                  inst2
129   //   region1               br block_to_outline
130   //   region2              block_to_outline:
131   //   region3          ->    region1
132   //   region4                region2
133   //   inst3                  region3
134   //   inst4                  region4
135   //                          br block_after_outline
136   //                        block_after_outline:
137   //                          inst3
138   //                          inst4
139 
140   std::string OriginalName = PrevBB->getName().str();
141 
142   StartBB = PrevBB->splitBasicBlock(StartInst, OriginalName + "_to_outline");
143 
144   // This is the case for the inner block since we do not have to include
145   // multiple blocks.
146   EndBB = StartBB;
147   FollowBB = EndBB->splitBasicBlock(EndInst, OriginalName + "_after_outline");
148 
149   CandidateSplit = true;
150 }
151 
152 void OutlinableRegion::reattachCandidate() {
153   assert(CandidateSplit && "Candidate is not split!");
154 
155   // The basic block gets reattached like so:
156   // block:                        block:
157   //   inst1                         inst1
158   //   inst2                         inst2
159   //   br block_to_outline           region1
160   // block_to_outline:        ->     region2
161   //   region1                       region3
162   //   region2                       region4
163   //   region3                       inst3
164   //   region4                       inst4
165   //   br block_after_outline
166   // block_after_outline:
167   //   inst3
168   //   inst4
169   assert(StartBB != nullptr && "StartBB for Candidate is not defined!");
170   assert(FollowBB != nullptr && "StartBB for Candidate is not defined!");
171 
172   // StartBB should only have one predecessor since we put an unconditional
173   // branch at the end of PrevBB when we split the BasicBlock.
174   PrevBB = StartBB->getSinglePredecessor();
175   assert(PrevBB != nullptr &&
176          "No Predecessor for the region start basic block!");
177 
178   assert(PrevBB->getTerminator() && "Terminator removed from PrevBB!");
179   assert(EndBB->getTerminator() && "Terminator removed from EndBB!");
180   PrevBB->getTerminator()->eraseFromParent();
181   EndBB->getTerminator()->eraseFromParent();
182 
183   moveBBContents(*StartBB, *PrevBB);
184 
185   BasicBlock *PlacementBB = PrevBB;
186   if (StartBB != EndBB)
187     PlacementBB = EndBB;
188   moveBBContents(*FollowBB, *PlacementBB);
189 
190   PrevBB->replaceSuccessorsPhiUsesWith(StartBB, PrevBB);
191   PrevBB->replaceSuccessorsPhiUsesWith(FollowBB, PlacementBB);
192   StartBB->eraseFromParent();
193   FollowBB->eraseFromParent();
194 
195   // Make sure to save changes back to the StartBB.
196   StartBB = PrevBB;
197   EndBB = nullptr;
198   PrevBB = nullptr;
199   FollowBB = nullptr;
200 
201   CandidateSplit = false;
202 }
203 
204 /// Find whether \p V matches the Constants previously found for the \p GVN.
205 ///
206 /// \param V - The value to check for consistency.
207 /// \param GVN - The global value number assigned to \p V.
208 /// \param GVNToConstant - The mapping of global value number to Constants.
209 /// \returns true if the Value matches the Constant mapped to by V and false if
210 /// it \p V is a Constant but does not match.
211 /// \returns None if \p V is not a Constant.
212 static Optional<bool>
213 constantMatches(Value *V, unsigned GVN,
214                 DenseMap<unsigned, Constant *> &GVNToConstant) {
215   // See if we have a constants
216   Constant *CST = dyn_cast<Constant>(V);
217   if (!CST)
218     return None;
219 
220   // Holds a mapping from a global value number to a Constant.
221   DenseMap<unsigned, Constant *>::iterator GVNToConstantIt;
222   bool Inserted;
223 
224 
225   // If we have a constant, try to make a new entry in the GVNToConstant.
226   std::tie(GVNToConstantIt, Inserted) =
227       GVNToConstant.insert(std::make_pair(GVN, CST));
228   // If it was found and is not equal, it is not the same. We do not
229   // handle this case yet, and exit early.
230   if (Inserted || (GVNToConstantIt->second == CST))
231     return true;
232 
233   return false;
234 }
235 
236 unsigned OutlinableRegion::getBenefit(TargetTransformInfo &TTI) {
237   InstructionCost Benefit(0);
238 
239   // Estimate the benefit of outlining a specific sections of the program.  We
240   // delegate mostly this task to the TargetTransformInfo so that if the target
241   // has specific changes, we can have a more accurate estimate.
242 
243   // However, getInstructionCost delegates the code size calculation for
244   // arithmetic instructions to getArithmeticInstrCost in
245   // include/Analysis/TargetTransformImpl.h, where it always estimates that the
246   // code size for a division and remainder instruction to be equal to 4, and
247   // everything else to 1.  This is not an accurate representation of the
248   // division instruction for targets that have a native division instruction.
249   // To be overly conservative, we only add 1 to the number of instructions for
250   // each division instruction.
251   for (Instruction &I : *StartBB) {
252     switch (I.getOpcode()) {
253     case Instruction::FDiv:
254     case Instruction::FRem:
255     case Instruction::SDiv:
256     case Instruction::SRem:
257     case Instruction::UDiv:
258     case Instruction::URem:
259       Benefit += 1;
260       break;
261     default:
262       Benefit += TTI.getInstructionCost(&I, TargetTransformInfo::TCK_CodeSize);
263       break;
264     }
265   }
266 
267   return *Benefit.getValue();
268 }
269 
270 /// Find whether \p Region matches the global value numbering to Constant
271 /// mapping found so far.
272 ///
273 /// \param Region - The OutlinableRegion we are checking for constants
274 /// \param GVNToConstant - The mapping of global value number to Constants.
275 /// \param NotSame - The set of global value numbers that do not have the same
276 /// constant in each region.
277 /// \returns true if all Constants are the same in every use of a Constant in \p
278 /// Region and false if not
279 static bool
280 collectRegionsConstants(OutlinableRegion &Region,
281                         DenseMap<unsigned, Constant *> &GVNToConstant,
282                         DenseSet<unsigned> &NotSame) {
283   bool ConstantsTheSame = true;
284 
285   IRSimilarityCandidate &C = *Region.Candidate;
286   for (IRInstructionData &ID : C) {
287 
288     // Iterate over the operands in an instruction. If the global value number,
289     // assigned by the IRSimilarityCandidate, has been seen before, we check if
290     // the the number has been found to be not the same value in each instance.
291     for (Value *V : ID.OperVals) {
292       Optional<unsigned> GVNOpt = C.getGVN(V);
293       assert(GVNOpt.hasValue() && "Expected a GVN for operand?");
294       unsigned GVN = GVNOpt.getValue();
295 
296       // Check if this global value has been found to not be the same already.
297       if (NotSame.find(GVN) != NotSame.end()) {
298         if (isa<Constant>(V))
299           ConstantsTheSame = false;
300         continue;
301       }
302 
303       // If it has been the same so far, we check the value for if the
304       // associated Constant value match the previous instances of the same
305       // global value number.  If the global value does not map to a Constant,
306       // it is considered to not be the same value.
307       Optional<bool> ConstantMatches = constantMatches(V, GVN, GVNToConstant);
308       if (ConstantMatches.hasValue()) {
309         if (ConstantMatches.getValue())
310           continue;
311         else
312           ConstantsTheSame = false;
313       }
314 
315       // While this value is a register, it might not have been previously,
316       // make sure we don't already have a constant mapped to this global value
317       // number.
318       if (GVNToConstant.find(GVN) != GVNToConstant.end())
319         ConstantsTheSame = false;
320 
321       NotSame.insert(GVN);
322     }
323   }
324 
325   return ConstantsTheSame;
326 }
327 
328 void OutlinableGroup::findSameConstants(DenseSet<unsigned> &NotSame) {
329   DenseMap<unsigned, Constant *> GVNToConstant;
330 
331   for (OutlinableRegion *Region : Regions)
332     collectRegionsConstants(*Region, GVNToConstant, NotSame);
333 }
334 
335 void OutlinableGroup::collectGVNStoreSets(Module &M) {
336   for (OutlinableRegion *OS : Regions)
337     OutputGVNCombinations.insert(OS->GVNStores);
338 
339   // We are adding an extracted argument to decide between which output path
340   // to use in the basic block.  It is used in a switch statement and only
341   // needs to be an integer.
342   if (OutputGVNCombinations.size() > 1)
343     ArgumentTypes.push_back(Type::getInt32Ty(M.getContext()));
344 }
345 
346 Function *IROutliner::createFunction(Module &M, OutlinableGroup &Group,
347                                      unsigned FunctionNameSuffix) {
348   assert(!Group.OutlinedFunction && "Function is already defined!");
349 
350   Group.OutlinedFunctionType = FunctionType::get(
351       Type::getVoidTy(M.getContext()), Group.ArgumentTypes, false);
352 
353   // These functions will only be called from within the same module, so
354   // we can set an internal linkage.
355   Group.OutlinedFunction = Function::Create(
356       Group.OutlinedFunctionType, GlobalValue::InternalLinkage,
357       "outlined_ir_func_" + std::to_string(FunctionNameSuffix), M);
358 
359   // Transfer the swifterr attribute to the correct function parameter.
360   if (Group.SwiftErrorArgument.hasValue())
361     Group.OutlinedFunction->addParamAttr(Group.SwiftErrorArgument.getValue(),
362                                          Attribute::SwiftError);
363 
364   Group.OutlinedFunction->addFnAttr(Attribute::OptimizeForSize);
365   Group.OutlinedFunction->addFnAttr(Attribute::MinSize);
366 
367   return Group.OutlinedFunction;
368 }
369 
370 /// Move each BasicBlock in \p Old to \p New.
371 ///
372 /// \param [in] Old - the function to move the basic blocks from.
373 /// \param [in] New - The function to move the basic blocks to.
374 /// \returns the first return block for the function in New.
375 static BasicBlock *moveFunctionData(Function &Old, Function &New) {
376   Function::iterator CurrBB, NextBB, FinalBB;
377   BasicBlock *NewEnd = nullptr;
378   std::vector<Instruction *> DebugInsts;
379   for (CurrBB = Old.begin(), FinalBB = Old.end(); CurrBB != FinalBB;
380        CurrBB = NextBB) {
381     NextBB = std::next(CurrBB);
382     CurrBB->removeFromParent();
383     CurrBB->insertInto(&New);
384     Instruction *I = CurrBB->getTerminator();
385     if (isa<ReturnInst>(I))
386       NewEnd = &(*CurrBB);
387   }
388 
389   assert(NewEnd && "No return instruction for new function?");
390   return NewEnd;
391 }
392 
393 /// Find the the constants that will need to be lifted into arguments
394 /// as they are not the same in each instance of the region.
395 ///
396 /// \param [in] C - The IRSimilarityCandidate containing the region we are
397 /// analyzing.
398 /// \param [in] NotSame - The set of global value numbers that do not have a
399 /// single Constant across all OutlinableRegions similar to \p C.
400 /// \param [out] Inputs - The list containing the global value numbers of the
401 /// arguments needed for the region of code.
402 static void findConstants(IRSimilarityCandidate &C, DenseSet<unsigned> &NotSame,
403                           std::vector<unsigned> &Inputs) {
404   DenseSet<unsigned> Seen;
405   // Iterate over the instructions, and find what constants will need to be
406   // extracted into arguments.
407   for (IRInstructionDataList::iterator IDIt = C.begin(), EndIDIt = C.end();
408        IDIt != EndIDIt; IDIt++) {
409     for (Value *V : (*IDIt).OperVals) {
410       // Since these are stored before any outlining, they will be in the
411       // global value numbering.
412       unsigned GVN = C.getGVN(V).getValue();
413       if (isa<Constant>(V))
414         if (NotSame.find(GVN) != NotSame.end() &&
415             Seen.find(GVN) == Seen.end()) {
416           Inputs.push_back(GVN);
417           Seen.insert(GVN);
418         }
419     }
420   }
421 }
422 
423 /// Find the GVN for the inputs that have been found by the CodeExtractor.
424 ///
425 /// \param [in] C - The IRSimilarityCandidate containing the region we are
426 /// analyzing.
427 /// \param [in] CurrentInputs - The set of inputs found by the
428 /// CodeExtractor.
429 /// \param [out] EndInputNumbers - The global value numbers for the extracted
430 /// arguments.
431 /// \param [in] OutputMappings - The mapping of values that have been replaced
432 /// by a new output value.
433 /// \param [out] EndInputs - The global value numbers for the extracted
434 /// arguments.
435 static void mapInputsToGVNs(IRSimilarityCandidate &C,
436                             SetVector<Value *> &CurrentInputs,
437                             const DenseMap<Value *, Value *> &OutputMappings,
438                             std::vector<unsigned> &EndInputNumbers) {
439   // Get the Global Value Number for each input.  We check if the Value has been
440   // replaced by a different value at output, and use the original value before
441   // replacement.
442   for (Value *Input : CurrentInputs) {
443     assert(Input && "Have a nullptr as an input");
444     if (OutputMappings.find(Input) != OutputMappings.end())
445       Input = OutputMappings.find(Input)->second;
446     assert(C.getGVN(Input).hasValue() &&
447            "Could not find a numbering for the given input");
448     EndInputNumbers.push_back(C.getGVN(Input).getValue());
449   }
450 }
451 
452 /// Find the original value for the \p ArgInput values if any one of them was
453 /// replaced during a previous extraction.
454 ///
455 /// \param [in] ArgInputs - The inputs to be extracted by the code extractor.
456 /// \param [in] OutputMappings - The mapping of values that have been replaced
457 /// by a new output value.
458 /// \param [out] RemappedArgInputs - The remapped values according to
459 /// \p OutputMappings that will be extracted.
460 static void
461 remapExtractedInputs(const ArrayRef<Value *> ArgInputs,
462                      const DenseMap<Value *, Value *> &OutputMappings,
463                      SetVector<Value *> &RemappedArgInputs) {
464   // Get the global value number for each input that will be extracted as an
465   // argument by the code extractor, remapping if needed for reloaded values.
466   for (Value *Input : ArgInputs) {
467     if (OutputMappings.find(Input) != OutputMappings.end())
468       Input = OutputMappings.find(Input)->second;
469     RemappedArgInputs.insert(Input);
470   }
471 }
472 
473 /// Find the input GVNs and the output values for a region of Instructions.
474 /// Using the code extractor, we collect the inputs to the extracted function.
475 ///
476 /// The \p Region can be identified as needing to be ignored in this function.
477 /// It should be checked whether it should be ignored after a call to this
478 /// function.
479 ///
480 /// \param [in,out] Region - The region of code to be analyzed.
481 /// \param [out] InputGVNs - The global value numbers for the extracted
482 /// arguments.
483 /// \param [in] NotSame - The global value numbers in the region that do not
484 /// have the same constant value in the regions structurally similar to
485 /// \p Region.
486 /// \param [in] OutputMappings - The mapping of values that have been replaced
487 /// by a new output value after extraction.
488 /// \param [out] ArgInputs - The values of the inputs to the extracted function.
489 /// \param [out] Outputs - The set of values extracted by the CodeExtractor
490 /// as outputs.
491 static void getCodeExtractorArguments(
492     OutlinableRegion &Region, std::vector<unsigned> &InputGVNs,
493     DenseSet<unsigned> &NotSame, DenseMap<Value *, Value *> &OutputMappings,
494     SetVector<Value *> &ArgInputs, SetVector<Value *> &Outputs) {
495   IRSimilarityCandidate &C = *Region.Candidate;
496 
497   // OverallInputs are the inputs to the region found by the CodeExtractor,
498   // SinkCands and HoistCands are used by the CodeExtractor to find sunken
499   // allocas of values whose lifetimes are contained completely within the
500   // outlined region. PremappedInputs are the arguments found by the
501   // CodeExtractor, removing conditions such as sunken allocas, but that
502   // may need to be remapped due to the extracted output values replacing
503   // the original values.
504   SetVector<Value *> OverallInputs, PremappedInputs, SinkCands, HoistCands;
505 
506   // Use the code extractor to get the inputs and outputs, without sunken
507   // allocas or removing llvm.assumes.
508   CodeExtractor *CE = Region.CE;
509   CE->findInputsOutputs(OverallInputs, Outputs, SinkCands);
510   assert(Region.StartBB && "Region must have a start BasicBlock!");
511   Function *OrigF = Region.StartBB->getParent();
512   CodeExtractorAnalysisCache CEAC(*OrigF);
513   BasicBlock *Dummy = nullptr;
514 
515   // The region may be ineligible due to VarArgs in the parent function. In this
516   // case we ignore the region.
517   if (!CE->isEligible()) {
518     Region.IgnoreRegion = true;
519     return;
520   }
521 
522   // Find if any values are going to be sunk into the function when extracted
523   CE->findAllocas(CEAC, SinkCands, HoistCands, Dummy);
524   CE->findInputsOutputs(PremappedInputs, Outputs, SinkCands);
525 
526   // TODO: Support regions with sunken allocas: values whose lifetimes are
527   // contained completely within the outlined region.  These are not guaranteed
528   // to be the same in every region, so we must elevate them all to arguments
529   // when they appear.  If these values are not equal, it means there is some
530   // Input in OverallInputs that was removed for ArgInputs.
531   if (OverallInputs.size() != PremappedInputs.size()) {
532     Region.IgnoreRegion = true;
533     return;
534   }
535 
536   findConstants(C, NotSame, InputGVNs);
537 
538   mapInputsToGVNs(C, OverallInputs, OutputMappings, InputGVNs);
539 
540   remapExtractedInputs(PremappedInputs.getArrayRef(), OutputMappings,
541                        ArgInputs);
542 
543   // Sort the GVNs, since we now have constants included in the \ref InputGVNs
544   // we need to make sure they are in a deterministic order.
545   stable_sort(InputGVNs.begin(), InputGVNs.end());
546 }
547 
548 /// Look over the inputs and map each input argument to an argument in the
549 /// overall function for the OutlinableRegions.  This creates a way to replace
550 /// the arguments of the extracted function with the arguments of the new
551 /// overall function.
552 ///
553 /// \param [in,out] Region - The region of code to be analyzed.
554 /// \param [in] InputsGVNs - The global value numbering of the input values
555 /// collected.
556 /// \param [in] ArgInputs - The values of the arguments to the extracted
557 /// function.
558 static void
559 findExtractedInputToOverallInputMapping(OutlinableRegion &Region,
560                                         std::vector<unsigned> &InputGVNs,
561                                         SetVector<Value *> &ArgInputs) {
562 
563   IRSimilarityCandidate &C = *Region.Candidate;
564   OutlinableGroup &Group = *Region.Parent;
565 
566   // This counts the argument number in the overall function.
567   unsigned TypeIndex = 0;
568 
569   // This counts the argument number in the extracted function.
570   unsigned OriginalIndex = 0;
571 
572   // Find the mapping of the extracted arguments to the arguments for the
573   // overall function. Since there may be extra arguments in the overall
574   // function to account for the extracted constants, we have two different
575   // counters as we find extracted arguments, and as we come across overall
576   // arguments.
577   for (unsigned InputVal : InputGVNs) {
578     Optional<Value *> InputOpt = C.fromGVN(InputVal);
579     assert(InputOpt.hasValue() && "Global value number not found?");
580     Value *Input = InputOpt.getValue();
581 
582     if (!Group.InputTypesSet) {
583       Group.ArgumentTypes.push_back(Input->getType());
584       // If the input value has a swifterr attribute, make sure to mark the
585       // argument in the overall function.
586       if (Input->isSwiftError()) {
587         assert(
588             !Group.SwiftErrorArgument.hasValue() &&
589             "Argument already marked with swifterr for this OutlinableGroup!");
590         Group.SwiftErrorArgument = TypeIndex;
591       }
592     }
593 
594     // Check if we have a constant. If we do add it to the overall argument
595     // number to Constant map for the region, and continue to the next input.
596     if (Constant *CST = dyn_cast<Constant>(Input)) {
597       Region.AggArgToConstant.insert(std::make_pair(TypeIndex, CST));
598       TypeIndex++;
599       continue;
600     }
601 
602     // It is not a constant, we create the mapping from extracted argument list
603     // to the overall argument list.
604     assert(ArgInputs.count(Input) && "Input cannot be found!");
605 
606     Region.ExtractedArgToAgg.insert(std::make_pair(OriginalIndex, TypeIndex));
607     Region.AggArgToExtracted.insert(std::make_pair(TypeIndex, OriginalIndex));
608     OriginalIndex++;
609     TypeIndex++;
610   }
611 
612   // If the function type definitions for the OutlinableGroup holding the region
613   // have not been set, set the length of the inputs here.  We should have the
614   // same inputs for all of the different regions contained in the
615   // OutlinableGroup since they are all structurally similar to one another.
616   if (!Group.InputTypesSet) {
617     Group.NumAggregateInputs = TypeIndex;
618     Group.InputTypesSet = true;
619   }
620 
621   Region.NumExtractedInputs = OriginalIndex;
622 }
623 
624 /// Create a mapping of the output arguments for the \p Region to the output
625 /// arguments of the overall outlined function.
626 ///
627 /// \param [in,out] Region - The region of code to be analyzed.
628 /// \param [in] Outputs - The values found by the code extractor.
629 static void
630 findExtractedOutputToOverallOutputMapping(OutlinableRegion &Region,
631                                           ArrayRef<Value *> Outputs) {
632   OutlinableGroup &Group = *Region.Parent;
633   IRSimilarityCandidate &C = *Region.Candidate;
634 
635   // This counts the argument number in the extracted function.
636   unsigned OriginalIndex = Region.NumExtractedInputs;
637 
638   // This counts the argument number in the overall function.
639   unsigned TypeIndex = Group.NumAggregateInputs;
640   bool TypeFound;
641   DenseSet<unsigned> AggArgsUsed;
642 
643   // Iterate over the output types and identify if there is an aggregate pointer
644   // type whose base type matches the current output type. If there is, we mark
645   // that we will use this output register for this value. If not we add another
646   // type to the overall argument type list. We also store the GVNs used for
647   // stores to identify which values will need to be moved into an special
648   // block that holds the stores to the output registers.
649   for (Value *Output : Outputs) {
650     TypeFound = false;
651     // We can do this since it is a result value, and will have a number
652     // that is necessarily the same. BUT if in the future, the instructions
653     // do not have to be in same order, but are functionally the same, we will
654     // have to use a different scheme, as one-to-one correspondence is not
655     // guaranteed.
656     unsigned GlobalValue = C.getGVN(Output).getValue();
657     unsigned ArgumentSize = Group.ArgumentTypes.size();
658 
659     for (unsigned Jdx = TypeIndex; Jdx < ArgumentSize; Jdx++) {
660       if (Group.ArgumentTypes[Jdx] != PointerType::getUnqual(Output->getType()))
661         continue;
662 
663       if (AggArgsUsed.find(Jdx) != AggArgsUsed.end())
664         continue;
665 
666       TypeFound = true;
667       AggArgsUsed.insert(Jdx);
668       Region.ExtractedArgToAgg.insert(std::make_pair(OriginalIndex, Jdx));
669       Region.AggArgToExtracted.insert(std::make_pair(Jdx, OriginalIndex));
670       Region.GVNStores.push_back(GlobalValue);
671       break;
672     }
673 
674     // We were unable to find an unused type in the output type set that matches
675     // the output, so we add a pointer type to the argument types of the overall
676     // function to handle this output and create a mapping to it.
677     if (!TypeFound) {
678       Group.ArgumentTypes.push_back(PointerType::getUnqual(Output->getType()));
679       AggArgsUsed.insert(Group.ArgumentTypes.size() - 1);
680       Region.ExtractedArgToAgg.insert(
681           std::make_pair(OriginalIndex, Group.ArgumentTypes.size() - 1));
682       Region.AggArgToExtracted.insert(
683           std::make_pair(Group.ArgumentTypes.size() - 1, OriginalIndex));
684       Region.GVNStores.push_back(GlobalValue);
685     }
686 
687     stable_sort(Region.GVNStores);
688     OriginalIndex++;
689     TypeIndex++;
690   }
691 }
692 
693 void IROutliner::findAddInputsOutputs(Module &M, OutlinableRegion &Region,
694                                       DenseSet<unsigned> &NotSame) {
695   std::vector<unsigned> Inputs;
696   SetVector<Value *> ArgInputs, Outputs;
697 
698   getCodeExtractorArguments(Region, Inputs, NotSame, OutputMappings, ArgInputs,
699                             Outputs);
700 
701   if (Region.IgnoreRegion)
702     return;
703 
704   // Map the inputs found by the CodeExtractor to the arguments found for
705   // the overall function.
706   findExtractedInputToOverallInputMapping(Region, Inputs, ArgInputs);
707 
708   // Map the outputs found by the CodeExtractor to the arguments found for
709   // the overall function.
710   findExtractedOutputToOverallOutputMapping(Region, Outputs.getArrayRef());
711 }
712 
713 /// Replace the extracted function in the Region with a call to the overall
714 /// function constructed from the deduplicated similar regions, replacing and
715 /// remapping the values passed to the extracted function as arguments to the
716 /// new arguments of the overall function.
717 ///
718 /// \param [in] M - The module to outline from.
719 /// \param [in] Region - The regions of extracted code to be replaced with a new
720 /// function.
721 /// \returns a call instruction with the replaced function.
722 CallInst *replaceCalledFunction(Module &M, OutlinableRegion &Region) {
723   std::vector<Value *> NewCallArgs;
724   DenseMap<unsigned, unsigned>::iterator ArgPair;
725 
726   OutlinableGroup &Group = *Region.Parent;
727   CallInst *Call = Region.Call;
728   assert(Call && "Call to replace is nullptr?");
729   Function *AggFunc = Group.OutlinedFunction;
730   assert(AggFunc && "Function to replace with is nullptr?");
731 
732   // If the arguments are the same size, there are not values that need to be
733   // made argument, or different output registers to handle.  We can simply
734   // replace the called function in this case.
735   if (AggFunc->arg_size() == Call->arg_size()) {
736     LLVM_DEBUG(dbgs() << "Replace call to " << *Call << " with call to "
737                       << *AggFunc << " with same number of arguments\n");
738     Call->setCalledFunction(AggFunc);
739     return Call;
740   }
741 
742   // We have a different number of arguments than the new function, so
743   // we need to use our previously mappings off extracted argument to overall
744   // function argument, and constants to overall function argument to create the
745   // new argument list.
746   for (unsigned AggArgIdx = 0; AggArgIdx < AggFunc->arg_size(); AggArgIdx++) {
747 
748     if (AggArgIdx == AggFunc->arg_size() - 1 &&
749         Group.OutputGVNCombinations.size() > 1) {
750       // If we are on the last argument, and we need to differentiate between
751       // output blocks, add an integer to the argument list to determine
752       // what block to take
753       LLVM_DEBUG(dbgs() << "Set switch block argument to "
754                         << Region.OutputBlockNum << "\n");
755       NewCallArgs.push_back(ConstantInt::get(Type::getInt32Ty(M.getContext()),
756                                              Region.OutputBlockNum));
757       continue;
758     }
759 
760     ArgPair = Region.AggArgToExtracted.find(AggArgIdx);
761     if (ArgPair != Region.AggArgToExtracted.end()) {
762       Value *ArgumentValue = Call->getArgOperand(ArgPair->second);
763       // If we found the mapping from the extracted function to the overall
764       // function, we simply add it to the argument list.  We use the same
765       // value, it just needs to honor the new order of arguments.
766       LLVM_DEBUG(dbgs() << "Setting argument " << AggArgIdx << " to value "
767                         << *ArgumentValue << "\n");
768       NewCallArgs.push_back(ArgumentValue);
769       continue;
770     }
771 
772     // If it is a constant, we simply add it to the argument list as a value.
773     if (Region.AggArgToConstant.find(AggArgIdx) !=
774         Region.AggArgToConstant.end()) {
775       Constant *CST = Region.AggArgToConstant.find(AggArgIdx)->second;
776       LLVM_DEBUG(dbgs() << "Setting argument " << AggArgIdx << " to value "
777                         << *CST << "\n");
778       NewCallArgs.push_back(CST);
779       continue;
780     }
781 
782     // Add a nullptr value if the argument is not found in the extracted
783     // function.  If we cannot find a value, it means it is not in use
784     // for the region, so we should not pass anything to it.
785     LLVM_DEBUG(dbgs() << "Setting argument " << AggArgIdx << " to nullptr\n");
786     NewCallArgs.push_back(ConstantPointerNull::get(
787         static_cast<PointerType *>(AggFunc->getArg(AggArgIdx)->getType())));
788   }
789 
790   LLVM_DEBUG(dbgs() << "Replace call to " << *Call << " with call to "
791                     << *AggFunc << " with new set of arguments\n");
792   // Create the new call instruction and erase the old one.
793   Call = CallInst::Create(AggFunc->getFunctionType(), AggFunc, NewCallArgs, "",
794                           Call);
795 
796   // It is possible that the call to the outlined function is either the first
797   // instruction is in the new block, the last instruction, or both.  If either
798   // of these is the case, we need to make sure that we replace the instruction
799   // in the IRInstructionData struct with the new call.
800   CallInst *OldCall = Region.Call;
801   if (Region.NewFront->Inst == OldCall)
802     Region.NewFront->Inst = Call;
803   if (Region.NewBack->Inst == OldCall)
804     Region.NewBack->Inst = Call;
805 
806   // Transfer any debug information.
807   Call->setDebugLoc(Region.Call->getDebugLoc());
808 
809   // Remove the old instruction.
810   OldCall->eraseFromParent();
811   Region.Call = Call;
812 
813   // Make sure that the argument in the new function has the SwiftError
814   // argument.
815   if (Group.SwiftErrorArgument.hasValue())
816     Call->addParamAttr(Group.SwiftErrorArgument.getValue(),
817                        Attribute::SwiftError);
818 
819   return Call;
820 }
821 
822 // Within an extracted function, replace the argument uses of the extracted
823 // region with the arguments of the function for an OutlinableGroup.
824 //
825 /// \param [in] Region - The region of extracted code to be changed.
826 /// \param [in,out] OutputBB - The BasicBlock for the output stores for this
827 /// region.
828 static void replaceArgumentUses(OutlinableRegion &Region,
829                                 BasicBlock *OutputBB) {
830   OutlinableGroup &Group = *Region.Parent;
831   assert(Region.ExtractedFunction && "Region has no extracted function?");
832 
833   for (unsigned ArgIdx = 0; ArgIdx < Region.ExtractedFunction->arg_size();
834        ArgIdx++) {
835     assert(Region.ExtractedArgToAgg.find(ArgIdx) !=
836                Region.ExtractedArgToAgg.end() &&
837            "No mapping from extracted to outlined?");
838     unsigned AggArgIdx = Region.ExtractedArgToAgg.find(ArgIdx)->second;
839     Argument *AggArg = Group.OutlinedFunction->getArg(AggArgIdx);
840     Argument *Arg = Region.ExtractedFunction->getArg(ArgIdx);
841     // The argument is an input, so we can simply replace it with the overall
842     // argument value
843     if (ArgIdx < Region.NumExtractedInputs) {
844       LLVM_DEBUG(dbgs() << "Replacing uses of input " << *Arg << " in function "
845                         << *Region.ExtractedFunction << " with " << *AggArg
846                         << " in function " << *Group.OutlinedFunction << "\n");
847       Arg->replaceAllUsesWith(AggArg);
848       continue;
849     }
850 
851     // If we are replacing an output, we place the store value in its own
852     // block inside the overall function before replacing the use of the output
853     // in the function.
854     assert(Arg->hasOneUse() && "Output argument can only have one use");
855     User *InstAsUser = Arg->user_back();
856     assert(InstAsUser && "User is nullptr!");
857 
858     Instruction *I = cast<Instruction>(InstAsUser);
859     I->setDebugLoc(DebugLoc());
860     LLVM_DEBUG(dbgs() << "Move store for instruction " << *I << " to "
861                       << *OutputBB << "\n");
862 
863     I->moveBefore(*OutputBB, OutputBB->end());
864 
865     LLVM_DEBUG(dbgs() << "Replacing uses of output " << *Arg << " in function "
866                       << *Region.ExtractedFunction << " with " << *AggArg
867                       << " in function " << *Group.OutlinedFunction << "\n");
868     Arg->replaceAllUsesWith(AggArg);
869   }
870 }
871 
872 /// Within an extracted function, replace the constants that need to be lifted
873 /// into arguments with the actual argument.
874 ///
875 /// \param Region [in] - The region of extracted code to be changed.
876 void replaceConstants(OutlinableRegion &Region) {
877   OutlinableGroup &Group = *Region.Parent;
878   // Iterate over the constants that need to be elevated into arguments
879   for (std::pair<unsigned, Constant *> &Const : Region.AggArgToConstant) {
880     unsigned AggArgIdx = Const.first;
881     Function *OutlinedFunction = Group.OutlinedFunction;
882     assert(OutlinedFunction && "Overall Function is not defined?");
883     Constant *CST = Const.second;
884     Argument *Arg = Group.OutlinedFunction->getArg(AggArgIdx);
885     // Identify the argument it will be elevated to, and replace instances of
886     // that constant in the function.
887 
888     // TODO: If in the future constants do not have one global value number,
889     // i.e. a constant 1 could be mapped to several values, this check will
890     // have to be more strict.  It cannot be using only replaceUsesWithIf.
891 
892     LLVM_DEBUG(dbgs() << "Replacing uses of constant " << *CST
893                       << " in function " << *OutlinedFunction << " with "
894                       << *Arg << "\n");
895     CST->replaceUsesWithIf(Arg, [OutlinedFunction](Use &U) {
896       if (Instruction *I = dyn_cast<Instruction>(U.getUser()))
897         return I->getFunction() == OutlinedFunction;
898       return false;
899     });
900   }
901 }
902 
903 /// For the given function, find all the nondebug or lifetime instructions,
904 /// and return them as a vector. Exclude any blocks in \p ExludeBlocks.
905 ///
906 /// \param [in] F - The function we collect the instructions from.
907 /// \param [in] ExcludeBlocks - BasicBlocks to ignore.
908 /// \returns the list of instructions extracted.
909 static std::vector<Instruction *>
910 collectRelevantInstructions(Function &F,
911                             DenseSet<BasicBlock *> &ExcludeBlocks) {
912   std::vector<Instruction *> RelevantInstructions;
913 
914   for (BasicBlock &BB : F) {
915     if (ExcludeBlocks.find(&BB) != ExcludeBlocks.end())
916       continue;
917 
918     for (Instruction &Inst : BB) {
919       if (Inst.isLifetimeStartOrEnd())
920         continue;
921       if (isa<DbgInfoIntrinsic>(Inst))
922         continue;
923 
924       RelevantInstructions.push_back(&Inst);
925     }
926   }
927 
928   return RelevantInstructions;
929 }
930 
931 /// It is possible that there is a basic block that already performs the same
932 /// stores. This returns a duplicate block, if it exists
933 ///
934 /// \param OutputBB [in] the block we are looking for a duplicate of.
935 /// \param OutputStoreBBs [in] The existing output blocks.
936 /// \returns an optional value with the number output block if there is a match.
937 Optional<unsigned>
938 findDuplicateOutputBlock(BasicBlock *OutputBB,
939                          ArrayRef<BasicBlock *> OutputStoreBBs) {
940 
941   bool WrongInst = false;
942   bool WrongSize = false;
943   unsigned MatchingNum = 0;
944   for (BasicBlock *CompBB : OutputStoreBBs) {
945     WrongInst = false;
946     if (CompBB->size() - 1 != OutputBB->size()) {
947       WrongSize = true;
948       MatchingNum++;
949       continue;
950     }
951 
952     WrongSize = false;
953     BasicBlock::iterator NIt = OutputBB->begin();
954     for (Instruction &I : *CompBB) {
955       if (isa<BranchInst>(&I))
956         continue;
957 
958       if (!I.isIdenticalTo(&(*NIt))) {
959         WrongInst = true;
960         break;
961       }
962 
963       NIt++;
964     }
965     if (!WrongInst && !WrongSize)
966       return MatchingNum;
967 
968     MatchingNum++;
969   }
970 
971   return None;
972 }
973 
974 /// For the outlined section, move needed the StoreInsts for the output
975 /// registers into their own block. Then, determine if there is a duplicate
976 /// output block already created.
977 ///
978 /// \param [in] OG - The OutlinableGroup of regions to be outlined.
979 /// \param [in] Region - The OutlinableRegion that is being analyzed.
980 /// \param [in,out] OutputBB - the block that stores for this region will be
981 /// placed in.
982 /// \param [in] EndBB - the final block of the extracted function.
983 /// \param [in] OutputMappings - OutputMappings the mapping of values that have
984 /// been replaced by a new output value.
985 /// \param [in,out] OutputStoreBBs - The existing output blocks.
986 static void
987 alignOutputBlockWithAggFunc(OutlinableGroup &OG, OutlinableRegion &Region,
988                             BasicBlock *OutputBB, BasicBlock *EndBB,
989                             const DenseMap<Value *, Value *> &OutputMappings,
990                             std::vector<BasicBlock *> &OutputStoreBBs) {
991   DenseSet<unsigned> ValuesToFind(Region.GVNStores.begin(),
992                                   Region.GVNStores.end());
993 
994   // We iterate over the instructions in the extracted function, and find the
995   // global value number of the instructions.  If we find a value that should
996   // be contained in a store, we replace the uses of the value with the value
997   // from the overall function, so that the store is storing the correct
998   // value from the overall function.
999   DenseSet<BasicBlock *> ExcludeBBs(OutputStoreBBs.begin(),
1000                                     OutputStoreBBs.end());
1001   ExcludeBBs.insert(OutputBB);
1002   std::vector<Instruction *> ExtractedFunctionInsts =
1003       collectRelevantInstructions(*(Region.ExtractedFunction), ExcludeBBs);
1004   std::vector<Instruction *> OverallFunctionInsts =
1005       collectRelevantInstructions(*OG.OutlinedFunction, ExcludeBBs);
1006 
1007   assert(ExtractedFunctionInsts.size() == OverallFunctionInsts.size() &&
1008          "Number of relevant instructions not equal!");
1009 
1010   unsigned NumInstructions = ExtractedFunctionInsts.size();
1011   for (unsigned Idx = 0; Idx < NumInstructions; Idx++) {
1012     Value *V = ExtractedFunctionInsts[Idx];
1013 
1014     if (OutputMappings.find(V) != OutputMappings.end())
1015       V = OutputMappings.find(V)->second;
1016     Optional<unsigned> GVN = Region.Candidate->getGVN(V);
1017 
1018     // If we have found one of the stored values for output, replace the value
1019     // with the corresponding one from the overall function.
1020     if (GVN.hasValue() &&
1021         ValuesToFind.find(GVN.getValue()) != ValuesToFind.end()) {
1022       ValuesToFind.erase(GVN.getValue());
1023       V->replaceAllUsesWith(OverallFunctionInsts[Idx]);
1024       if (ValuesToFind.size() == 0)
1025         break;
1026     }
1027 
1028     if (ValuesToFind.size() == 0)
1029       break;
1030   }
1031 
1032   assert(ValuesToFind.size() == 0 && "Not all store values were handled!");
1033 
1034   // If the size of the block is 0, then there are no stores, and we do not
1035   // need to save this block.
1036   if (OutputBB->size() == 0) {
1037     Region.OutputBlockNum = -1;
1038     OutputBB->eraseFromParent();
1039     return;
1040   }
1041 
1042   // Determine is there is a duplicate block.
1043   Optional<unsigned> MatchingBB =
1044       findDuplicateOutputBlock(OutputBB, OutputStoreBBs);
1045 
1046   // If there is, we remove the new output block.  If it does not,
1047   // we add it to our list of output blocks.
1048   if (MatchingBB.hasValue()) {
1049     LLVM_DEBUG(dbgs() << "Set output block for region in function"
1050                       << Region.ExtractedFunction << " to "
1051                       << MatchingBB.getValue());
1052 
1053     Region.OutputBlockNum = MatchingBB.getValue();
1054     OutputBB->eraseFromParent();
1055     return;
1056   }
1057 
1058   Region.OutputBlockNum = OutputStoreBBs.size();
1059 
1060   LLVM_DEBUG(dbgs() << "Create output block for region in"
1061                     << Region.ExtractedFunction << " to "
1062                     << *OutputBB);
1063   OutputStoreBBs.push_back(OutputBB);
1064   BranchInst::Create(EndBB, OutputBB);
1065 }
1066 
1067 /// Create the switch statement for outlined function to differentiate between
1068 /// all the output blocks.
1069 ///
1070 /// For the outlined section, determine if an outlined block already exists that
1071 /// matches the needed stores for the extracted section.
1072 /// \param [in] M - The module we are outlining from.
1073 /// \param [in] OG - The group of regions to be outlined.
1074 /// \param [in] OS - The region that is being analyzed.
1075 /// \param [in] EndBB - The final block of the extracted function.
1076 /// \param [in,out] OutputStoreBBs - The existing output blocks.
1077 void createSwitchStatement(Module &M, OutlinableGroup &OG, BasicBlock *EndBB,
1078                            ArrayRef<BasicBlock *> OutputStoreBBs) {
1079   // We only need the switch statement if there is more than one store
1080   // combination.
1081   if (OG.OutputGVNCombinations.size() > 1) {
1082     Function *AggFunc = OG.OutlinedFunction;
1083     // Create a final block
1084     BasicBlock *ReturnBlock =
1085         BasicBlock::Create(M.getContext(), "final_block", AggFunc);
1086     Instruction *Term = EndBB->getTerminator();
1087     Term->moveBefore(*ReturnBlock, ReturnBlock->end());
1088     // Put the switch statement in the old end basic block for the function with
1089     // a fall through to the new return block
1090     LLVM_DEBUG(dbgs() << "Create switch statement in " << *AggFunc << " for "
1091                       << OutputStoreBBs.size() << "\n");
1092     SwitchInst *SwitchI =
1093         SwitchInst::Create(AggFunc->getArg(AggFunc->arg_size() - 1),
1094                            ReturnBlock, OutputStoreBBs.size(), EndBB);
1095 
1096     unsigned Idx = 0;
1097     for (BasicBlock *BB : OutputStoreBBs) {
1098       SwitchI->addCase(ConstantInt::get(Type::getInt32Ty(M.getContext()), Idx),
1099                        BB);
1100       Term = BB->getTerminator();
1101       Term->setSuccessor(0, ReturnBlock);
1102       Idx++;
1103     }
1104     return;
1105   }
1106 
1107   // If there needs to be stores, move them from the output block to the end
1108   // block to save on branching instructions.
1109   if (OutputStoreBBs.size() == 1) {
1110     LLVM_DEBUG(dbgs() << "Move store instructions to the end block in "
1111                       << *OG.OutlinedFunction << "\n");
1112     BasicBlock *OutputBlock = OutputStoreBBs[0];
1113     Instruction *Term = OutputBlock->getTerminator();
1114     Term->eraseFromParent();
1115     Term = EndBB->getTerminator();
1116     moveBBContents(*OutputBlock, *EndBB);
1117     Term->moveBefore(*EndBB, EndBB->end());
1118     OutputBlock->eraseFromParent();
1119   }
1120 
1121   return;
1122 }
1123 
1124 /// Fill the new function that will serve as the replacement function for all of
1125 /// the extracted regions of a certain structure from the first region in the
1126 /// list of regions.  Replace this first region's extracted function with the
1127 /// new overall function.
1128 ///
1129 /// \param [in] M - The module we are outlining from.
1130 /// \param [in] CurrentGroup - The group of regions to be outlined.
1131 /// \param [in,out] OutputStoreBBs - The output blocks for each different
1132 /// set of stores needed for the different functions.
1133 /// \param [in,out] FuncsToRemove - Extracted functions to erase from module
1134 /// once outlining is complete.
1135 static void fillOverallFunction(Module &M, OutlinableGroup &CurrentGroup,
1136                                 std::vector<BasicBlock *> &OutputStoreBBs,
1137                                 std::vector<Function *> &FuncsToRemove) {
1138   OutlinableRegion *CurrentOS = CurrentGroup.Regions[0];
1139 
1140   // Move first extracted function's instructions into new function.
1141   LLVM_DEBUG(dbgs() << "Move instructions from "
1142                     << *CurrentOS->ExtractedFunction << " to instruction "
1143                     << *CurrentGroup.OutlinedFunction << "\n");
1144 
1145   CurrentGroup.EndBB = moveFunctionData(*CurrentOS->ExtractedFunction,
1146                                         *CurrentGroup.OutlinedFunction);
1147 
1148   // Transfer the attributes from the function to the new function.
1149   for (Attribute A :
1150        CurrentOS->ExtractedFunction->getAttributes().getFnAttributes())
1151     CurrentGroup.OutlinedFunction->addFnAttr(A);
1152 
1153   // Create an output block for the first extracted function.
1154   BasicBlock *NewBB = BasicBlock::Create(
1155       M.getContext(), Twine("output_block_") + Twine(static_cast<unsigned>(0)),
1156       CurrentGroup.OutlinedFunction);
1157   CurrentOS->OutputBlockNum = 0;
1158 
1159   replaceArgumentUses(*CurrentOS, NewBB);
1160   replaceConstants(*CurrentOS);
1161 
1162   // If the new basic block has no new stores, we can erase it from the module.
1163   // It it does, we create a branch instruction to the last basic block from the
1164   // new one.
1165   if (NewBB->size() == 0) {
1166     CurrentOS->OutputBlockNum = -1;
1167     NewBB->eraseFromParent();
1168   } else {
1169     BranchInst::Create(CurrentGroup.EndBB, NewBB);
1170     OutputStoreBBs.push_back(NewBB);
1171   }
1172 
1173   // Replace the call to the extracted function with the outlined function.
1174   CurrentOS->Call = replaceCalledFunction(M, *CurrentOS);
1175 
1176   // We only delete the extracted functions at the end since we may need to
1177   // reference instructions contained in them for mapping purposes.
1178   FuncsToRemove.push_back(CurrentOS->ExtractedFunction);
1179 }
1180 
1181 void IROutliner::deduplicateExtractedSections(
1182     Module &M, OutlinableGroup &CurrentGroup,
1183     std::vector<Function *> &FuncsToRemove, unsigned &OutlinedFunctionNum) {
1184   createFunction(M, CurrentGroup, OutlinedFunctionNum);
1185 
1186   std::vector<BasicBlock *> OutputStoreBBs;
1187 
1188   OutlinableRegion *CurrentOS;
1189 
1190   fillOverallFunction(M, CurrentGroup, OutputStoreBBs, FuncsToRemove);
1191 
1192   for (unsigned Idx = 1; Idx < CurrentGroup.Regions.size(); Idx++) {
1193     CurrentOS = CurrentGroup.Regions[Idx];
1194 
1195     // Create a new BasicBlock to hold the needed store instructions.
1196     BasicBlock *NewBB = BasicBlock::Create(
1197         M.getContext(), "output_block_" + std::to_string(Idx),
1198         CurrentGroup.OutlinedFunction);
1199     replaceArgumentUses(*CurrentOS, NewBB);
1200 
1201     alignOutputBlockWithAggFunc(CurrentGroup, *CurrentOS, NewBB,
1202                                 CurrentGroup.EndBB, OutputMappings,
1203                                 OutputStoreBBs);
1204 
1205     CurrentOS->Call = replaceCalledFunction(M, *CurrentOS);
1206     FuncsToRemove.push_back(CurrentOS->ExtractedFunction);
1207   }
1208 
1209   // Create a switch statement to handle the different output schemes.
1210   createSwitchStatement(M, CurrentGroup, CurrentGroup.EndBB, OutputStoreBBs);
1211 
1212   OutlinedFunctionNum++;
1213 }
1214 
1215 void IROutliner::pruneIncompatibleRegions(
1216     std::vector<IRSimilarityCandidate> &CandidateVec,
1217     OutlinableGroup &CurrentGroup) {
1218   bool PreviouslyOutlined;
1219 
1220   // Sort from beginning to end, so the IRSimilarityCandidates are in order.
1221   stable_sort(CandidateVec, [](const IRSimilarityCandidate &LHS,
1222                                const IRSimilarityCandidate &RHS) {
1223     return LHS.getStartIdx() < RHS.getStartIdx();
1224   });
1225 
1226   unsigned CurrentEndIdx = 0;
1227   for (IRSimilarityCandidate &IRSC : CandidateVec) {
1228     PreviouslyOutlined = false;
1229     unsigned StartIdx = IRSC.getStartIdx();
1230     unsigned EndIdx = IRSC.getEndIdx();
1231 
1232     for (unsigned Idx = StartIdx; Idx <= EndIdx; Idx++)
1233       if (Outlined.contains(Idx)) {
1234         PreviouslyOutlined = true;
1235         break;
1236       }
1237 
1238     if (PreviouslyOutlined)
1239       continue;
1240 
1241     // TODO: If in the future we can outline across BasicBlocks, we will need to
1242     // check all BasicBlocks contained in the region.
1243     if (IRSC.getStartBB()->hasAddressTaken())
1244       continue;
1245 
1246     // Greedily prune out any regions that will overlap with already chosen
1247     // regions.
1248     if (CurrentEndIdx != 0 && StartIdx <= CurrentEndIdx)
1249       continue;
1250 
1251     bool BadInst = any_of(IRSC, [this](IRInstructionData &ID) {
1252       return !this->InstructionClassifier.visit(ID.Inst);
1253     });
1254 
1255     if (BadInst)
1256       continue;
1257 
1258     OutlinableRegion *OS = new (RegionAllocator.Allocate())
1259         OutlinableRegion(IRSC, CurrentGroup);
1260     CurrentGroup.Regions.push_back(OS);
1261 
1262     CurrentEndIdx = EndIdx;
1263   }
1264 }
1265 
1266 unsigned IROutliner::findBenefitFromAllRegions(OutlinableGroup &CurrentGroup) {
1267   unsigned RegionBenefit = 0;
1268   for (OutlinableRegion *Region : CurrentGroup.Regions) {
1269     TargetTransformInfo &TTI = getTTI(*Region->StartBB->getParent());
1270     // We add the number of instructions in the region to the benefit as an
1271     // estimate as to how much will be removed.
1272     RegionBenefit += Region->getBenefit(TTI);
1273     LLVM_DEBUG(dbgs() << "Adding: " << RegionBenefit
1274                       << " saved instructions to overfall benefit.\n");
1275     CurrentGroup.Benefit += RegionBenefit;
1276   }
1277 
1278   return RegionBenefit;
1279 }
1280 
1281 unsigned IROutliner::findCostOutputReloads(OutlinableGroup &CurrentGroup) {
1282   unsigned OverallCost = 0;
1283   for (OutlinableRegion *Region : CurrentGroup.Regions) {
1284     TargetTransformInfo &TTI = getTTI(*Region->StartBB->getParent());
1285 
1286     // Each output incurs a load after the call, so we add that to the cost.
1287     for (unsigned OutputGVN : Region->GVNStores) {
1288       Optional<Value *> OV = Region->Candidate->fromGVN(OutputGVN);
1289       assert(OV.hasValue() && "Could not find value for GVN?");
1290       Value *V = OV.getValue();
1291       unsigned LoadCost =
1292           TTI.getMemoryOpCost(Instruction::Load, V->getType(), Align(1), 0,
1293                               TargetTransformInfo::TCK_CodeSize);
1294 
1295       LLVM_DEBUG(dbgs() << "Adding: " << LoadCost
1296                         << " instructions to cost for output of type "
1297                         << *V->getType() << "\n");
1298       OverallCost += LoadCost;
1299     }
1300   }
1301 
1302   return OverallCost;
1303 }
1304 
1305 /// Find the extra instructions needed to handle any output values for the
1306 /// region.
1307 ///
1308 /// \param [in] M - The Module to outline from.
1309 /// \param [in] CurrentGroup - The collection of OutlinableRegions to analyze.
1310 /// \param [in] TTI - The TargetTransformInfo used to collect information for
1311 /// new instruction costs.
1312 /// \returns the additional cost to handle the outputs.
1313 static unsigned findCostForOutputBlocks(Module &M,
1314                                         OutlinableGroup &CurrentGroup,
1315                                         TargetTransformInfo &TTI) {
1316   unsigned OutputCost = 0;
1317 
1318   for (const ArrayRef<unsigned> &OutputUse :
1319        CurrentGroup.OutputGVNCombinations) {
1320     IRSimilarityCandidate &Candidate = *CurrentGroup.Regions[0]->Candidate;
1321     for (unsigned GVN : OutputUse) {
1322       Optional<Value *> OV = Candidate.fromGVN(GVN);
1323       assert(OV.hasValue() && "Could not find value for GVN?");
1324       Value *V = OV.getValue();
1325       unsigned StoreCost =
1326           TTI.getMemoryOpCost(Instruction::Load, V->getType(), Align(1), 0,
1327                               TargetTransformInfo::TCK_CodeSize);
1328 
1329       // An instruction cost is added for each store set that needs to occur for
1330       // various output combinations inside the function, plus a branch to
1331       // return to the exit block.
1332       LLVM_DEBUG(dbgs() << "Adding: " << StoreCost
1333                         << " instructions to cost for output of type "
1334                         << *V->getType() << "\n");
1335       OutputCost += StoreCost;
1336     }
1337 
1338     unsigned BranchCost =
1339         TTI.getCFInstrCost(Instruction::Br, TargetTransformInfo::TCK_CodeSize);
1340     LLVM_DEBUG(dbgs() << "Adding " << BranchCost << " to the current cost for"
1341                       << " a branch instruction\n");
1342     OutputCost += BranchCost;
1343   }
1344 
1345   // If there is more than one output scheme, we must have a comparison and
1346   // branch for each different item in the switch statement.
1347   if (CurrentGroup.OutputGVNCombinations.size() > 1) {
1348     unsigned ComparisonCost = TTI.getCmpSelInstrCost(
1349         Instruction::ICmp, Type::getInt32Ty(M.getContext()),
1350         Type::getInt32Ty(M.getContext()), CmpInst::BAD_ICMP_PREDICATE,
1351         TargetTransformInfo::TCK_CodeSize);
1352     unsigned BranchCost =
1353         TTI.getCFInstrCost(Instruction::Br, TargetTransformInfo::TCK_CodeSize);
1354 
1355     unsigned DifferentBlocks = CurrentGroup.OutputGVNCombinations.size();
1356     unsigned TotalCost = ComparisonCost * BranchCost * DifferentBlocks;
1357 
1358     LLVM_DEBUG(dbgs() << "Adding: " << TotalCost
1359                       << " instructions for each switch case for each different"
1360                       << " output path in a function\n");
1361     OutputCost += TotalCost;
1362   }
1363 
1364   return OutputCost;
1365 }
1366 
1367 void IROutliner::findCostBenefit(Module &M, OutlinableGroup &CurrentGroup) {
1368   unsigned RegionBenefit = findBenefitFromAllRegions(CurrentGroup);
1369   CurrentGroup.Benefit += RegionBenefit;
1370   LLVM_DEBUG(dbgs() << "Current Benefit: " << CurrentGroup.Benefit << "\n");
1371 
1372   unsigned OutputReloadCost = findCostOutputReloads(CurrentGroup);
1373   CurrentGroup.Cost += OutputReloadCost;
1374   LLVM_DEBUG(dbgs() << "Current Cost: " << CurrentGroup.Cost << "\n");
1375 
1376   unsigned AverageRegionBenefit = RegionBenefit / CurrentGroup.Regions.size();
1377   unsigned OverallArgumentNum = CurrentGroup.ArgumentTypes.size();
1378   unsigned NumRegions = CurrentGroup.Regions.size();
1379   TargetTransformInfo &TTI =
1380       getTTI(*CurrentGroup.Regions[0]->Candidate->getFunction());
1381 
1382   // We add one region to the cost once, to account for the instructions added
1383   // inside of the newly created function.
1384   LLVM_DEBUG(dbgs() << "Adding: " << AverageRegionBenefit
1385                     << " instructions to cost for body of new function.\n");
1386   CurrentGroup.Cost += AverageRegionBenefit;
1387   LLVM_DEBUG(dbgs() << "Current Cost: " << CurrentGroup.Cost << "\n");
1388 
1389   // For each argument, we must add an instruction for loading the argument
1390   // out of the register and into a value inside of the newly outlined function.
1391   LLVM_DEBUG(dbgs() << "Adding: " << OverallArgumentNum
1392                     << " instructions to cost for each argument in the new"
1393                     << " function.\n");
1394   CurrentGroup.Cost += 2 * OverallArgumentNum * TargetTransformInfo::TCC_Basic;
1395   LLVM_DEBUG(dbgs() << "Current Cost: " << CurrentGroup.Cost << "\n");
1396 
1397   // Each argument needs to either be loaded into a register or onto the stack.
1398   // Some arguments will only be loaded into the stack once the argument
1399   // registers are filled.
1400   LLVM_DEBUG(dbgs() << "Adding: " << OverallArgumentNum
1401                     << " instructions to cost for each argument in the new"
1402                     << " function " << NumRegions << " times for the "
1403                     << "needed argument handling at the call site.\n");
1404   CurrentGroup.Cost +=
1405       2 * OverallArgumentNum * TargetTransformInfo::TCC_Basic * NumRegions;
1406   LLVM_DEBUG(dbgs() << "Current Cost: " << CurrentGroup.Cost << "\n");
1407 
1408   CurrentGroup.Cost += findCostForOutputBlocks(M, CurrentGroup, TTI);
1409   LLVM_DEBUG(dbgs() << "Current Cost: " << CurrentGroup.Cost << "\n");
1410 }
1411 
1412 void IROutliner::updateOutputMapping(OutlinableRegion &Region,
1413                                      ArrayRef<Value *> Outputs,
1414                                      LoadInst *LI) {
1415   // For and load instructions following the call
1416   Value *Operand = LI->getPointerOperand();
1417   Optional<unsigned> OutputIdx = None;
1418   // Find if the operand it is an output register.
1419   for (unsigned ArgIdx = Region.NumExtractedInputs;
1420        ArgIdx < Region.Call->arg_size(); ArgIdx++) {
1421     if (Operand == Region.Call->getArgOperand(ArgIdx)) {
1422       OutputIdx = ArgIdx - Region.NumExtractedInputs;
1423       break;
1424     }
1425   }
1426 
1427   // If we found an output register, place a mapping of the new value
1428   // to the original in the mapping.
1429   if (!OutputIdx.hasValue())
1430     return;
1431 
1432   if (OutputMappings.find(Outputs[OutputIdx.getValue()]) ==
1433       OutputMappings.end()) {
1434     LLVM_DEBUG(dbgs() << "Mapping extracted output " << *LI << " to "
1435                       << *Outputs[OutputIdx.getValue()] << "\n");
1436     OutputMappings.insert(std::make_pair(LI, Outputs[OutputIdx.getValue()]));
1437   } else {
1438     Value *Orig = OutputMappings.find(Outputs[OutputIdx.getValue()])->second;
1439     LLVM_DEBUG(dbgs() << "Mapping extracted output " << *Orig << " to "
1440                       << *Outputs[OutputIdx.getValue()] << "\n");
1441     OutputMappings.insert(std::make_pair(LI, Orig));
1442   }
1443 }
1444 
1445 bool IROutliner::extractSection(OutlinableRegion &Region) {
1446   SetVector<Value *> ArgInputs, Outputs, SinkCands;
1447   Region.CE->findInputsOutputs(ArgInputs, Outputs, SinkCands);
1448 
1449   assert(Region.StartBB && "StartBB for the OutlinableRegion is nullptr!");
1450   assert(Region.FollowBB && "FollowBB for the OutlinableRegion is nullptr!");
1451   Function *OrigF = Region.StartBB->getParent();
1452   CodeExtractorAnalysisCache CEAC(*OrigF);
1453   Region.ExtractedFunction = Region.CE->extractCodeRegion(CEAC);
1454 
1455   // If the extraction was successful, find the BasicBlock, and reassign the
1456   // OutlinableRegion blocks
1457   if (!Region.ExtractedFunction) {
1458     LLVM_DEBUG(dbgs() << "CodeExtractor failed to outline " << Region.StartBB
1459                       << "\n");
1460     Region.reattachCandidate();
1461     return false;
1462   }
1463 
1464   BasicBlock *RewrittenBB = Region.FollowBB->getSinglePredecessor();
1465   Region.StartBB = RewrittenBB;
1466   Region.EndBB = RewrittenBB;
1467 
1468   // The sequences of outlinable regions has now changed.  We must fix the
1469   // IRInstructionDataList for consistency.  Although they may not be illegal
1470   // instructions, they should not be compared with anything else as they
1471   // should not be outlined in this round.  So marking these as illegal is
1472   // allowed.
1473   IRInstructionDataList *IDL = Region.Candidate->front()->IDL;
1474   Instruction *BeginRewritten = &*RewrittenBB->begin();
1475   Instruction *EndRewritten = &*RewrittenBB->begin();
1476   Region.NewFront = new (InstDataAllocator.Allocate()) IRInstructionData(
1477       *BeginRewritten, InstructionClassifier.visit(*BeginRewritten), *IDL);
1478   Region.NewBack = new (InstDataAllocator.Allocate()) IRInstructionData(
1479       *EndRewritten, InstructionClassifier.visit(*EndRewritten), *IDL);
1480 
1481   // Insert the first IRInstructionData of the new region in front of the
1482   // first IRInstructionData of the IRSimilarityCandidate.
1483   IDL->insert(Region.Candidate->begin(), *Region.NewFront);
1484   // Insert the first IRInstructionData of the new region after the
1485   // last IRInstructionData of the IRSimilarityCandidate.
1486   IDL->insert(Region.Candidate->end(), *Region.NewBack);
1487   // Remove the IRInstructionData from the IRSimilarityCandidate.
1488   IDL->erase(Region.Candidate->begin(), std::prev(Region.Candidate->end()));
1489 
1490   assert(RewrittenBB != nullptr &&
1491          "Could not find a predecessor after extraction!");
1492 
1493   // Iterate over the new set of instructions to find the new call
1494   // instruction.
1495   for (Instruction &I : *RewrittenBB)
1496     if (CallInst *CI = dyn_cast<CallInst>(&I)) {
1497       if (Region.ExtractedFunction == CI->getCalledFunction())
1498         Region.Call = CI;
1499     } else if (LoadInst *LI = dyn_cast<LoadInst>(&I))
1500       updateOutputMapping(Region, Outputs.getArrayRef(), LI);
1501   Region.reattachCandidate();
1502   return true;
1503 }
1504 
1505 unsigned IROutliner::doOutline(Module &M) {
1506   // Find the possible similarity sections.
1507   IRSimilarityIdentifier &Identifier = getIRSI(M);
1508   SimilarityGroupList &SimilarityCandidates = *Identifier.getSimilarity();
1509 
1510   // Sort them by size of extracted sections
1511   unsigned OutlinedFunctionNum = 0;
1512   // If we only have one SimilarityGroup in SimilarityCandidates, we do not have
1513   // to sort them by the potential number of instructions to be outlined
1514   if (SimilarityCandidates.size() > 1)
1515     llvm::stable_sort(SimilarityCandidates,
1516                       [](const std::vector<IRSimilarityCandidate> &LHS,
1517                          const std::vector<IRSimilarityCandidate> &RHS) {
1518                         return LHS[0].getLength() * LHS.size() >
1519                                RHS[0].getLength() * RHS.size();
1520                       });
1521 
1522   DenseSet<unsigned> NotSame;
1523   std::vector<Function *> FuncsToRemove;
1524   // Iterate over the possible sets of similarity.
1525   for (SimilarityGroup &CandidateVec : SimilarityCandidates) {
1526     OutlinableGroup CurrentGroup;
1527 
1528     // Remove entries that were previously outlined
1529     pruneIncompatibleRegions(CandidateVec, CurrentGroup);
1530 
1531     // We pruned the number of regions to 0 to 1, meaning that it's not worth
1532     // trying to outlined since there is no compatible similar instance of this
1533     // code.
1534     if (CurrentGroup.Regions.size() < 2)
1535       continue;
1536 
1537     // Determine if there are any values that are the same constant throughout
1538     // each section in the set.
1539     NotSame.clear();
1540     CurrentGroup.findSameConstants(NotSame);
1541 
1542     if (CurrentGroup.IgnoreGroup)
1543       continue;
1544 
1545     // Create a CodeExtractor for each outlinable region. Identify inputs and
1546     // outputs for each section using the code extractor and create the argument
1547     // types for the Aggregate Outlining Function.
1548     std::vector<OutlinableRegion *> OutlinedRegions;
1549     for (OutlinableRegion *OS : CurrentGroup.Regions) {
1550       // Break the outlinable region out of its parent BasicBlock into its own
1551       // BasicBlocks (see function implementation).
1552       OS->splitCandidate();
1553       std::vector<BasicBlock *> BE = {OS->StartBB};
1554       OS->CE = new (ExtractorAllocator.Allocate())
1555           CodeExtractor(BE, nullptr, false, nullptr, nullptr, nullptr, false,
1556                         false, "outlined");
1557       findAddInputsOutputs(M, *OS, NotSame);
1558       if (!OS->IgnoreRegion)
1559         OutlinedRegions.push_back(OS);
1560       else
1561         OS->reattachCandidate();
1562     }
1563 
1564     CurrentGroup.Regions = std::move(OutlinedRegions);
1565 
1566     if (CurrentGroup.Regions.empty())
1567       continue;
1568 
1569     CurrentGroup.collectGVNStoreSets(M);
1570 
1571     if (CostModel)
1572       findCostBenefit(M, CurrentGroup);
1573 
1574     // If we are adhering to the cost model, reattach all the candidates
1575     if (CurrentGroup.Cost >= CurrentGroup.Benefit && CostModel) {
1576       for (OutlinableRegion *OS : CurrentGroup.Regions)
1577         OS->reattachCandidate();
1578       OptimizationRemarkEmitter &ORE = getORE(
1579           *CurrentGroup.Regions[0]->Candidate->getFunction());
1580       ORE.emit([&]() {
1581         IRSimilarityCandidate *C = CurrentGroup.Regions[0]->Candidate;
1582         OptimizationRemarkMissed R(DEBUG_TYPE, "WouldNotDecreaseSize",
1583                                    C->frontInstruction());
1584         R << "did not outline "
1585           << ore::NV(std::to_string(CurrentGroup.Regions.size()))
1586           << " regions due to estimated increase of "
1587           << ore::NV("InstructionIncrease",
1588                      std::to_string(static_cast<int>(CurrentGroup.Cost -
1589                                                      CurrentGroup.Benefit)))
1590           << " instructions at locations ";
1591         interleave(
1592             CurrentGroup.Regions.begin(), CurrentGroup.Regions.end(),
1593             [&R](OutlinableRegion *Region) {
1594               R << ore::NV(
1595                   "DebugLoc",
1596                   Region->Candidate->frontInstruction()->getDebugLoc());
1597             },
1598             [&R]() { R << " "; });
1599         return R;
1600       });
1601       continue;
1602     }
1603 
1604     LLVM_DEBUG(dbgs() << "Outlining regions with cost " << CurrentGroup.Cost
1605                       << " and benefit " << CurrentGroup.Benefit << "\n");
1606 
1607     // Create functions out of all the sections, and mark them as outlined.
1608     OutlinedRegions.clear();
1609     for (OutlinableRegion *OS : CurrentGroup.Regions) {
1610       bool FunctionOutlined = extractSection(*OS);
1611       if (FunctionOutlined) {
1612         unsigned StartIdx = OS->Candidate->getStartIdx();
1613         unsigned EndIdx = OS->Candidate->getEndIdx();
1614         for (unsigned Idx = StartIdx; Idx <= EndIdx; Idx++)
1615           Outlined.insert(Idx);
1616 
1617         OutlinedRegions.push_back(OS);
1618       }
1619     }
1620 
1621     LLVM_DEBUG(dbgs() << "Outlined " << OutlinedRegions.size()
1622                       << " with benefit " << CurrentGroup.Benefit
1623                       << " and cost " << CurrentGroup.Cost << "\n");
1624 
1625     CurrentGroup.Regions = std::move(OutlinedRegions);
1626 
1627     if (CurrentGroup.Regions.empty())
1628       continue;
1629 
1630     OptimizationRemarkEmitter &ORE =
1631         getORE(*CurrentGroup.Regions[0]->Call->getFunction());
1632     ORE.emit([&]() {
1633       IRSimilarityCandidate *C = CurrentGroup.Regions[0]->Candidate;
1634       OptimizationRemark R(DEBUG_TYPE, "Outlined", C->front()->Inst);
1635       R << "outlined " << ore::NV(std::to_string(CurrentGroup.Regions.size()))
1636         << " regions with decrease of "
1637         << ore::NV("Benefit", std::to_string(static_cast<int>(
1638                                   CurrentGroup.Benefit - CurrentGroup.Cost)))
1639         << " instructions at locations ";
1640       interleave(
1641           CurrentGroup.Regions.begin(), CurrentGroup.Regions.end(),
1642           [&R](OutlinableRegion *Region) {
1643             R << ore::NV("DebugLoc",
1644                          Region->Candidate->frontInstruction()->getDebugLoc());
1645           },
1646           [&R]() { R << " "; });
1647       return R;
1648     });
1649 
1650     deduplicateExtractedSections(M, CurrentGroup, FuncsToRemove,
1651                                  OutlinedFunctionNum);
1652   }
1653 
1654   for (Function *F : FuncsToRemove)
1655     F->eraseFromParent();
1656 
1657   return OutlinedFunctionNum;
1658 }
1659 
1660 bool IROutliner::run(Module &M) {
1661   CostModel = !NoCostModel;
1662 
1663   return doOutline(M) > 0;
1664 }
1665 
1666 // Pass Manager Boilerplate
1667 class IROutlinerLegacyPass : public ModulePass {
1668 public:
1669   static char ID;
1670   IROutlinerLegacyPass() : ModulePass(ID) {
1671     initializeIROutlinerLegacyPassPass(*PassRegistry::getPassRegistry());
1672   }
1673 
1674   void getAnalysisUsage(AnalysisUsage &AU) const override {
1675     AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
1676     AU.addRequired<TargetTransformInfoWrapperPass>();
1677     AU.addRequired<IRSimilarityIdentifierWrapperPass>();
1678   }
1679 
1680   bool runOnModule(Module &M) override;
1681 };
1682 
1683 bool IROutlinerLegacyPass::runOnModule(Module &M) {
1684   if (skipModule(M))
1685     return false;
1686 
1687   std::unique_ptr<OptimizationRemarkEmitter> ORE;
1688   auto GORE = [&ORE](Function &F) -> OptimizationRemarkEmitter & {
1689     ORE.reset(new OptimizationRemarkEmitter(&F));
1690     return *ORE.get();
1691   };
1692 
1693   auto GTTI = [this](Function &F) -> TargetTransformInfo & {
1694     return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
1695   };
1696 
1697   auto GIRSI = [this](Module &) -> IRSimilarityIdentifier & {
1698     return this->getAnalysis<IRSimilarityIdentifierWrapperPass>().getIRSI();
1699   };
1700 
1701   return IROutliner(GTTI, GIRSI, GORE).run(M);
1702 }
1703 
1704 PreservedAnalyses IROutlinerPass::run(Module &M, ModuleAnalysisManager &AM) {
1705   auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
1706 
1707   std::function<TargetTransformInfo &(Function &)> GTTI =
1708       [&FAM](Function &F) -> TargetTransformInfo & {
1709     return FAM.getResult<TargetIRAnalysis>(F);
1710   };
1711 
1712   std::function<IRSimilarityIdentifier &(Module &)> GIRSI =
1713       [&AM](Module &M) -> IRSimilarityIdentifier & {
1714     return AM.getResult<IRSimilarityAnalysis>(M);
1715   };
1716 
1717   std::unique_ptr<OptimizationRemarkEmitter> ORE;
1718   std::function<OptimizationRemarkEmitter &(Function &)> GORE =
1719       [&ORE](Function &F) -> OptimizationRemarkEmitter & {
1720     ORE.reset(new OptimizationRemarkEmitter(&F));
1721     return *ORE.get();
1722   };
1723 
1724   if (IROutliner(GTTI, GIRSI, GORE).run(M))
1725     return PreservedAnalyses::none();
1726   return PreservedAnalyses::all();
1727 }
1728 
1729 char IROutlinerLegacyPass::ID = 0;
1730 INITIALIZE_PASS_BEGIN(IROutlinerLegacyPass, "iroutliner", "IR Outliner", false,
1731                       false)
1732 INITIALIZE_PASS_DEPENDENCY(IRSimilarityIdentifierWrapperPass)
1733 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
1734 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
1735 INITIALIZE_PASS_END(IROutlinerLegacyPass, "iroutliner", "IR Outliner", false,
1736                     false)
1737 
1738 ModulePass *llvm::createIROutlinerPass() { return new IROutlinerLegacyPass(); }
1739