-
Notifications
You must be signed in to change notification settings - Fork 873
Expand file tree
/
Copy pathPassManagerBuilder.cpp
More file actions
952 lines (792 loc) · 35.3 KB
/
PassManagerBuilder.cpp
File metadata and controls
952 lines (792 loc) · 35.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
//===- PassManagerBuilder.cpp - Build Standard Pass -----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the PassManagerBuilder class, which is used to set up a
// "standard" optimization sequence suitable for languages like C and C++.
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm-c/Transforms/PassManagerBuilder.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Vectorize.h"
#include "dxc/HLSL/DxilGenerationPass.h" // HLSL Change
#include "dxc/HLSL/HLMatrixLowerPass.h" // HLSL Change
#include "dxc/HLSL/ComputeViewIdState.h" // HLSL Change
#include "llvm/Analysis/DxilValueCache.h" // HLSL Change
using namespace llvm;
#if HLSL_VECTORIZATION_ENABLED // HLSL Change - don't build vectorization passes
static cl::opt<bool>
RunLoopVectorization("vectorize-loops", cl::Hidden,
cl::desc("Run the Loop vectorization passes"));
static cl::opt<bool>
RunSLPVectorization("vectorize-slp", cl::Hidden,
cl::desc("Run the SLP vectorization passes"));
static cl::opt<bool>
RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
cl::desc("Run the BB vectorization passes"));
static cl::opt<bool>
UseGVNAfterVectorization("use-gvn-after-vectorization",
cl::init(false), cl::Hidden,
cl::desc("Run GVN instead of Early CSE after vectorization passes"));
static cl::opt<bool> ExtraVectorizerPasses(
"extra-vectorizer-passes", cl::init(false), cl::Hidden,
cl::desc("Run cleanup optimization passes after vectorization."));
static cl::opt<bool> UseNewSROA("use-new-sroa",
cl::init(true), cl::Hidden,
cl::desc("Enable the new, experimental SROA pass"));
static cl::opt<bool>
RunLoopRerolling("reroll-loops", cl::Hidden,
cl::desc("Run the loop rerolling pass"));
static cl::opt<bool>
RunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
cl::desc("Run the float2int (float demotion) pass"));
static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
cl::Hidden,
cl::desc("Run the load combining pass"));
static cl::opt<bool>
RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
cl::init(true), cl::Hidden,
cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
"vectorizer instead of before"));
static cl::opt<bool> UseCFLAA("use-cfl-aa",
cl::init(false), cl::Hidden,
cl::desc("Enable the new, experimental CFL alias analysis"));
static cl::opt<bool>
EnableMLSM("mlsm", cl::init(true), cl::Hidden,
cl::desc("Enable motion of merged load and store"));
static cl::opt<bool> EnableLoopInterchange(
"enable-loopinterchange", cl::init(false), cl::Hidden,
cl::desc("Enable the new, experimental LoopInterchange Pass"));
static cl::opt<bool> EnableLoopDistribute(
"enable-loop-distribute", cl::init(false), cl::Hidden,
cl::desc("Enable the new, experimental LoopDistribution Pass"));
#else
// Don't declare the 'false' counterparts - simply avoid altogether.
static const bool UseNewSROA = true;
static const bool RunLoopRerolling = false;
static const bool RunFloat2Int = true;
static const bool RunLoadCombine = false;
#if HLSL_VECTORIZATION_ENABLED // HLSL Change - don't build vectorization passes
static const bool RunSLPAfterLoopVectorization = true;
#endif // HLSL Change
static const bool UseCFLAA = false;
static const bool EnableMLSM = true;
static const bool EnableLoopInterchange = false;
static const bool EnableLoopDistribute = false;
#endif // HLSL Change - don't build vectorization passes
PassManagerBuilder::PassManagerBuilder() {
OptLevel = 2;
SizeLevel = 0;
LibraryInfo = nullptr;
Inliner = nullptr;
DisableUnitAtATime = false;
DisableUnrollLoops = false;
#if HLSL_VECTORIZATION_ENABLED // HLSL Change - don't build vectorization passes
BBVectorize = RunBBVectorization;
SLPVectorize = RunSLPVectorization;
LoopVectorize = RunLoopVectorization;
#else
BBVectorize = SLPVectorize = LoopVectorize = false;
#endif
RerollLoops = RunLoopRerolling;
LoadCombine = RunLoadCombine;
DisableGVNLoadPRE = false;
VerifyInput = false;
VerifyOutput = false;
MergeFunctions = false;
PrepareForLTO = false;
StripDebug = false;
}
PassManagerBuilder::~PassManagerBuilder() {
delete LibraryInfo;
delete Inliner;
}
#if 0 // HLSL Change Starts - no global extensions
/// Set of global extensions, automatically added as part of the standard set.
static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
#endif // HLSL Change Ends
#if 0 // HLSL Change Starts - no global extensions
void PassManagerBuilder::addGlobalExtension(
PassManagerBuilder::ExtensionPointTy Ty,
PassManagerBuilder::ExtensionFn Fn) {
GlobalExtensions->push_back(std::make_pair(Ty, Fn));
}
#endif // HLSL Change Ends
void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
Extensions.push_back(std::make_pair(Ty, Fn));
}
void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
legacy::PassManagerBase &PM) const {
#if 0 // HLSL Change Starts - no global extensions
for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
if ((*GlobalExtensions)[i].first == ETy)
(*GlobalExtensions)[i].second(*this, PM);
for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
if (Extensions[i].first == ETy)
Extensions[i].second(*this, PM);
#endif // HLSL Change Ends
}
void PassManagerBuilder::addInitialAliasAnalysisPasses(
legacy::PassManagerBase &PM) const {
// Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
// BasicAliasAnalysis wins if they disagree. This is intended to help
// support "obvious" type-punning idioms.
if (UseCFLAA)
PM.add(createCFLAliasAnalysisPass());
PM.add(createTypeBasedAliasAnalysisPass());
PM.add(createScopedNoAliasAAPass());
PM.add(createBasicAliasAnalysisPass());
}
void PassManagerBuilder::populateFunctionPassManager(
legacy::FunctionPassManager &FPM) {
addExtensionsToPM(EP_EarlyAsPossible, FPM);
// Add LibraryInfo if we have some.
if (LibraryInfo)
FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
if (OptLevel == 0) return;
addInitialAliasAnalysisPasses(FPM);
FPM.add(createCFGSimplificationPass());
// HLSL Change - don't run SROA.
// HLSL uses special SROA added in addHLSLPasses.
if (HLSLHighLevel) { // HLSL Change
if (UseNewSROA)
FPM.add(createSROAPass());
else
FPM.add(createScalarReplAggregatesPass());
}
// HLSL Change. FPM.add(createEarlyCSEPass());
FPM.add(createLowerExpectIntrinsicPass());
}
// HLSL Change Starts
void PassManagerBuilder::addHLSLPasses(legacy::PassManagerBase &MPM) {
// Don't do any lowering if we're targeting high-level.
if (HLSLHighLevel) {
MPM.add(createHLEmitMetadataPass());
return;
}
MPM.add(createDxilCleanupAddrSpaceCastPass());
MPM.add(createHLPreprocessPass());
bool NoOpt = OptLevel == 0;
if (!NoOpt) {
MPM.add(createHLDeadFunctionEliminationPass());
}
// Do this before scalarrepl-param-hlsl for opportunities to move things
// like resource arrays to alloca, allowing more likely memcpy replacement.
MPM.add(createLowerStaticGlobalIntoAlloca());
// Expand buffer store intrinsics before we SROA
MPM.add(createHLExpandStoreIntrinsicsPass());
// Split struct and array of parameter.
MPM.add(createSROA_Parameter_HLSL());
MPM.add(createHLMatrixLowerPass());
// DCE should after SROA to remove unused element.
MPM.add(createDeadCodeEliminationPass());
MPM.add(createGlobalDCEPass());
if (NoOpt) {
// If not run mem2reg, try to promote allocas used by EvalOperations.
// Do this before change vector to array.
MPM.add(createDxilLegalizeEvalOperationsPass());
}
// This should go between matrix lower and dynamic indexing vector to array,
// because matrix lower may create dynamically indexed global vectors,
// which should become locals. If they are turned into arrays first,
// this pass will ignore them as it only works on scalars and vectors.
MPM.add(createLowerStaticGlobalIntoAlloca());
// Change dynamic indexing vector to array.
MPM.add(createDynamicIndexingVectorToArrayPass(false /* ReplaceAllVector */));
// Rotate the loops before, mem2reg, since it messes up dbg.value's
MPM.add(createLoopRotatePass());
// mem2reg
// Special Mem2Reg pass that skips precise marker.
MPM.add(createDxilConditionalMem2RegPass(NoOpt));
MPM.add(createDxilDeleteRedundantDebugValuesPass());
// Remove unneeded dxbreak conditionals
MPM.add(createCleanupDxBreakPass());
if (!NoOpt) {
MPM.add(createDxilConvergentMarkPass());
// Clean up inefficiencies that can cause unnecessary live values related to
// lifetime marker cleanup blocks. This is the earliest possible location
// without interfering with HLSL-specific lowering.
// Partial lifetime markers don't have cleanup blocks, so these passes are
// unnecessary.
if (HLSLEnableLifetimeMarkers && !HLSLEnablePartialLifetimeMarkers) {
MPM.add(createSROAPass());
MPM.add(createSimplifyInstPass());
MPM.add(createJumpThreadingPass());
}
}
if (!NoOpt)
MPM.add(createSimplifyInstPass());
if (!NoOpt)
MPM.add(createCFGSimplificationPass());
MPM.add(createDxilPromoteLocalResources());
MPM.add(createDxilPromoteStaticResources());
// Verify no undef resource again after promotion
MPM.add(createInvalidateUndefResourcesPass());
MPM.add(createDxilGenerationPass(NoOpt, this->HLSLExtensionsCodeGen));
// Propagate precise attribute.
MPM.add(createDxilPrecisePropagatePass());
if (!NoOpt)
MPM.add(createSimplifyInstPass());
// scalarize vector to scalar
MPM.add(createScalarizerPass(!NoOpt /* AllowFolding */));
// Remove vector instructions
MPM.add(createDxilEliminateVectorPass());
// Passes to handle [unroll]
// Needs to happen after SROA since loop count may depend on
// struct members.
// Needs to happen before resources are lowered and before HL
// module is gone.
MPM.add(createDxilLoopUnrollPass(1024, HLSLOnlyWarnOnUnrollFail, StructurizeLoopExitsForUnroll));
// Default unroll pass. This is purely for optimizing loops without
// attributes.
if (OptLevel > 2) {
MPM.add(createLoopUnrollPass(-1, -1, -1, -1, StructurizeLoopExitsForUnroll));
}
if (!NoOpt)
MPM.add(createSimplifyInstPass());
if (!NoOpt)
MPM.add(createCFGSimplificationPass());
MPM.add(createDeadCodeEliminationPass());
if (OptLevel > 0) {
MPM.add(createDxilFixConstArrayInitializerPass());
}
}
// HLSL Change Ends
void PassManagerBuilder::populateModulePassManager(
legacy::PassManagerBase &MPM) {
// If all optimizations are disabled, just run the always-inline pass and,
// if enabled, the function merging pass.
if (OptLevel == 0) {
if (!HLSLHighLevel) {
MPM.add(createHLEnsureMetadataPass()); // HLSL Change - rehydrate metadata from high-level codegen
}
MPM.add(createDxilRewriteOutputArgDebugInfoPass()); // Fix output argument types.
if (!HLSLHighLevel)
if (HLSLEnableDebugNops) MPM.add(createDxilInsertPreservesPass(HLSLAllowPreserveValues)); // HLSL Change - insert preserve instructions
if (Inliner) {
MPM.add(createHLLegalizeParameter()); // HLSL Change - legalize parameters
// before inline.
MPM.add(Inliner);
Inliner = nullptr;
}
// FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
// creates a CGSCC pass manager, but we don't want to add extensions into
// that pass manager. To prevent this we insert a no-op module pass to reset
// the pass manager to get the same behavior as EP_OptimizerLast in non-O0
// builds. The function merging pass is
if (MergeFunctions)
MPM.add(createMergeFunctionsPass());
else if (!Extensions.empty()) // HLSL Change - GlobalExtensions not considered
MPM.add(createBarrierNoopPass());
if (!HLSLHighLevel)
MPM.add(createDxilPreserveToSelectPass()); // HLSL Change - lower preserve instructions to selects
addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
// HLSL Change Begins.
addHLSLPasses(MPM);
if (!HLSLHighLevel) {
MPM.add(createDxilConvergentClearPass());
MPM.add(createDxilSimpleGVNEliminateRegionPass());
MPM.add(createDeadCodeEliminationPass());
MPM.add(createDxilRemoveDeadBlocksPass());
MPM.add(createDxilEraseDeadRegionPass());
MPM.add(createDxilNoOptSimplifyInstructionsPass());
MPM.add(createGlobalOptimizerPass());
MPM.add(createMultiDimArrayToOneDimArrayPass());
MPM.add(createDeadCodeEliminationPass());
MPM.add(createGlobalDCEPass());
MPM.add(createDxilMutateResourceToHandlePass());
MPM.add(createDxilCleanupDynamicResourceHandlePass());
MPM.add(createDxilLowerCreateHandleForLibPass());
MPM.add(createDxilTranslateRawBuffer());
MPM.add(createDxilLegalizeSampleOffsetPass());
MPM.add(createDxilNoOptLegalizePass());
MPM.add(createDxilFinalizePreservesPass());
MPM.add(createDxilFinalizeModulePass());
MPM.add(createComputeViewIdStatePass());
MPM.add(createDxilDeadFunctionEliminationPass());
MPM.add(createDxilDeleteRedundantDebugValuesPass());
MPM.add(createNoPausePassesPass());
MPM.add(createDxilEmitMetadataPass());
if (StripDebug)
MPM.add(createDxilStripDebugSensitiveInfoPass());
}
// HLSL Change Ends.
return;
}
if (!HLSLHighLevel) {
MPM.add(createHLEnsureMetadataPass()); // HLSL Change - rehydrate metadata from high-level codegen
}
// HLSL Change Begins
MPM.add(createDxilRewriteOutputArgDebugInfoPass()); // Fix output argument types.
MPM.add(createHLLegalizeParameter()); // legalize parameters before inline.
if (HLSLEarlyInlining && Inliner) {
MPM.add(Inliner);
Inliner = nullptr;
}
addHLSLPasses(MPM);
// HLSL Change Ends
// Add LibraryInfo if we have some.
if (LibraryInfo)
MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
addInitialAliasAnalysisPasses(MPM);
if (!DisableUnitAtATime) {
addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
MPM.add(createIPSCCPPass()); // IP SCCP
MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
MPM.add(createInstructionCombiningPass(HLSLNoSink));// Clean up after IPCP & DAE
addExtensionsToPM(EP_Peephole, MPM);
MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
}
// Start of CallGraph SCC passes.
if (!DisableUnitAtATime)
MPM.add(createPruneEHPass()); // Remove dead EH info
if (Inliner) {
MPM.add(Inliner);
Inliner = nullptr;
}
if (!DisableUnitAtATime)
MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
#if 0 // HLSL Change Starts: Disable ArgumentPromotion
if (OptLevel > 2)
MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
#endif // HLSL Change Ends
// Start of function pass.
// Break up aggregate allocas, using SSAUpdater.
if (UseNewSROA)
MPM.add(createSROAPass(/*RequiresDomTree*/ false));
else
MPM.add(createScalarReplAggregatesPass(-1, false));
// HLSL Change. MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
// HLSL Change. MPM.add(createJumpThreadingPass()); // Thread jumps.
MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
MPM.add(createInstructionCombiningPass(HLSLNoSink)); // Combine silly seq's
addExtensionsToPM(EP_Peephole, MPM);
// HLSL Change Begins.
// HLSL does not allow recursize functions.
//MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
// HLSL Change Ends.
MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
MPM.add(createReassociatePass(
HLSLEnableAggressiveReassociation)); // Reassociate expressions
// Rotate Loop - disable header duplication at -Oz
MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
// HLSL Change - disable LICM in frontend for not consider register pressure.
//MPM.add(createLICMPass()); // Hoist loop invariants
//MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3)); // HLSL Change - may move barrier inside divergent if.
MPM.add(createInstructionCombiningPass(HLSLNoSink));
MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
// HLSL Change Begins
// Don't allow loop idiom pass which may insert memset/memcpy thereby breaking the dxil
//MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
// HLSL Change Ends
MPM.add(createLoopDeletionPass()); // Delete dead loops
if (EnableLoopInterchange) {
MPM.add(createLoopInterchangePass()); // Interchange loops
MPM.add(createCFGSimplificationPass());
}
if (!DisableUnrollLoops)
MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
if (OptLevel > 1) {
if (EnableMLSM)
MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
// HLSL Change Begins
if (EnableGVN) {
MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
if (!HLSLResMayAlias)
MPM.add(createDxilSimpleGVNHoistPass());
}
// HLSL Change Ends
}
// HLSL Change Begins.
{
// Run reassociate pass again after GVN since GVN will expose more
// opportunities for reassociation.
if (HLSLEnableAggressiveReassociation) {
MPM.add(createReassociatePass(true)); // Reassociate expressions
if (EnableGVN)
MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
}
}
// Use value numbering to figure out if regions are equivalent, and branch to only one.
MPM.add(createDxilSimpleGVNEliminateRegionPass());
// HLSL don't allow memcpy and memset.
//MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
// HLSL Change Ends.
MPM.add(createSCCPPass()); // Constant prop with SCCP
// Delete dead bit computations (instcombine runs after to fold away the dead
// computations, and then ADCE will run later to exploit any new DCE
// opportunities that creates).
MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
// Run instcombine after redundancy elimination to exploit opportunities
// opened up by them.
MPM.add(createInstructionCombiningPass(HLSLNoSink));
addExtensionsToPM(EP_Peephole, MPM);
// HLSL Change. MPM.add(createJumpThreadingPass()); // Thread jumps
MPM.add(createCorrelatedValuePropagationPass());
MPM.add(createDeadStoreEliminationPass(ScanLimit)); // Delete dead stores
// HLSL Change - disable LICM in frontend for not consider register pressure.
// MPM.add(createLICMPass());
addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
if (RerollLoops)
MPM.add(createLoopRerollPass());
#if HLSL_VECTORIZATION_ENABLED // HLSL Change - don't build vectorization passes
if (!RunSLPAfterLoopVectorization) {
if (SLPVectorize)
MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
if (BBVectorize) {
MPM.add(createBBVectorizePass());
MPM.add(createInstructionCombiningPass());
addExtensionsToPM(EP_Peephole, MPM);
if (OptLevel > 1 && UseGVNAfterVectorization)
MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
else
MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
// BBVectorize may have significantly shortened a loop body; unroll again.
if (!DisableUnrollLoops)
MPM.add(createLoopUnrollPass());
}
}
#endif
if (LoadCombine)
MPM.add(createLoadCombinePass());
MPM.add(createHoistConstantArrayPass()); // HLSL change
MPM.add(createAggressiveDCEPass()); // Delete dead instructions
MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
MPM.add(createInstructionCombiningPass(HLSLNoSink)); // Clean up after everything.
addExtensionsToPM(EP_Peephole, MPM);
// FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
// pass manager that we are specifically trying to avoid. To prevent this
// we must insert a no-op module pass to reset the pass manager.
MPM.add(createBarrierNoopPass());
if (RunFloat2Int)
MPM.add(createFloat2IntPass());
// Re-rotate loops in all our loop nests. These may have fallout out of
// rotated form due to GVN or other transformations, and the vectorizer relies
// on the rotated form. Disable header duplication at -Oz.
MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
// Distribute loops to allow partial vectorization. I.e. isolate dependences
// into separate loop that would otherwise inhibit vectorization.
if (EnableLoopDistribute)
MPM.add(createLoopDistributePass());
#if HLSL_VECTORIZATION_ENABLED // HLSL Change - don't build vectorization passes
MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
#endif
// FIXME: Because of #pragma vectorize enable, the passes below are always
// inserted in the pipeline, even when the vectorizer doesn't run (ex. when
// on -O1 and no #pragma is found). Would be good to have these two passes
// as function calls, so that we can only pass them when the vectorizer
// changed the code.
MPM.add(createInstructionCombiningPass(HLSLNoSink));
#if HLSL_VECTORIZATION_ENABLED // HLSL Change - don't build vectorization passes
if (OptLevel > 1 && ExtraVectorizerPasses) {
// At higher optimization levels, try to clean up any runtime overlap and
// alignment checks inserted by the vectorizer. We want to track correllated
// runtime checks for two inner loops in the same outer loop, fold any
// common computations, hoist loop-invariant aspects out of any outer loop,
// and unswitch the runtime checks if possible. Once hoisted, we may have
// dead (or speculatable) control flows or more combining opportunities.
MPM.add(createEarlyCSEPass());
MPM.add(createCorrelatedValuePropagationPass());
MPM.add(createInstructionCombiningPass());
MPM.add(createLICMPass());
MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
MPM.add(createCFGSimplificationPass());
MPM.add(createInstructionCombiningPass());
}
if (RunSLPAfterLoopVectorization) {
if (SLPVectorize) {
MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
if (OptLevel > 1 && ExtraVectorizerPasses) {
MPM.add(createEarlyCSEPass());
}
}
if (BBVectorize) {
MPM.add(createBBVectorizePass());
MPM.add(createInstructionCombiningPass());
addExtensionsToPM(EP_Peephole, MPM);
if (OptLevel > 1 && UseGVNAfterVectorization)
MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
else
MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
// BBVectorize may have significantly shortened a loop body; unroll again.
if (!DisableUnrollLoops)
MPM.add(createLoopUnrollPass());
}
}
#endif // HLSL Change - don't build vectorization passes
addExtensionsToPM(EP_Peephole, MPM);
MPM.add(createCFGSimplificationPass());
MPM.add(createDxilLoopDeletionPass(HLSLNoSink)); // HLSL Change - try to delete loop again.
//MPM.add(createInstructionCombiningPass()); // HLSL Change - pass is included in above
if (!DisableUnrollLoops) {
MPM.add(createLoopUnrollPass(/* HLSL Change begin */-1, -1, -1, -1, this->StructurizeLoopExitsForUnroll /* HLSL Change end */)); // Unroll small loops
// LoopUnroll may generate some redundency to cleanup.
MPM.add(createInstructionCombiningPass(HLSLNoSink));
// Runtime unrolling will introduce runtime check in loop prologue. If the
// unrolled loop is a inner loop, then the prologue will be inside the
// outer loop. LICM pass can help to promote the runtime check out if the
// checked value is loop invariant.
// MPM.add(createLICMPass());// HLSL Change - disable LICM in frontend for
// not consider register pressure.
}
// After vectorization and unrolling, assume intrinsics may tell us more
// about pointer alignments.
MPM.add(createAlignmentFromAssumptionsPass());
if (!DisableUnitAtATime) {
// FIXME: We shouldn't bother with this anymore.
MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
// GlobalOpt already deletes dead functions and globals, at -O2 try a
// late pass of GlobalDCE. It is capable of deleting dead cycles.
if (OptLevel > 1) {
if (!PrepareForLTO) {
// Remove avail extern fns and globals definitions if we aren't
// compiling an object file for later LTO. For LTO we want to preserve
// these so they are eligible for inlining at link-time. Note if they
// are unreferenced they will be removed by GlobalDCE below, so
// this only impacts referenced available externally globals.
// Eventually they will be suppressed during codegen, but eliminating
// here enables more opportunity for GlobalDCE as it may make
// globals referenced by available external functions dead.
MPM.add(createEliminateAvailableExternallyPass());
}
MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
MPM.add(createConstantMergePass()); // Merge dup global constants
}
}
if (MergeFunctions)
MPM.add(createMergeFunctionsPass());
// HLSL Change Begins.
if (!HLSLHighLevel) {
MPM.add(createDxilEraseDeadRegionPass());
MPM.add(createDxilConvergentClearPass());
MPM.add(createDeadCodeEliminationPass()); // DCE needed after clearing convergence
// annotations before CreateHandleForLib
// so no unused resources get re-added to
// DxilModule.
MPM.add(createMultiDimArrayToOneDimArrayPass());
MPM.add(createDxilRemoveDeadBlocksPass());
MPM.add(createDeadCodeEliminationPass());
MPM.add(createGlobalDCEPass());
MPM.add(createDxilMutateResourceToHandlePass());
MPM.add(createDxilCleanupDynamicResourceHandlePass());
MPM.add(createDxilLowerCreateHandleForLibPass());
MPM.add(createDxilTranslateRawBuffer());
// Always try to legalize sample offsets as loop unrolling
// is not guaranteed for higher opt levels.
MPM.add(createDxilLegalizeSampleOffsetPass());
MPM.add(createDxilFinalizeModulePass());
MPM.add(createComputeViewIdStatePass());
MPM.add(createDxilDeadFunctionEliminationPass());
MPM.add(createDxilDeleteRedundantDebugValuesPass());
MPM.add(createNoPausePassesPass());
MPM.add(createDxilValidateWaveSensitivityPass());
MPM.add(createDxilEmitMetadataPass());
if (StripDebug)
MPM.add(createDxilStripDebugSensitiveInfoPass());
}
// HLSL Change Ends.
addExtensionsToPM(EP_OptimizerLast, MPM);
}
#if 0 // HLSL Change: No LTO
void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
// Provide AliasAnalysis services for optimizations.
addInitialAliasAnalysisPasses(PM);
// Propagate constants at call sites into the functions they call. This
// opens opportunities for globalopt (and inlining) by substituting function
// pointers passed as arguments to direct uses of functions.
PM.add(createIPSCCPPass());
// Now that we internalized some globals, see if we can hack on them!
PM.add(createGlobalOptimizerPass());
// Linking modules together can lead to duplicated global constants, only
// keep one copy of each constant.
PM.add(createConstantMergePass());
// Remove unused arguments from functions.
PM.add(createDeadArgEliminationPass());
// Reduce the code after globalopt and ipsccp. Both can open up significant
// simplification opportunities, and both can propagate functions through
// function pointers. When this happens, we often have to resolve varargs
// calls, etc, so let instcombine do this.
PM.add(createInstructionCombiningPass());
addExtensionsToPM(EP_Peephole, PM);
// Inline small functions
bool RunInliner = Inliner;
if (RunInliner) {
PM.add(Inliner);
Inliner = nullptr;
}
PM.add(createPruneEHPass()); // Remove dead EH info.
// Optimize globals again if we ran the inliner.
if (RunInliner)
PM.add(createGlobalOptimizerPass());
PM.add(createGlobalDCEPass()); // Remove dead functions.
// If we didn't decide to inline a function, check to see if we can
// transform it to pass arguments by value instead of by reference.
PM.add(createArgumentPromotionPass());
// The IPO passes may leave cruft around. Clean up after them.
PM.add(createInstructionCombiningPass());
addExtensionsToPM(EP_Peephole, PM);
// HLSL Change. PM.add(createJumpThreadingPass());
// Break up allocas
if (UseNewSROA)
PM.add(createSROAPass());
else
PM.add(createScalarReplAggregatesPass());
// Run a few AA driven optimizations here and now, to cleanup the code.
PM.add(createFunctionAttrsPass()); // Add nocapture.
PM.add(createGlobalsModRefPass()); // IP alias analysis.
// HLSL Change - disable LICM in frontend for not consider register pressure.
// PM.add(createLICMPass()); // Hoist loop invariants.
if (EnableMLSM)
PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
if (EnableGVN) // HLSL Change
PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
PM.add(createMemCpyOptPass()); // Remove dead memcpys.
// Nuke dead stores.
PM.add(createDeadStoreEliminationPass(ScanLimit)); // HLSL Change - add ScanLimit
// More loops are countable; try to optimize them.
PM.add(createIndVarSimplifyPass());
PM.add(createLoopDeletionPass());
if (EnableLoopInterchange)
PM.add(createLoopInterchangePass());
#if HLSL_VECTORIZATION_ENABLED // HLSL Change - don't build vectorization passes
PM.add(createLoopVectorizePass(true, LoopVectorize));
// More scalar chains could be vectorized due to more alias information
if (RunSLPAfterLoopVectorization)
if (SLPVectorize)
PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
// After vectorization, assume intrinsics may tell us more about pointer
// alignments.
PM.add(createAlignmentFromAssumptionsPass());
#endif
if (LoadCombine)
PM.add(createLoadCombinePass());
// Cleanup and simplify the code after the scalar optimizations.
PM.add(createInstructionCombiningPass());
addExtensionsToPM(EP_Peephole, PM);
// HLSL Change. PM.add(createJumpThreadingPass());
}
void PassManagerBuilder::addLateLTOOptimizationPasses(
legacy::PassManagerBase &PM) {
// Delete basic blocks, which optimization passes may have killed.
PM.add(createCFGSimplificationPass());
// Now that we have optimized the program, discard unreachable functions.
PM.add(createGlobalDCEPass());
// FIXME: this is profitable (for compiler time) to do at -O0 too, but
// currently it damages debug info.
if (MergeFunctions)
PM.add(createMergeFunctionsPass());
}
void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
if (LibraryInfo)
PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
if (VerifyInput)
PM.add(createVerifierPass());
if (OptLevel > 1)
addLTOOptimizationPasses(PM);
// Lower bit sets to globals. This pass supports Clang's control flow
// integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
// is enabled. The pass does nothing if CFI is disabled.
PM.add(createLowerBitSetsPass());
if (OptLevel != 0)
addLateLTOOptimizationPasses(PM);
if (VerifyOutput)
PM.add(createVerifierPass());
}
#endif
inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
return reinterpret_cast<PassManagerBuilder*>(P);
}
inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
}
LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
PassManagerBuilder *PMB = new PassManagerBuilder();
return wrap(PMB);
}
void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
PassManagerBuilder *Builder = unwrap(PMB);
delete Builder;
}
void
LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
unsigned OptLevel) {
PassManagerBuilder *Builder = unwrap(PMB);
Builder->OptLevel = OptLevel;
}
void
LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
unsigned SizeLevel) {
PassManagerBuilder *Builder = unwrap(PMB);
Builder->SizeLevel = SizeLevel;
}
void
LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
LLVMBool Value) {
PassManagerBuilder *Builder = unwrap(PMB);
Builder->DisableUnitAtATime = Value;
}
void
LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
LLVMBool Value) {
PassManagerBuilder *Builder = unwrap(PMB);
Builder->DisableUnrollLoops = Value;
}
void
LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
LLVMBool Value) {
// NOTE: The simplify-libcalls pass has been removed.
}
void
LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
unsigned Threshold) {
PassManagerBuilder *Builder = unwrap(PMB);
Builder->Inliner = createFunctionInliningPass(Threshold);
}
void
LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
LLVMPassManagerRef PM) {
PassManagerBuilder *Builder = unwrap(PMB);
legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Builder->populateFunctionPassManager(*FPM);
}
void
LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
LLVMPassManagerRef PM) {
PassManagerBuilder *Builder = unwrap(PMB);
legacy::PassManagerBase *MPM = unwrap(PM);
Builder->populateModulePassManager(*MPM);
}
#if 0 // HLSL Change: No LTO
void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
LLVMPassManagerRef PM,
LLVMBool Internalize,
LLVMBool RunInliner) {
PassManagerBuilder *Builder = unwrap(PMB);
legacy::PassManagerBase *LPM = unwrap(PM);
// A small backwards compatibility hack. populateLTOPassManager used to take
// an RunInliner option.
if (RunInliner && !Builder->Inliner)
Builder->Inliner = createFunctionInliningPass();
Builder->populateLTOPassManager(*LPM);
}
#endif