Coverage Report

Created: 2019-02-20 00:17

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Analysis/InstructionSimplify.h
Line
Count
Source (jump to first uncovered line)
1
//===-- InstructionSimplify.h - Fold instrs into simpler forms --*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file declares routines for folding instructions into simpler forms
10
// that do not require creating new instructions.  This does constant folding
11
// ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
12
// returning a constant ("and i32 %x, 0" -> "0") or an already existing value
13
// ("and i32 %x, %x" -> "%x").  If the simplification is also an instruction
14
// then it dominates the original instruction.
15
//
16
// These routines implicitly resolve undef uses. The easiest way to be safe when
17
// using these routines to obtain simplified values for existing instructions is
18
// to always replace all uses of the instructions with the resulting simplified
19
// values. This will prevent other code from seeing the same undef uses and
20
// resolving them to different values.
21
//
22
// These routines are designed to tolerate moderately incomplete IR, such as
23
// instructions that are not connected to basic blocks yet. However, they do
24
// require that all the IR that they encounter be valid. In particular, they
25
// require that all non-constant values be defined in the same function, and the
26
// same call context of that function (and not split between caller and callee
27
// contexts of a directly recursive call, for example).
28
//
29
//===----------------------------------------------------------------------===//
30
31
#ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
32
#define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
33
34
#include "llvm/IR/Instruction.h"
35
#include "llvm/IR/Operator.h"
36
#include "llvm/IR/User.h"
37
38
namespace llvm {
39
class Function;
40
template <typename T, typename... TArgs> class AnalysisManager;
41
template <class T> class ArrayRef;
42
class AssumptionCache;
43
class CallBase;
44
class DominatorTree;
45
class DataLayout;
46
class FastMathFlags;
47
struct LoopStandardAnalysisResults;
48
class OptimizationRemarkEmitter;
49
class Pass;
50
class TargetLibraryInfo;
51
class Type;
52
class Value;
53
class MDNode;
54
class BinaryOperator;
55
56
/// InstrInfoQuery provides an interface to query additional information for
57
/// instructions like metadata or keywords like nsw, which provides conservative
58
/// results if the users specified it is safe to use.
59
struct InstrInfoQuery {
60
328M
  InstrInfoQuery(bool UMD) : UseInstrInfo(UMD) {}
61
197M
  InstrInfoQuery() : UseInstrInfo(true) {}
62
  bool UseInstrInfo = true;
63
64
208M
  MDNode *getMetadata(const Instruction *I, unsigned KindID) const {
65
208M
    if (UseInstrInfo)
66
208M
      return I->getMetadata(KindID);
67
935
    return nullptr;
68
935
  }
69
70
6.01M
  template <class InstT> bool hasNoUnsignedWrap(const InstT *Op) const {
71
6.01M
    if (UseInstrInfo)
72
6.01M
      return Op->hasNoUnsignedWrap();
73
60
    return false;
74
60
  }
bool llvm::InstrInfoQuery::hasNoUnsignedWrap<llvm::OverflowingBinaryOperator>(llvm::OverflowingBinaryOperator const*) const
Line
Count
Source
70
2.43M
  template <class InstT> bool hasNoUnsignedWrap(const InstT *Op) const {
71
2.43M
    if (UseInstrInfo)
72
2.43M
      return Op->hasNoUnsignedWrap();
73
60
    return false;
74
60
  }
bool llvm::InstrInfoQuery::hasNoUnsignedWrap<llvm::BinaryOperator>(llvm::BinaryOperator const*) const
Line
Count
Source
70
3.58M
  template <class InstT> bool hasNoUnsignedWrap(const InstT *Op) const {
71
3.58M
    if (UseInstrInfo)
72
3.58M
      return Op->hasNoUnsignedWrap();
73
0
    return false;
74
0
  }
75
76
115M
  template <class InstT> bool hasNoSignedWrap(const InstT *Op) const {
77
115M
    if (UseInstrInfo)
78
115M
      return Op->hasNoSignedWrap();
79
326
    return false;
80
326
  }
bool llvm::InstrInfoQuery::hasNoSignedWrap<llvm::OverflowingBinaryOperator>(llvm::OverflowingBinaryOperator const*) const
Line
Count
Source
76
111M
  template <class InstT> bool hasNoSignedWrap(const InstT *Op) const {
77
111M
    if (UseInstrInfo)
78
111M
      return Op->hasNoSignedWrap();
79
326
    return false;
80
326
  }
bool llvm::InstrInfoQuery::hasNoSignedWrap<llvm::BinaryOperator>(llvm::BinaryOperator const*) const
Line
Count
Source
76
3.59M
  template <class InstT> bool hasNoSignedWrap(const InstT *Op) const {
77
3.59M
    if (UseInstrInfo)
78
3.59M
      return Op->hasNoSignedWrap();
79
0
    return false;
80
0
  }
bool llvm::InstrInfoQuery::hasNoSignedWrap<llvm::Instruction>(llvm::Instruction const*) const
Line
Count
Source
76
667k
  template <class InstT> bool hasNoSignedWrap(const InstT *Op) const {
77
667k
    if (UseInstrInfo)
78
667k
      return Op->hasNoSignedWrap();
79
0
    return false;
80
0
  }
81
82
496k
  bool isExact(const BinaryOperator *Op) const {
83
496k
    if (UseInstrInfo && isa<PossiblyExactOperator>(Op))
84
496k
      return cast<PossiblyExactOperator>(Op)->isExact();
85
0
    return false;
86
0
  }
87
};
88
89
struct SimplifyQuery {
90
  const DataLayout &DL;
91
  const TargetLibraryInfo *TLI = nullptr;
92
  const DominatorTree *DT = nullptr;
93
  AssumptionCache *AC = nullptr;
94
  const Instruction *CxtI = nullptr;
95
96
  // Wrapper to query additional information for instructions like metadata or
97
  // keywords like nsw, which provides conservative results if those cannot
98
  // be safely used.
99
  const InstrInfoQuery IIQ;
100
101
  SimplifyQuery(const DataLayout &DL, const Instruction *CXTI = nullptr)
102
197M
      : DL(DL), CxtI(CXTI) {}
103
104
  SimplifyQuery(const DataLayout &DL, const TargetLibraryInfo *TLI,
105
                const DominatorTree *DT = nullptr,
106
                AssumptionCache *AC = nullptr,
107
                const Instruction *CXTI = nullptr, bool UseInstrInfo = true)
108
46.7M
      : DL(DL), TLI(TLI), DT(DT), AC(AC), CxtI(CXTI), IIQ(UseInstrInfo) {}
109
212M
  SimplifyQuery getWithInstruction(Instruction *I) const {
110
212M
    SimplifyQuery Copy(*this);
111
212M
    Copy.CxtI = I;
112
212M
    return Copy;
113
212M
  }
114
};
115
116
// NOTE: the explicit multiple argument versions of these functions are
117
// deprecated.
118
// Please use the SimplifyQuery versions in new code.
119
120
/// Given operands for an Add, fold the result or return null.
121
Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
122
                       const SimplifyQuery &Q);
