llvm.org GIT mirror llvm / 7d3d44a
Migrate SimplifyLibCalls to new OptimizationRemarkEmitter Summary: This changes SimplifyLibCalls to use the new OptimizationRemarkEmitter API. In fact, as SimplifyLibCalls is only ever called via InstCombine, (as far as I can tell) the OptimizationRemarkEmitter is added there, and then passed through to SimplifyLibCalls later. I have avoided changing any remark text. This closes PR33787 Patch by Sam Elliott! Reviewers: anemet, davide Reviewed By: anemet Subscribers: davide, mehdi_amini, eraman, fhahn, llvm-commits Differential Revision: https://reviews.llvm.org/D35608 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@309158 91177308-0d34-0410-b5e6-96231b3b80d8 Adam Nemet 2 years ago
9 changed file(s) with 88 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
2727 class TargetLibraryInfo;
2828 class BasicBlock;
2929 class Function;
30 class OptimizationRemarkEmitter;
3031
3132 /// \brief This class implements simplifications for calls to fortified library
3233 /// functions (__st*cpy_chk, __memcpy_chk, __memmove_chk, __memset_chk), to,
7273 FortifiedLibCallSimplifier FortifiedSimplifier;
7374 const DataLayout &DL;
7475 const TargetLibraryInfo *TLI;
76 OptimizationRemarkEmitter &ORE;
7577 bool UnsafeFPShrink;
7678 function_ref Replacer;
7779
8688
8789 public:
8890 LibCallSimplifier(const DataLayout &DL, const TargetLibraryInfo *TLI,
91 OptimizationRemarkEmitter &ORE,
8992 function_ref Replacer =
9093 &replaceAllUsesWithDefault);
9194
37833783 auto InstCombineRAUW = [this](Instruction *From, Value *With) {
37843784 replaceInstUsesWith(*From, With);
37853785 };
3786 LibCallSimplifier Simplifier(DL, &TLI, InstCombineRAUW);
3786 LibCallSimplifier Simplifier(DL, &TLI, ORE, InstCombineRAUW);
37873787 if (Value *With = Simplifier.optimizeCall(CI)) {
37883788 ++NumSimplified;
37893789 return CI->use_empty() ? CI : replaceInstUsesWith(*CI, With);
4141 class DbgDeclareInst;
4242 class MemIntrinsic;
4343 class MemSetInst;
44 class OptimizationRemarkEmitter;
4445
4546 /// Assign a complexity or rank value to LLVM Values. This is used to reduce
4647 /// the amount of pattern matching needed for compares and commutative
225226 DominatorTree &DT;
226227 const DataLayout &DL;
227228 const SimplifyQuery SQ;
229 OptimizationRemarkEmitter &ORE;
228230 // Optional analyses. When non-null, these can both be used to do better
229231 // combining and will be updated to reflect any changes.
230232 LoopInfo *LI;
235237 InstCombiner(InstCombineWorklist &Worklist, BuilderTy &Builder,
236238 bool MinimizeSize, bool ExpensiveCombines, AliasAnalysis *AA,
237239 AssumptionCache &AC, TargetLibraryInfo &TLI, DominatorTree &DT,
238 const DataLayout &DL, LoopInfo *LI)
240 OptimizationRemarkEmitter &ORE, const DataLayout &DL,
241 LoopInfo *LI)
239242 : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
240243 ExpensiveCombines(ExpensiveCombines), AA(AA), AC(AC), TLI(TLI), DT(DT),
241 DL(DL), SQ(DL, &TLI, &DT, &AC), LI(LI), MadeIRChange(false) {}
244 DL(DL), SQ(DL, &TLI, &DT, &AC), ORE(ORE), LI(LI), MadeIRChange(false) {}
242245
243246 /// \brief Run the combiner over the entire worklist until it is empty.
244247 ///
4747 #include "llvm/Analysis/InstructionSimplify.h"
4848 #include "llvm/Analysis/LoopInfo.h"
4949 #include "llvm/Analysis/MemoryBuiltins.h"
50 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
5051 #include "llvm/Analysis/TargetLibraryInfo.h"
5152 #include "llvm/Analysis/ValueTracking.h"
5253 #include "llvm/IR/CFG.h"
31613162 return MadeIRChange;
31623163 }
31633164
3164 static bool
3165 combineInstructionsOverFunction(Function &F, InstCombineWorklist &Worklist,
3166 AliasAnalysis *AA, AssumptionCache &AC,
3167 TargetLibraryInfo &TLI, DominatorTree &DT,
3168 bool ExpensiveCombines = true,
3169 LoopInfo *LI = nullptr) {
3165 static bool combineInstructionsOverFunction(
3166 Function &F, InstCombineWorklist &Worklist, AliasAnalysis *AA,
3167 AssumptionCache &AC, TargetLibraryInfo &TLI, DominatorTree &DT,
3168 OptimizationRemarkEmitter &ORE, bool ExpensiveCombines = true,
3169 LoopInfo *LI = nullptr) {
31703170 auto &DL = F.getParent()->getDataLayout();
31713171 ExpensiveCombines |= EnableExpensiveCombines;
31723172
31953195
31963196 MadeIRChange |= prepareICWorklistFromFunction(F, DL, &TLI, Worklist);
31973197
3198 InstCombiner IC(Worklist, Builder, F.optForMinSize(), ExpensiveCombines,
3199 AA, AC, TLI, DT, DL, LI);
3198 InstCombiner IC(Worklist, Builder, F.optForMinSize(), ExpensiveCombines, AA,
3199 AC, TLI, DT, ORE, DL, LI);
32003200 IC.MaxArraySizeForCombine = MaxArraySize;
32013201
32023202 if (!IC.run())
32113211 auto &AC = AM.getResult(F);
32123212 auto &DT = AM.getResult(F);
32133213 auto &TLI = AM.getResult(F);
3214 auto &ORE = AM.getResult(F);
32143215
32153216 auto *LI = AM.getCachedResult(F);
32163217
32173218 // FIXME: The AliasAnalysis is not yet supported in the new pass manager
3218 if (!combineInstructionsOverFunction(F, Worklist, nullptr, AC, TLI, DT,
3219 if (!combineInstructionsOverFunction(F, Worklist, nullptr, AC, TLI, DT, ORE,
32193220 ExpensiveCombines, LI))
32203221 // No changes, all analyses are preserved.
32213222 return PreservedAnalyses::all();
32343235 AU.addRequired();
32353236 AU.addRequired();
32363237 AU.addRequired();
3238 AU.addRequired();
32373239 AU.addPreserved();
32383240 AU.addPreserved();
32393241 AU.addPreserved();
32493251 auto &AC = getAnalysis().getAssumptionCache(F);
32503252 auto &TLI = getAnalysis().getTLI();
32513253 auto &DT = getAnalysis().getDomTree();
3254 auto &ORE = getAnalysis().getORE();
32523255
32533256 // Optional analyses.
32543257 auto *LIWP = getAnalysisIfAvailable();
32553258 auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
32563259
3257 return combineInstructionsOverFunction(F, Worklist, AA, AC, TLI, DT,
3260 return combineInstructionsOverFunction(F, Worklist, AA, AC, TLI, DT, ORE,
32583261 ExpensiveCombines, LI);
32593262 }
32603263
32663269 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
32673270 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
32683271 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
3272 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
32693273 INITIALIZE_PASS_END(InstructionCombiningPass, "instcombine",
32703274 "Combine redundant instructions", false, false)
32713275
1717 #include "llvm/ADT/SmallString.h"
1818 #include "llvm/ADT/StringMap.h"
1919 #include "llvm/ADT/Triple.h"
20 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
2021 #include "llvm/Analysis/TargetLibraryInfo.h"
2122 #include "llvm/Analysis/ValueTracking.h"
2223 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/DiagnosticInfo.h"
2424 #include "llvm/IR/Function.h"
2525 #include "llvm/IR/IRBuilder.h"
2626 #include "llvm/IR/IntrinsicInst.h"
483483 uint64_t LenTrue = GetStringLength(SI->getTrueValue(), CharSize);
484484 uint64_t LenFalse = GetStringLength(SI->getFalseValue(), CharSize);
485485 if (LenTrue && LenFalse) {
486 Function *Caller = CI->getParent()->getParent();
487 emitOptimizationRemark(CI->getContext(), "simplify-libcalls", *Caller,
488 SI->getDebugLoc(),
489 "folded strlen(select) to select of constants");
486 ORE.emit(OptimizationRemark("instcombine", "simplify-libcalls", CI)
487 << "folded strlen(select) to select of constants");
490488 return B.CreateSelect(SI->getCondition(),
491489 ConstantInt::get(CI->getType(), LenTrue - 1),
492490 ConstantInt::get(CI->getType(), LenFalse - 1));
22272225
22282226 LibCallSimplifier::LibCallSimplifier(
22292227 const DataLayout &DL, const TargetLibraryInfo *TLI,
2228 OptimizationRemarkEmitter &ORE,
22302229 function_ref Replacer)
2231 : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), UnsafeFPShrink(false),
2232 Replacer(Replacer) {}
2230 : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), ORE(ORE),
2231 UnsafeFPShrink(false), Replacer(Replacer) {}
22332232
22342233 void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) {
22352234 // Indirect through the replacer used in this instance.
8383 ; CHECK-O-NEXT: Running pass: ModuleToFunctionPassAdaptor<{{.*}}PassManager{{.*}}>
8484 ; CHECK-O-NEXT: Starting llvm::Function pass manager run.
8585 ; CHECK-O-NEXT: Running pass: InstCombinePass
86 ; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
8687 ; CHECK-EP-PEEPHOLE-NEXT: Running pass: NoOpFunctionPass
8788 ; CHECK-O-NEXT: Running pass: SimplifyCFGPass
8889 ; CHECK-O-NEXT: Finished llvm::Function pass manager run.
117118 ; CHECK-O3-NEXT: Running pass: LibCallsShrinkWrapPass
118119 ; CHECK-EP-PEEPHOLE-NEXT: Running pass: NoOpFunctionPass
119120 ; CHECK-O-NEXT: Running pass: TailCallElimPass
120 ; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
121121 ; CHECK-O-NEXT: Running pass: SimplifyCFGPass
122122 ; CHECK-O-NEXT: Running pass: ReassociatePass
123123 ; CHECK-O-NEXT: Running pass: RequireAnalysisPass<{{.*}}OptimizationRemarkEmitterAnalysis
5151 ; CHECK-O2-NEXT: Running pass: ModuleToFunctionPassAdaptor<{{.*}}PassManager{{.*}}>
5252 ; CHECK-O2-NEXT: Starting llvm::Function pass manager run.
5353 ; CHECK-O2-NEXT: Running pass: InstCombinePass
54 ; CHECK-O2-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
5455 ; CHECK-EP-Peephole-NEXT: Running pass: NoOpFunctionPass
5556 ; CHECK-O2-NEXT: Finished llvm::Function pass manager run.
5657 ; CHECK-O2-NEXT: Running pass: ModuleToPostOrderCGSCCPassAdaptor<{{.*}}InlinerPass>
6768 ; CHECK-O2-NEXT: Running pass: ModuleToPostOrderCGSCCPassAdaptor<{{.*}}PostOrderFunctionAttrsPass>
6869 ; CHECK-O2-NEXT: Running pass: ModuleToFunctionPassAdaptor<{{.*}}PassManager{{.*}}>
6970 ; CHECK-O2-NEXT: Running analysis: MemoryDependenceAnalysis
70 ; CHECK-O2-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
7171 ; CHECK-O2-NEXT: Running analysis: TargetIRAnalysis
7272 ; CHECK-O2-NEXT: Running analysis: DemandedBitsAnalysis
7373 ; CHECK-O2-NEXT: Running pass: CrossDSOCFIPass
6868 ; CHECK-O-NEXT: Running pass: ModuleToFunctionPassAdaptor<{{.*}}PassManager{{.*}}>
6969 ; CHECK-O-NEXT: Starting llvm::Function pass manager run.
7070 ; CHECK-O-NEXT: Running pass: InstCombinePass
71 ; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
7172 ; CHECK-O-NEXT: Running pass: SimplifyCFGPass
7273 ; CHECK-O-NEXT: Finished llvm::Function pass manager run.
7374 ; CHECK-O-NEXT: Running pass: RequireAnalysisPass<{{.*}}GlobalsAA
100101 ; CHECK-O2-NEXT: Running pass: LibCallsShrinkWrapPass
101102 ; CHECK-O3-NEXT: Running pass: LibCallsShrinkWrapPass
102103 ; CHECK-O-NEXT: Running pass: TailCallElimPass
103 ; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
104104 ; CHECK-O-NEXT: Running pass: SimplifyCFGPass
105105 ; CHECK-O-NEXT: Running pass: ReassociatePass
106106 ; CHECK-O-NEXT: Running pass: RequireAnalysisPass<{{.*}}OptimizationRemarkEmitterAnalysis
0 ; RUN: opt < %s -instcombine -o /dev/null -pass-remarks-output=%t -S -pass-remarks=instcombine \
1 ; RUN: 2>&1 | FileCheck %s
2 ; RUN: cat %t | FileCheck -check-prefix=YAML %s
3 ; RUN: opt < %s -passes='require,instcombine' -o /dev/null -S -pass-remarks=instcombine \
4 ; RUN: 2>&1 | FileCheck %s
5
6 ; CHECK: remark: libcalls-opt-remarks.c:10:10: folded strlen(select) to select of constants{{$}}
7 ; CHECK-NOT: remark:
8
9 ; YAML: --- !Passed
10 ; YAML-NEXT: Pass: instcombine
11 ; YAML-NEXT: Name: simplify-libcalls
12 ; YAML-NEXT: DebugLoc: { File: libcalls-opt-remarks.c, Line: 10, Column: 10 }
13 ; YAML-NEXT: Function: f1
14 ; YAML-NEXT: Args:
15 ; YAML-NEXT: - String: 'folded strlen(select) to select of constants'
16 ; YAML-NEXT: ...
17
18 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
19
20 declare i32 @strlen(i8*)
21
22 @hello = constant [6 x i8] c"hello\00"
23 @longer = constant [7 x i8] c"longer\00"
24
25 define i32 @f1(i1) !dbg !7 {
26 %hello = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0, !dbg !10
27 %longer = getelementptr [7 x i8], [7 x i8]* @longer, i32 0, i32 0, !dbg !12
28 %2 = select i1 %0, i8* %hello, i8* %longer, !dbg !9
29 %3 = call i32 @strlen(i8* %2), !dbg !14
30 ret i32 %3, !dbg !16
31 }
32
33
34
35 !llvm.dbg.cu = !{!0}
36 !llvm.module.flags = !{!3, !4, !5}
37 !llvm.ident = !{!6}
38
39 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "Apple LLVM version 8.1.0 (clang-802.0.42)", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, enums: !2)
40 !1 = !DIFile(filename: "libcalls-opt-remarks.c", directory: "/tmp")
41 !2 = !{}
42 !3 = !{i32 2, !"Dwarf Version", i32 4}
43 !4 = !{i32 2, !"Debug Info Version", i32 3}
44 !5 = !{i32 1, !"PIC Level", i32 2}
45 !6 = !{!"Apple LLVM version 8.1.0 (clang-802.0.42)"}
46 !7 = distinct !DISubprogram(name: "f1", scope: !1, file: !1, line: 9, type: !8, isLocal: false, isDefinition: true, scopeLine: 9, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !2)
47 !8 = !DISubroutineType(types: !2)
48 !9 = !DILocation(line: 10, column: 17, scope: !7)
49 !10 = !DILocation(line: 10, column: 24, scope: !11)
50 !11 = !DILexicalBlockFile(scope: !7, file: !1, discriminator: 1)
51 !12 = !DILocation(line: 10, column: 32, scope: !13)
52 !13 = !DILexicalBlockFile(scope: !7, file: !1, discriminator: 2)
53 !14 = !DILocation(line: 10, column: 10, scope: !15)
54 !15 = !DILexicalBlockFile(scope: !7, file: !1, discriminator: 3)
55 !16 = !DILocation(line: 10, column: 3, scope: !15)