1c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola//===- PassManagerBuilder.cpp - Build Standard Pass -----------------------===//
2c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola//
3c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola//                     The LLVM Compiler Infrastructure
4c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola//
5c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// This file is distributed under the University of Illinois Open Source
6c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// License. See LICENSE.TXT for details.
7c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola//
8c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola//===----------------------------------------------------------------------===//
9c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola//
10c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// This file defines the PassManagerBuilder class, which is used to set up a
11c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola// "standard" optimization sequence suitable for languages like C and C++.
12c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola//
13c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola//===----------------------------------------------------------------------===//
14c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
15c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
16c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/Transforms/IPO/PassManagerBuilder.h"
1769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola#include "llvm-c/Transforms/PassManagerBuilder.h"
18d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/SmallVector.h"
19c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/Analysis/Passes.h"
2037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/IR/DataLayout.h"
2136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/Verifier.h"
22ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/IR/LegacyPassManager.h"
23de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel#include "llvm/Support/CommandLine.h"
24d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/ManagedStatic.h"
25ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/Analysis/TargetLibraryInfo.h"
2637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/Target/TargetMachine.h"
27d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Transforms/IPO.h"
28c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola#include "llvm/Transforms/Scalar.h"
29de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel#include "llvm/Transforms/Vectorize.h"
30c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
31c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolausing namespace llvm;
32c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
33de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkelstatic cl::opt<bool>
34fe16848601bdde6e3a5e0860199169dd171222a4Nadav RotemRunLoopVectorization("vectorize-loops", cl::Hidden,
359210cc3a173224b40d9fc45b202f52cc7acf5c5aNadav Rotem                     cl::desc("Run the Loop vectorization passes"));
36d233b78760e481a9602266f8376eb1189c58bc9aNadav Rotem
37d233b78760e481a9602266f8376eb1189c58bc9aNadav Rotemstatic cl::opt<bool>
38fe16848601bdde6e3a5e0860199169dd171222a4Nadav RotemRunSLPVectorization("vectorize-slp", cl::Hidden,
391129a832e6ef5291b0144e9f22c76b2599cd5587Nadav Rotem                    cl::desc("Run the SLP vectorization passes"));
401129a832e6ef5291b0144e9f22c76b2599cd5587Nadav Rotem
411129a832e6ef5291b0144e9f22c76b2599cd5587Nadav Rotemstatic cl::opt<bool>
42fe16848601bdde6e3a5e0860199169dd171222a4Nadav RotemRunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
431129a832e6ef5291b0144e9f22c76b2599cd5587Nadav Rotem                    cl::desc("Run the BB vectorization passes"));
44de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel
45064551e94c9763c7e74121b88f9cbccc0969946aHal Finkelstatic cl::opt<bool>
46064551e94c9763c7e74121b88f9cbccc0969946aHal FinkelUseGVNAfterVectorization("use-gvn-after-vectorization",
47064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel  cl::init(false), cl::Hidden,
48064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel  cl::desc("Run GVN instead of Early CSE after vectorization passes"));
49064551e94c9763c7e74121b88f9cbccc0969946aHal Finkel
5037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstatic cl::opt<bool> ExtraVectorizerPasses(
5137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    "extra-vectorizer-passes", cl::init(false), cl::Hidden,
5237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    cl::desc("Run cleanup optimization passes after vectorization."));
5337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
54713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruthstatic cl::opt<bool> UseNewSROA("use-new-sroa",
551fe4fae463eba5965aa1f98600161ef7de96aaf1Chandler Carruth  cl::init(true), cl::Hidden,
56713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth  cl::desc("Enable the new, experimental SROA pass"));
57713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth
58bebe48dbfe00078329341945bfb11f778ace6d12Hal Finkelstatic cl::opt<bool>
59bebe48dbfe00078329341945bfb11f778ace6d12Hal FinkelRunLoopRerolling("reroll-loops", cl::Hidden,
60bebe48dbfe00078329341945bfb11f778ace6d12Hal Finkel                 cl::desc("Run the loop rerolling pass"));
61bebe48dbfe00078329341945bfb11f778ace6d12Hal Finkel
622c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainarstatic cl::opt<bool>
632c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga NainarRunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
642c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar             cl::desc("Run the float2int (float demotion) pass"));
652c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar
66c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesstatic cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
67c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                                    cl::Hidden,
68c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines                                    cl::desc("Run the load combining pass"));
69c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
7037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstatic cl::opt<bool>
7137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesRunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
7237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  cl::init(true), cl::Hidden,
7337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
7437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines           "vectorizer instead of before"));
7537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
7637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstatic cl::opt<bool> UseCFLAA("use-cfl-aa",
7737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  cl::init(false), cl::Hidden,
7837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  cl::desc("Enable the new, experimental CFL alias analysis"));
7937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
8037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstatic cl::opt<bool>
8137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesEnableMLSM("mlsm", cl::init(true), cl::Hidden,
8237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines           cl::desc("Enable motion of merged load and store"));
8337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
844c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainarstatic cl::opt<bool> EnableLoopInterchange(
854c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    "enable-loopinterchange", cl::init(false), cl::Hidden,
864c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    cl::desc("Enable the new, experimental LoopInterchange Pass"));
874c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar
88c684e83e4554bfaf6180426f86b9253b65dd46ecRafael EspindolaPassManagerBuilder::PassManagerBuilder() {
89c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    OptLevel = 2;
90c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    SizeLevel = 0;
91dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    LibraryInfo = nullptr;
92dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Inliner = nullptr;
93dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    DisableTailCalls = false;
94c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    DisableUnitAtATime = false;
95c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    DisableUnrollLoops = false;
961129a832e6ef5291b0144e9f22c76b2599cd5587Nadav Rotem    BBVectorize = RunBBVectorization;
978849838965941919257b923f8603ca1bacd3fa46Nadav Rotem    SLPVectorize = RunSLPVectorization;
98d233b78760e481a9602266f8376eb1189c58bc9aNadav Rotem    LoopVectorize = RunLoopVectorization;
99c8dc96be28fd1a3d6ddebbb48b8d55b61e4bd89bHal Finkel    RerollLoops = RunLoopRerolling;
100c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines    LoadCombine = RunLoadCombine;
10137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    DisableGVNLoadPRE = false;
10237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    VerifyInput = false;
10337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    VerifyOutput = false;
10437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    MergeFunctions = false;
105c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola}
106c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
107c684e83e4554bfaf6180426f86b9253b65dd46ecRafael EspindolaPassManagerBuilder::~PassManagerBuilder() {
108c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  delete LibraryInfo;
109c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  delete Inliner;
110c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola}
111c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
1127a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall/// Set of global extensions, automatically added as part of the standard set.
1137a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnallstatic ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
1147a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall   PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
1157a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall
1167a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnallvoid PassManagerBuilder::addGlobalExtension(
1177a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall    PassManagerBuilder::ExtensionPointTy Ty,
1187a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall    PassManagerBuilder::ExtensionFn Fn) {
1197a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall  GlobalExtensions->push_back(std::make_pair(Ty, Fn));
1207a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall}
1217a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall
122c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
123c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  Extensions.push_back(std::make_pair(Ty, Fn));
124c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola}
125c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
126c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindolavoid PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
127ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                                           legacy::PassManagerBase &PM) const {
1287a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall  for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
1297a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall    if ((*GlobalExtensions)[i].first == ETy)
1307a817eae6cb00a2ba5ec2a4127775b839a357e4cDavid Chisnall      (*GlobalExtensions)[i].second(*this, PM);
131c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
132c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    if (Extensions[i].first == ETy)
133c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola      Extensions[i].second(*this, PM);
134c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola}
135c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
136ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesvoid PassManagerBuilder::addInitialAliasAnalysisPasses(
137ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    legacy::PassManagerBase &PM) const {
138c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
139c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // BasicAliasAnalysis wins if they disagree. This is intended to help
140c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // support "obvious" type-punning idioms.
14137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (UseCFLAA)
14237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    PM.add(createCFLAliasAnalysisPass());
143c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createTypeBasedAliasAnalysisPass());
14437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  PM.add(createScopedNoAliasAAPass());
145c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createBasicAliasAnalysisPass());
146c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola}
147c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
148ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesvoid PassManagerBuilder::populateFunctionPassManager(
149ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    legacy::FunctionPassManager &FPM) {
150c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  addExtensionsToPM(EP_EarlyAsPossible, FPM);
151c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
152c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Add LibraryInfo if we have some.
153ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  if (LibraryInfo)
154ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
155c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
156c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  if (OptLevel == 0) return;
157c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
158c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  addInitialAliasAnalysisPasses(FPM);
159c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
160c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  FPM.add(createCFGSimplificationPass());
161713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth  if (UseNewSROA)
162713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth    FPM.add(createSROAPass());
163713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth  else
164713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth    FPM.add(createScalarReplAggregatesPass());
165c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  FPM.add(createEarlyCSEPass());
166c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  FPM.add(createLowerExpectIntrinsicPass());
167c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola}
168c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
169ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesvoid PassManagerBuilder::populateModulePassManager(
170ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    legacy::PassManagerBase &MPM) {
17137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // If all optimizations are disabled, just run the always-inline pass and,
17237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // if enabled, the function merging pass.
173c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  if (OptLevel == 0) {
174c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    if (Inliner) {
175c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola      MPM.add(Inliner);
176dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      Inliner = nullptr;
177c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    }
17863a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth
17937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
18037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // creates a CGSCC pass manager, but we don't want to add extensions into
18137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // that pass manager. To prevent this we insert a no-op module pass to reset
18237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
18337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // builds. The function merging pass is
18437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (MergeFunctions)
18537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      MPM.add(createMergeFunctionsPass());
18637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    else if (!GlobalExtensions->empty() || !Extensions.empty())
18763a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth      MPM.add(createBarrierNoopPass());
18863a1eb62e4eef1cbdedce5c60c3e1243a071eba1Chandler Carruth
189af65a8c54b66e9a78e2c7a9a810a33a73fb7f63aKostya Serebryany    addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
190c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    return;
191c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  }
192c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
193c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Add LibraryInfo if we have some.
194ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  if (LibraryInfo)
195ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
196c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
197c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  addInitialAliasAnalysisPasses(MPM);
198c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
199c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  if (!DisableUnitAtATime) {
2007d4c87ef6eea424b7a28392ea11137ed77b44b57Dan Gohman    addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
2017d4c87ef6eea424b7a28392ea11137ed77b44b57Dan Gohman
202c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines    MPM.add(createIPSCCPPass());              // IP SCCP
203c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    MPM.add(createGlobalOptimizerPass());     // Optimize out global vars
204c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
205c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    MPM.add(createDeadArgEliminationPass());  // Dead argument elimination
206c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
207c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
208dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    addExtensionsToPM(EP_Peephole, MPM);
209c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    MPM.add(createCFGSimplificationPass());   // Clean up after IPCP & DAE
210c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  }
211c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
212c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Start of CallGraph SCC passes.
213c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  if (!DisableUnitAtATime)
214c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    MPM.add(createPruneEHPass());             // Remove dead EH info
215c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  if (Inliner) {
216c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    MPM.add(Inliner);
217dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Inliner = nullptr;
218c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  }
219c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  if (!DisableUnitAtATime)
220c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    MPM.add(createFunctionAttrsPass());       // Set readonly/readnone attrs
221c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  if (OptLevel > 2)
222c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    MPM.add(createArgumentPromotionPass());   // Scalarize uninlined fn args
223c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
224c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Start of function pass.
225c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Break up aggregate allocas, using SSAUpdater.
2261c8db50a9aca4229ebee75ed9cfbf8b222292787Chandler Carruth  if (UseNewSROA)
2271c8db50a9aca4229ebee75ed9cfbf8b222292787Chandler Carruth    MPM.add(createSROAPass(/*RequiresDomTree*/ false));
2281c8db50a9aca4229ebee75ed9cfbf8b222292787Chandler Carruth  else
2291c8db50a9aca4229ebee75ed9cfbf8b222292787Chandler Carruth    MPM.add(createScalarReplAggregatesPass(-1, false));
230c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createEarlyCSEPass());              // Catch trivial redundancies
231c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createJumpThreadingPass());         // Thread jumps.
232c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
233c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
234c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createInstructionCombiningPass());  // Combine silly seq's
235dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  addExtensionsToPM(EP_Peephole, MPM);
236c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
237dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (!DisableTailCalls)
238dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
239c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
240c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createReassociatePass());           // Reassociate expressions
241ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  // Rotate Loop - disable header duplication at -Oz
242ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
243c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createLICMPass());                  // Hoist loop invariants
244c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
245c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createInstructionCombiningPass());
246c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createIndVarSimplifyPass());        // Canonicalize indvars
247c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createLoopIdiomPass());             // Recognize idioms like memset.
248c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createLoopDeletionPass());          // Delete dead loops
2494c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  if (EnableLoopInterchange)
2504c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    MPM.add(createLoopInterchangePass()); // Interchange loops
251d15c0c7ac118cb23241b002e7206221283e36e2dNadav Rotem
252c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  if (!DisableUnrollLoops)
25336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    MPM.add(createSimpleLoopUnrollPass());    // Unroll small loops
254c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
255c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
25637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (OptLevel > 1) {
25737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (EnableMLSM)
25837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
25937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    MPM.add(createGVNPass(DisableGVNLoadPRE));  // Remove redundancies
26037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
261c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createMemCpyOptPass());             // Remove memcpy / form memset
262c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createSCCPPass());                  // Constant prop with SCCP
263c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
264ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  // Delete dead bit computations (instcombine runs after to fold away the dead
265ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  // computations, and then ADCE will run later to exploit any new DCE
266ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  // opportunities that creates).
267ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  MPM.add(createBitTrackingDCEPass());        // Delete dead bit computations
268ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
269c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Run instcombine after redundancy elimination to exploit opportunities
270c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // opened up by them.
271c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createInstructionCombiningPass());
272dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  addExtensionsToPM(EP_Peephole, MPM);
273c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createJumpThreadingPass());         // Thread jumps
274c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createCorrelatedValuePropagationPass());
275c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createDeadStoreEliminationPass());  // Delete dead stores
276ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  MPM.add(createLICMPass());
277c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
278c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
279c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
280c8dc96be28fd1a3d6ddebbb48b8d55b61e4bd89bHal Finkel  if (RerollLoops)
281bebe48dbfe00078329341945bfb11f778ace6d12Hal Finkel    MPM.add(createLoopRerollPass());
28237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (!RunSLPAfterLoopVectorization) {
28337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (SLPVectorize)
28437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      MPM.add(createSLPVectorizerPass());   // Vectorize parallel scalar chains.
28537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
28637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (BBVectorize) {
28737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      MPM.add(createBBVectorizePass());
28837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      MPM.add(createInstructionCombiningPass());
28937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      addExtensionsToPM(EP_Peephole, MPM);
29037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      if (OptLevel > 1 && UseGVNAfterVectorization)
29137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
29237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      else
29337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        MPM.add(createEarlyCSEPass());      // Catch trivial redundancies
29437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
29537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      // BBVectorize may have significantly shortened a loop body; unroll again.
29637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      if (!DisableUnrollLoops)
29737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        MPM.add(createLoopUnrollPass());
29837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    }
299de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel  }
300de5e5ec3045a73a06b1054417f9ac6c02929e9ceHal Finkel
301c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  if (LoadCombine)
302c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines    MPM.add(createLoadCombinePass());
303c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
304c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createAggressiveDCEPass());         // Delete dead instructions
30501d7203ef8316fdd71c3cec59f8e68fb869e0dbfTom Stellard  MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
306c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  MPM.add(createInstructionCombiningPass());  // Clean up after everything.
307dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  addExtensionsToPM(EP_Peephole, MPM);
308c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
30936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
31036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // pass manager that we are specifically trying to avoid. To prevent this
31136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // we must insert a no-op module pass to reset the pass manager.
31236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  MPM.add(createBarrierNoopPass());
31337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
3142c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar  if (RunFloat2Int)
3152c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar    MPM.add(createFloat2IntPass());
3162c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar
31737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // Re-rotate loops in all our loop nests. These may have fallout out of
31837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // rotated form due to GVN or other transformations, and the vectorizer relies
31937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // on the rotated form.
3204c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  MPM.add(createLoopRotatePass());
32137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
32236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
32336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // FIXME: Because of #pragma vectorize enable, the passes below are always
32436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
32536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // on -O1 and no #pragma is found). Would be good to have these two passes
32636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // as function calls, so that we can only pass them when the vectorizer
32736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // changed the code.
32836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  MPM.add(createInstructionCombiningPass());
32937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (OptLevel > 1 && ExtraVectorizerPasses) {
33037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // At higher optimization levels, try to clean up any runtime overlap and
33137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // alignment checks inserted by the vectorizer. We want to track correllated
33237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // runtime checks for two inner loops in the same outer loop, fold any
33337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // common computations, hoist loop-invariant aspects out of any outer loop,
33437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // and unswitch the runtime checks if possible. Once hoisted, we may have
33537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // dead (or speculatable) control flows or more combining opportunities.
33637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    MPM.add(createEarlyCSEPass());
33737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    MPM.add(createCorrelatedValuePropagationPass());
33837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    MPM.add(createInstructionCombiningPass());
33937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    MPM.add(createLICMPass());
34037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
34137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    MPM.add(createCFGSimplificationPass());
34237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    MPM.add(createInstructionCombiningPass());
34337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
34437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
34537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (RunSLPAfterLoopVectorization) {
34637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (SLPVectorize) {
34737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      MPM.add(createSLPVectorizerPass());   // Vectorize parallel scalar chains.
34837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      if (OptLevel > 1 && ExtraVectorizerPasses) {
34937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        MPM.add(createEarlyCSEPass());
35037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      }
35137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    }
35237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
35337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (BBVectorize) {
35437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      MPM.add(createBBVectorizePass());
35537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      MPM.add(createInstructionCombiningPass());
35637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      addExtensionsToPM(EP_Peephole, MPM);
35737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      if (OptLevel > 1 && UseGVNAfterVectorization)
35837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
35937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      else
36037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        MPM.add(createEarlyCSEPass());      // Catch trivial redundancies
36137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
36237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      // BBVectorize may have significantly shortened a loop body; unroll again.
36337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      if (!DisableUnrollLoops)
36437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        MPM.add(createLoopUnrollPass());
36537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    }
36637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
36737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
368dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  addExtensionsToPM(EP_Peephole, MPM);
36936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  MPM.add(createCFGSimplificationPass());
37037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  MPM.add(createInstructionCombiningPass());
3716bf3a05235effa06b10196ee9ebeffee14d886eeChandler Carruth
3724c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  if (!DisableUnrollLoops) {
37336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    MPM.add(createLoopUnrollPass());    // Unroll small loops
3746bf3a05235effa06b10196ee9ebeffee14d886eeChandler Carruth
3754c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    // This is a barrier pass to avoid combine LICM pass and loop unroll pass
3764c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    // within same loop pass manager.
3774c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    MPM.add(createInstructionSimplifierPass());
3784c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar
3794c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    // Runtime unrolling will introduce runtime check in loop prologue. If the
3804c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    // unrolled loop is a inner loop, then the prologue will be inside the
3814c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    // outer loop. LICM pass can help to promote the runtime check out if the
3824c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    // checked value is loop invariant.
3834c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    MPM.add(createLICMPass());
3844c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  }
3854c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar
38637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // After vectorization and unrolling, assume intrinsics may tell us more
38737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // about pointer alignments.
38837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  MPM.add(createAlignmentFromAssumptionsPass());
38937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
390c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  if (!DisableUnitAtATime) {
391c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    // FIXME: We shouldn't bother with this anymore.
392c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
393c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
394f5fdc14df9914969bf56315cd3be8a3b3822a22eEvan Cheng    // GlobalOpt already deletes dead functions and globals, at -O2 try a
395c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    // late pass of GlobalDCE.  It is capable of deleting dead cycles.
396f5fdc14df9914969bf56315cd3be8a3b3822a22eEvan Cheng    if (OptLevel > 1) {
397c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola      MPM.add(createGlobalDCEPass());         // Remove dead fns and globals.
398c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola      MPM.add(createConstantMergePass());     // Merge dup global constants
399f5fdc14df9914969bf56315cd3be8a3b3822a22eEvan Cheng    }
400c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  }
40137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
40237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (MergeFunctions)
40337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    MPM.add(createMergeFunctionsPass());
40437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
4051db394921b9635980555cc8d7e0d05b5e885784fKostya Serebryany  addExtensionsToPM(EP_OptimizerLast, MPM);
406c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola}
407c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
408ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesvoid PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
409c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Provide AliasAnalysis services for optimizations.
410c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  addInitialAliasAnalysisPasses(PM);
411c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
412c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Propagate constants at call sites into the functions they call.  This
413c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // opens opportunities for globalopt (and inlining) by substituting function
414c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // pointers passed as arguments to direct uses of functions.
415c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createIPSCCPPass());
416c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
417c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Now that we internalized some globals, see if we can hack on them!
418c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createGlobalOptimizerPass());
419c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
420c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Linking modules together can lead to duplicated global constants, only
421c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // keep one copy of each constant.
422c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createConstantMergePass());
423c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
424c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Remove unused arguments from functions.
425c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createDeadArgEliminationPass());
426c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
427c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Reduce the code after globalopt and ipsccp.  Both can open up significant
428c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // simplification opportunities, and both can propagate functions through
429c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // function pointers.  When this happens, we often have to resolve varargs
430c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // calls, etc, so let instcombine do this.
431c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createInstructionCombiningPass());
432dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  addExtensionsToPM(EP_Peephole, PM);
433c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
434c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Inline small functions
43537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  bool RunInliner = Inliner;
43637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (RunInliner) {
43737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    PM.add(Inliner);
43837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Inliner = nullptr;
43937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
440c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
441c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createPruneEHPass());   // Remove dead EH info.
442c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
443c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Optimize globals again if we ran the inliner.
444c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  if (RunInliner)
445c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola    PM.add(createGlobalOptimizerPass());
446c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createGlobalDCEPass()); // Remove dead functions.
447c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
448c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // If we didn't decide to inline a function, check to see if we can
449c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // transform it to pass arguments by value instead of by reference.
450c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createArgumentPromotionPass());
451c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
452c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // The IPO passes may leave cruft around.  Clean up after them.
453c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createInstructionCombiningPass());
454dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  addExtensionsToPM(EP_Peephole, PM);
455c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createJumpThreadingPass());
4566e4d93b2325b268d5394b82201c1a782cca78faeBill Wendling
457c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Break up allocas
458713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth  if (UseNewSROA)
459713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth    PM.add(createSROAPass());
460713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth  else
461713aa9431d84805c5b7ddce57159d14ee94cd548Chandler Carruth    PM.add(createScalarReplAggregatesPass());
462c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
463c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Run a few AA driven optimizations here and now, to cleanup the code.
464c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createFunctionAttrsPass()); // Add nocapture.
465c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createGlobalsModRefPass()); // IP alias analysis.
466c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
4673197b4453d214aa96de3a42da8f8fe189fff2077Bill Wendling  PM.add(createLICMPass());                 // Hoist loop invariants.
46837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (EnableMLSM)
46937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
4703197b4453d214aa96de3a42da8f8fe189fff2077Bill Wendling  PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
4713197b4453d214aa96de3a42da8f8fe189fff2077Bill Wendling  PM.add(createMemCpyOptPass());            // Remove dead memcpys.
4726e4d93b2325b268d5394b82201c1a782cca78faeBill Wendling
473c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Nuke dead stores.
474c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createDeadStoreEliminationPass());
475c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
476dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // More loops are countable; try to optimize them.
477dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  PM.add(createIndVarSimplifyPass());
478dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  PM.add(createLoopDeletionPass());
4794c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  if (EnableLoopInterchange)
4804c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    PM.add(createLoopInterchangePass());
4814c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar
48237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  PM.add(createLoopVectorizePass(true, LoopVectorize));
48336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
484dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // More scalar chains could be vectorized due to more alias information
48537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (RunSLPAfterLoopVectorization)
48637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (SLPVectorize)
48737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
48837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
48937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // After vectorization, assume intrinsics may tell us more about pointer
49037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // alignments.
49137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  PM.add(createAlignmentFromAssumptionsPass());
492dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
493c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  if (LoadCombine)
494c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines    PM.add(createLoadCombinePass());
495c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
496c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Cleanup and simplify the code after the scalar optimizations.
497c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createInstructionCombiningPass());
498dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  addExtensionsToPM(EP_Peephole, PM);
499c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
500c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createJumpThreadingPass());
5014c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar}
502c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
5034c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainarvoid PassManagerBuilder::addLateLTOOptimizationPasses(
5044c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    legacy::PassManagerBase &PM) {
505c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Delete basic blocks, which optimization passes may have killed.
50601d7203ef8316fdd71c3cec59f8e68fb869e0dbfTom Stellard  PM.add(createCFGSimplificationPass());
507c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola
508c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  // Now that we have optimized the program, discard unreachable functions.
509c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola  PM.add(createGlobalDCEPass());
51037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
51137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // FIXME: this is profitable (for compiler time) to do at -O0 too, but
51237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // currently it damages debug info.
51337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (MergeFunctions)
51437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    PM.add(createMergeFunctionsPass());
51537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
51637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
517ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesvoid PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
51837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (LibraryInfo)
519ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
52037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
52137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (VerifyInput)
52237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    PM.add(createVerifierPass());
52337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
5244c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  if (OptLevel > 1)
5254c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    addLTOOptimizationPasses(PM);
52637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
5274c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  // Lower bit sets to globals. This pass supports Clang's control flow
5284c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
5294c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  // is enabled. The pass does nothing if CFI is disabled.
5304c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  PM.add(createLowerBitSetsPass());
53137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
53237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (OptLevel != 0)
5334c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    addLateLTOOptimizationPasses(PM);
53437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
5354c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  if (VerifyOutput)
53637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    PM.add(createVerifierPass());
537c684e83e4554bfaf6180426f86b9253b65dd46ecRafael Espindola}
53869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola
5393e39731e88f2d4f597cebc74388fd6650ca4f604Eric Christopherinline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
5403e39731e88f2d4f597cebc74388fd6650ca4f604Eric Christopher    return reinterpret_cast<PassManagerBuilder*>(P);
5413e39731e88f2d4f597cebc74388fd6650ca4f604Eric Christopher}
5423e39731e88f2d4f597cebc74388fd6650ca4f604Eric Christopher
5433e39731e88f2d4f597cebc74388fd6650ca4f604Eric Christopherinline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
5443e39731e88f2d4f597cebc74388fd6650ca4f604Eric Christopher  return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
5453e39731e88f2d4f597cebc74388fd6650ca4f604Eric Christopher}
5463e39731e88f2d4f597cebc74388fd6650ca4f604Eric Christopher
54779c07d2a36282b09b9c5d0aa65ebf4bff017621bDmitri GribenkoLLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
54869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  PassManagerBuilder *PMB = new PassManagerBuilder();
54969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  return wrap(PMB);
55069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola}
55169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola
55269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
55369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  PassManagerBuilder *Builder = unwrap(PMB);
55469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  delete Builder;
55569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola}
55669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola
55769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid
55869cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
55969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola                                  unsigned OptLevel) {
56069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  PassManagerBuilder *Builder = unwrap(PMB);
56169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  Builder->OptLevel = OptLevel;
56269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola}
56369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola
56469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid
56569cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
56669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola                                   unsigned SizeLevel) {
56769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  PassManagerBuilder *Builder = unwrap(PMB);
56869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  Builder->SizeLevel = SizeLevel;
56969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola}
57069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola
57169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid
57269cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
57369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola                                            LLVMBool Value) {
57469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  PassManagerBuilder *Builder = unwrap(PMB);
57569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  Builder->DisableUnitAtATime = Value;
57669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola}
57769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola
57869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid
57969cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
58069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola                                            LLVMBool Value) {
58169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  PassManagerBuilder *Builder = unwrap(PMB);
58269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  Builder->DisableUnrollLoops = Value;
58369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola}
58469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola
58569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid
58669cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
58769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola                                                 LLVMBool Value) {
588be87bce32bc9af9bc5918a6e08806b61e3088165Meador Inge  // NOTE: The simplify-libcalls pass has been removed.
58969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola}
59069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola
59169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid
59269cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
59369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola                                              unsigned Threshold) {
59469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  PassManagerBuilder *Builder = unwrap(PMB);
59569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  Builder->Inliner = createFunctionInliningPass(Threshold);
59669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola}
59769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola
59869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid
59969cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
60069cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola                                                  LLVMPassManagerRef PM) {
60169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  PassManagerBuilder *Builder = unwrap(PMB);
602ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
60369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  Builder->populateFunctionPassManager(*FPM);
60469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola}
60569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola
60669cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid
60769cb216009387694122c78438a9117f31a2f9ad5Rafael EspindolaLLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
60869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola                                                LLVMPassManagerRef PM) {
60969cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  PassManagerBuilder *Builder = unwrap(PMB);
610ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  legacy::PassManagerBase *MPM = unwrap(PM);
61169cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  Builder->populateModulePassManager(*MPM);
61269cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola}
61369cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola
61469cb216009387694122c78438a9117f31a2f9ad5Rafael Espindolavoid LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
61569cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola                                                  LLVMPassManagerRef PM,
6160ebc084132cbf4ebebdb6c931ef42f97e3ff5fe1Nick Lewycky                                                  LLVMBool Internalize,
6170ebc084132cbf4ebebdb6c931ef42f97e3ff5fe1Nick Lewycky                                                  LLVMBool RunInliner) {
61869cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola  PassManagerBuilder *Builder = unwrap(PMB);
619ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  legacy::PassManagerBase *LPM = unwrap(PM);
62037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
62137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // A small backwards compatibility hack. populateLTOPassManager used to take
62237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // an RunInliner option.
62337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (RunInliner && !Builder->Inliner)
62437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Builder->Inliner = createFunctionInliningPass();
62537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
62637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Builder->populateLTOPassManager(*LPM);
62769cb216009387694122c78438a9117f31a2f9ad5Rafael Espindola}
628