123
124
/// Given operands for a Sub, fold the result or return null.
125
Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
126
                       const SimplifyQuery &Q);
127
128
/// Given operands for an FAdd, fold the result or return null.
129
Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
130
                        const SimplifyQuery &Q);
131
132
/// Given operands for an FSub, fold the result or return null.
133
Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
134
                        const SimplifyQuery &Q);
135
136
/// Given operands for an FMul, fold the result or return null.
137
Value *SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF,
138
                        const SimplifyQuery &Q);
139
140
/// Given operands for a Mul, fold the result or return null.
141
Value *SimplifyMulInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
142
143
/// Given operands for an SDiv, fold the result or return null.
144
Value *SimplifySDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
145
146
/// Given operands for a UDiv, fold the result or return null.
147
Value *SimplifyUDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
148
149
/// Given operands for an FDiv, fold the result or return null.
150
Value *SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF,
151
                        const SimplifyQuery &Q);
152
153
/// Given operands for an SRem, fold the result or return null.
154
Value *SimplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
155
156
/// Given operands for a URem, fold the result or return null.
157
Value *SimplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
158
159
/// Given operands for an FRem, fold the result or return null.
160
Value *SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF,
161
                        const SimplifyQuery &Q);
162
163
/// Given operands for a Shl, fold the result or return null.
164
Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
165
                       const SimplifyQuery &Q);
166
167
/// Given operands for a LShr, fold the result or return null.
168
Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
169
                        const SimplifyQuery &Q);
170
171
/// Given operands for a AShr, fold the result or return nulll.
172
Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
173
                        const SimplifyQuery &Q);
174
175
/// Given operands for an And, fold the result or return null.
176
Value *SimplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
177
178
/// Given operands for an Or, fold the result or return null.
179
Value *SimplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
180
181
/// Given operands for an Xor, fold the result or return null.
182
Value *SimplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
183
184
/// Given operands for an ICmpInst, fold the result or return null.
185
Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
186
                        const SimplifyQuery &Q);
187
188
/// Given operands for an FCmpInst, fold the result or return null.
189
Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
190
                        FastMathFlags FMF, const SimplifyQuery &Q);
191
192
/// Given operands for a SelectInst, fold the result or return null.
193
Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
194
                          const SimplifyQuery &Q);
195
196
/// Given operands for a GetElementPtrInst, fold the result or return null.
197
Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
198
                       const SimplifyQuery &Q);
199
200
/// Given operands for an InsertValueInst, fold the result or return null.
201
Value *SimplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
202
                               const SimplifyQuery &Q);
203
204
/// Given operands for an InsertElement, fold the result or return null.
205
Value *SimplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx,
206
                                 const SimplifyQuery &Q);
207
208
/// Given operands for an ExtractValueInst, fold the result or return null.
209
Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
210
                                const SimplifyQuery &Q);
211
212
/// Given operands for an ExtractElementInst, fold the result or return null.
213
Value *SimplifyExtractElementInst(Value *Vec, Value *Idx,
214
                                  const SimplifyQuery &Q);
215
216
/// Given operands for a CastInst, fold the result or return null.
217
Value *SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
218
                        const SimplifyQuery &Q);
219
220
/// Given operands for a ShuffleVectorInst, fold the result or return null.
221
Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask,
222
                                 Type *RetTy, const SimplifyQuery &Q);
223
224
//=== Helper functions for higher up the class hierarchy.
225
226
/// Given operands for a CmpInst, fold the result or return null.
227
Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
228
                       const SimplifyQuery &Q);
229
230
/// Given operands for a BinaryOperator, fold the result or return null.
231
Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
232
                     const SimplifyQuery &Q);
233
234
/// Given operands for an FP BinaryOperator, fold the result or return null.
235
/// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the
236
/// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp.
237
Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
238
                       FastMathFlags FMF, const SimplifyQuery &Q);
239
240
/// Given a callsite, fold the result or return null.
241
Value *SimplifyCall(CallBase *Call, const SimplifyQuery &Q);
242
243
/// Given a function and iterators over arguments, fold the result or return
244
/// null.
245
Value *SimplifyCall(CallBase *Call, Value *V, User::op_iterator ArgBegin,
246
                    User::op_iterator ArgEnd, const SimplifyQuery &Q);
247
248
/// Given a function and set of arguments, fold the result or return null.
249
Value *SimplifyCall(CallBase *Call, Value *V, ArrayRef<Value *> Args,
250
                    const SimplifyQuery &Q);
251
252
/// See if we can compute a simplified version of this instruction. If not,
253
/// return null.
254
Value *SimplifyInstruction(Instruction *I, const SimplifyQuery &Q,
255
                           OptimizationRemarkEmitter *ORE = nullptr);
256
257
/// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
258
///
259
/// This first performs a normal RAUW of I with SimpleV. It then recursively
260
/// attempts to simplify those users updated by the operation. The 'I'
261
/// instruction must not be equal to the simplified value 'SimpleV'.
262
///
263
/// The function returns true if any simplifications were performed.
264
bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
265
                                   const TargetLibraryInfo *TLI = nullptr,
266
                                   const DominatorTree *DT = nullptr,
267
                                   AssumptionCache *AC = nullptr);
268
269
/// Recursively attempt to simplify an instruction.
270
///
271
/// This routine uses SimplifyInstruction to simplify 'I', and if successful
272
/// replaces uses of 'I' with the simplified value. It then recurses on each
273
/// of the users impacted. It returns true if any simplifications were
274
/// performed.
275
bool recursivelySimplifyInstruction(Instruction *I,
276
                                    const TargetLibraryInfo *TLI = nullptr,
277
                                    const DominatorTree *DT = nullptr,
278
                                    AssumptionCache *AC = nullptr);
279
280
// These helper functions return a SimplifyQuery structure that contains as
281
// many of the optional analysis we use as are currently valid.  This is the
282
// strongly preferred way of constructing SimplifyQuery in passes.
283
const SimplifyQuery getBestSimplifyQuery(Pass &, Function &);
284
template <class T, class... TArgs>
285
const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &,
286
                                         Function &);
287
const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &,
288
                                         const DataLayout &);
289
} // end namespace llvm
290
291
#endif
292