llvm.org GIT mirror llvm / 65696f4
[GVN] Initial check-in of a new global value numbering algorithm. The code have been developed by Daniel Berlin over the years, and the new implementation goal is that of addressing shortcomings of the current GVN infrastructure, i.e. long compile time for large testcases, lack of phi predication, no load/store value numbering etc... The current code just implements the "core" GVN algorithm, although other pieces (load coercion, phi handling, predicate system) are already implemented in a branch out of tree. Once the core is stable, we'll start adding pieces on top of the base framework. The test currently living in test/Transform/NewGVN are a copy of the ones in GVN, with proper `XFAIL` (missing features in NewGVN). A flag will be added in a future commit to enable NewGVN, so that interested parties can exercise this code easily. Differential Revision: https://reviews.llvm.org/D26224 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@290346 91177308-0d34-0410-b5e6-96231b3b80d8 Davide Italiano 2 years ago
97 changed file(s) with 8160 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
251251 void initializeModuleSummaryIndexWrapperPassPass(PassRegistry &);
252252 void initializeNameAnonGlobalLegacyPassPass(PassRegistry &);
253253 void initializeNaryReassociateLegacyPassPass(PassRegistry &);
254 void initializeNewGVNPass(PassRegistry&);
254255 void initializeNoAAPass(PassRegistry&);
255256 void initializeObjCARCAAWrapperPassPass(PassRegistry&);
256257 void initializeObjCARCAPElimPass(PassRegistry&);
166166 (void) llvm::createGVNHoistPass();
167167 (void) llvm::createMergedLoadStoreMotionPass();
168168 (void) llvm::createGVNPass();
169 (void) llvm::createNewGVNPass();
169170 (void) llvm::createMemCpyOptPass();
170171 (void) llvm::createLoopDeletionPass();
171172 (void) llvm::createPostDomTree();
0 //======- GVNExpression.h - GVN Expression classes -------*- C++ -*-==-------=//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// The header file for the GVN pass that contains expression handling
11 /// classes
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TRANSFORMS_SCALAR_GVNEXPRESSION_H
16 #define LLVM_TRANSFORMS_SCALAR_GVNEXPRESSION_H
17
18 #include "llvm/ADT/Hashing.h"
19 #include "llvm/IR/Constant.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/Value.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Support/ArrayRecycler.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include
27
28 namespace llvm {
29 class MemoryAccess;
30
31 namespace GVNExpression {
32
33 enum ExpressionType {
34 ET_Base,
35 ET_Constant,
36 ET_Variable,
37 ET_BasicStart,
38 ET_Basic,
39 ET_Call,
40 ET_AggregateValue,
41 ET_Phi,
42 ET_Load,
43 ET_Store,
44 ET_BasicEnd
45 };
46
47 class Expression {
48 private:
49 ExpressionType EType;
50 unsigned Opcode;
51
52 public:
53 Expression(const Expression &) = delete;
54 Expression(ExpressionType ET = ET_Base, unsigned O = ~2U)
55 : EType(ET), Opcode(O) {}
56 void operator=(const Expression &) = delete;
57 virtual ~Expression();
58
59 static unsigned getEmptyKey() { return ~0U; }
60 static unsigned getTombstoneKey() { return ~1U; }
61
62 bool operator==(const Expression &Other) const {
63 if (getOpcode() != Other.getOpcode())
64 return false;
65 if (getOpcode() == getEmptyKey() || getOpcode() == getTombstoneKey())
66 return true;
67 // Compare the expression type for anything but load and store.
68 // For load and store we set the opcode to zero.
69 // This is needed for load coercion.
70 if (getExpressionType() != ET_Load &&
71 getExpressionType() != ET_Store &&
72 getExpressionType() != Other.getExpressionType())
73 return false;
74
75 return equals(Other);
76 }
77
78 virtual bool equals(const Expression &Other) const { return true; }
79
80 unsigned getOpcode() const { return Opcode; }
81 void setOpcode(unsigned opcode) { Opcode = opcode; }
82 ExpressionType getExpressionType() const { return EType; }
83
84 virtual hash_code getHashValue() const {
85 return hash_combine(getExpressionType(), getOpcode());
86 }
87
88 //
89 // Debugging support
90 //
91 virtual void printInternal(raw_ostream &OS, bool PrintEType) const {
92 if (PrintEType)
93 OS << "etype = " << getExpressionType() << ",";
94 OS << "opcode = " << getOpcode() << ", ";
95 }
96
97 void print(raw_ostream &OS) const {
98 OS << "{ ";
99 printInternal(OS, true);
100 OS << "}";
101 }
102 void dump() const { print(dbgs()); }
103 };
104
105 inline raw_ostream &operator<<(raw_ostream &OS, const Expression &E) {
106 E.print(OS);
107 return OS;
108 }
109
110 class BasicExpression : public Expression {
111 private:
112 typedef ArrayRecycler RecyclerType;
113 typedef RecyclerType::Capacity RecyclerCapacity;
114 Value **Operands;
115 unsigned MaxOperands;
116 unsigned NumOperands;
117 Type *ValueType;
118
119 public:
120 static bool classof(const Expression *EB) {
121 ExpressionType ET = EB->getExpressionType();
122 return ET > ET_BasicStart && ET < ET_BasicEnd;
123 }
124
125 BasicExpression(unsigned NumOperands)
126 : BasicExpression(NumOperands, ET_Basic) {}
127 BasicExpression(unsigned NumOperands, ExpressionType ET)
128 : Expression(ET), Operands(nullptr), MaxOperands(NumOperands),
129 NumOperands(0), ValueType(nullptr) {}
130 virtual ~BasicExpression() override;
131 void operator=(const BasicExpression &) = delete;
132 BasicExpression(const BasicExpression &) = delete;
133 BasicExpression() = delete;
134
135 /// \brief Swap two operands. Used during GVN to put commutative operands in
136 /// order.
137 void swapOperands(unsigned First, unsigned Second) {
138 std::swap(Operands[First], Operands[Second]);
139 }
140
141 Value *getOperand(unsigned N) const {
142 assert(Operands && "Operands not allocated");
143 assert(N < NumOperands && "Operand out of range");
144 return Operands[N];
145 }
146
147 void setOperand(unsigned N, Value *V) {
148 assert(Operands && "Operands not allocated before setting");
149 assert(N < NumOperands && "Operand out of range");
150 Operands[N] = V;
151 }
152
153 unsigned getNumOperands() const { return NumOperands; }
154
155 typedef Value **op_iterator;
156 typedef Value *const *const_ops_iterator;
157 op_iterator ops_begin() { return Operands; }
158 op_iterator ops_end() { return Operands + NumOperands; }
159 const_ops_iterator ops_begin() const { return Operands; }
160 const_ops_iterator ops_end() const { return Operands + NumOperands; }
161 iterator_range operands() {
162 return iterator_range(ops_begin(), ops_end());
163 }
164 iterator_range operands() const {
165 return iterator_range(ops_begin(), ops_end());
166 }
167
168 void ops_push_back(Value *Arg) {
169 assert(NumOperands < MaxOperands && "Tried to add too many operands");
170 assert(Operands && "Operandss not allocated before pushing");
171 Operands[NumOperands++] = Arg;
172 }
173 bool ops_empty() const { return getNumOperands() == 0; }
174
175 void allocateOperands(RecyclerType &Recycler, BumpPtrAllocator &Allocator) {
176 assert(!Operands && "Operands already allocated");
177 Operands = Recycler.allocate(RecyclerCapacity::get(MaxOperands), Allocator);
178 }
179 void deallocateOperands(RecyclerType &Recycler) {
180 Recycler.deallocate(RecyclerCapacity::get(MaxOperands), Operands);
181 }
182
183 void setType(Type *T) { ValueType = T; }
184 Type *getType() const { return ValueType; }
185
186 virtual bool equals(const Expression &Other) const override {
187 if (getOpcode() != Other.getOpcode())
188 return false;
189
190 const auto &OE = cast(Other);
191 if (getType() != OE.getType())
192 return false;
193 if (NumOperands != OE.NumOperands)
194 return false;
195 if (!std::equal(ops_begin(), ops_end(), OE.ops_begin()))
196 return false;
197 return true;
198 }
199
200 virtual hash_code getHashValue() const override {
201 return hash_combine(getExpressionType(), getOpcode(), ValueType,
202 hash_combine_range(ops_begin(), ops_end()));
203 }
204
205 //
206 // Debugging support
207 //
208 virtual void printInternal(raw_ostream &OS, bool PrintEType) const override {
209 if (PrintEType)
210 OS << "ExpressionTypeBasic, ";
211
212 this->Expression::printInternal(OS, false);
213 OS << "operands = {";
214 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
215 OS << "[" << i << "] = ";
216 Operands[i]->printAsOperand(OS);
217 OS << " ";
218 }
219 OS << "} ";
220 }
221 };
222
223 class CallExpression final : public BasicExpression {
224 private:
225 CallInst *Call;
226 MemoryAccess *DefiningAccess;
227
228 public:
229 static bool classof(const Expression *EB) {
230 return EB->getExpressionType() == ET_Call;
231 }
232
233 CallExpression(unsigned NumOperands, CallInst *C, MemoryAccess *DA)
234 : BasicExpression(NumOperands, ET_Call), Call(C),
235 DefiningAccess(DA) {}
236 void operator=(const CallExpression &) = delete;
237 CallExpression(const CallExpression &) = delete;
238 CallExpression() = delete;
239 virtual ~CallExpression() override;
240
241 virtual bool equals(const Expression &Other) const override {
242 if (!this->BasicExpression::equals(Other))
243 return false;
244 const auto &OE = cast(Other);
245 return DefiningAccess == OE.DefiningAccess;
246 }
247
248 virtual hash_code getHashValue() const override {
249 return hash_combine(this->BasicExpression::getHashValue(), DefiningAccess);
250 }
251
252 //
253 // Debugging support
254 //
255 virtual void printInternal(raw_ostream &OS, bool PrintEType) const override {
256 if (PrintEType)
257 OS << "ExpressionTypeCall, ";
258 this->BasicExpression::printInternal(OS, false);
259 OS << " represents call at " << Call;
260 }
261 };
262
263 class LoadExpression final : public BasicExpression {
264 private:
265 LoadInst *Load;
266 MemoryAccess *DefiningAccess;
267 unsigned Alignment;
268
269 public:
270 static bool classof(const Expression *EB) {
271 return EB->getExpressionType() == ET_Load;
272 }
273
274 LoadExpression(unsigned NumOperands, LoadInst *L, MemoryAccess *DA)
275 : LoadExpression(ET_Load, NumOperands, L, DA) {}
276 LoadExpression(enum ExpressionType EType, unsigned NumOperands,
277 LoadInst *L, MemoryAccess *DA)
278 : BasicExpression(NumOperands, EType), Load(L), DefiningAccess(DA) {
279 Alignment = L ? L->getAlignment() : 0;
280 }
281 void operator=(const LoadExpression &) = delete;
282 LoadExpression(const LoadExpression &) = delete;
283 LoadExpression() = delete;
284 virtual ~LoadExpression() override;
285
286 LoadInst *getLoadInst() const { return Load; }
287 void setLoadInst(LoadInst *L) { Load = L; }
288
289 MemoryAccess *getDefiningAccess() const { return DefiningAccess; }
290 void setDefiningAccess(MemoryAccess *MA) { DefiningAccess = MA; }
291 unsigned getAlignment() const { return Alignment; }
292 void setAlignment(unsigned Align) { Alignment = Align; }
293
294 virtual bool equals(const Expression &Other) const override;
295
296 virtual hash_code getHashValue() const override {
297 return hash_combine(getOpcode(), getType(), DefiningAccess,
298 hash_combine_range(ops_begin(), ops_end()));
299 }
300
301 //
302 // Debugging support
303 //
304 virtual void printInternal(raw_ostream &OS, bool PrintEType) const override {
305 if (PrintEType)
306 OS << "ExpressionTypeLoad, ";
307 this->BasicExpression::printInternal(OS, false);
308 OS << " represents Load at " << Load;
309 OS << " with DefiningAccess " << DefiningAccess;
310 }
311 };
312
313 class StoreExpression final : public BasicExpression {
314 private:
315 StoreInst *Store;
316 MemoryAccess *DefiningAccess;
317
318 public:
319 static bool classof(const Expression *EB) {
320 return EB->getExpressionType() == ET_Store;
321 }
322
323 StoreExpression(unsigned NumOperands, StoreInst *S, MemoryAccess *DA)
324 : BasicExpression(NumOperands, ET_Store), Store(S),
325 DefiningAccess(DA) {}
326 void operator=(const StoreExpression &) = delete;
327 StoreExpression(const StoreExpression &) = delete;
328 StoreExpression() = delete;
329 virtual ~StoreExpression() override;
330
331 StoreInst *getStoreInst() const { return Store; }
332 MemoryAccess *getDefiningAccess() const { return DefiningAccess; }
333
334 virtual bool equals(const Expression &Other) const override;
335
336 virtual hash_code getHashValue() const override {
337 return hash_combine(getOpcode(), getType(), DefiningAccess,
338 hash_combine_range(ops_begin(), ops_end()));
339 }
340
341 //
342 // Debugging support
343 //
344 virtual void printInternal(raw_ostream &OS, bool PrintEType) const override {
345 if (PrintEType)
346 OS << "ExpressionTypeStore, ";
347 this->BasicExpression::printInternal(OS, false);
348 OS << " represents Store at " << Store;
349 }
350 };
351
352 class AggregateValueExpression final : public BasicExpression {
353 private:
354 unsigned MaxIntOperands;
355 unsigned NumIntOperands;
356 unsigned *IntOperands;
357
358 public:
359 static bool classof(const Expression *EB) {
360 return EB->getExpressionType() == ET_AggregateValue;
361 }
362
363 AggregateValueExpression(unsigned NumOperands,
364 unsigned NumIntOperands)
365 : BasicExpression(NumOperands, ET_AggregateValue),
366 MaxIntOperands(NumIntOperands), NumIntOperands(0),
367 IntOperands(nullptr) {}
368
369 void operator=(const AggregateValueExpression &) = delete;
370 AggregateValueExpression(const AggregateValueExpression &) = delete;
371 AggregateValueExpression() = delete;
372 virtual ~AggregateValueExpression() override;
373
374 typedef unsigned *int_arg_iterator;
375 typedef const unsigned *const_int_arg_iterator;
376
377 int_arg_iterator int_ops_begin() { return IntOperands; }
378 int_arg_iterator int_ops_end() { return IntOperands + NumIntOperands; }
379 const_int_arg_iterator int_ops_begin() const { return IntOperands; }
380 const_int_arg_iterator int_ops_end() const {
381 return IntOperands + NumIntOperands;
382 }
383 unsigned int_ops_size() const { return NumIntOperands; }
384 bool int_ops_empty() const { return NumIntOperands == 0; }
385 void int_ops_push_back(unsigned IntOperand) {
386 assert(NumIntOperands < MaxIntOperands &&
387 "Tried to add too many int operands");
388 assert(IntOperands && "Operands not allocated before pushing");
389 IntOperands[NumIntOperands++] = IntOperand;
390 }
391
392 virtual void allocateIntOperands(BumpPtrAllocator &Allocator) {
393 assert(!IntOperands && "Operands already allocated");
394 IntOperands = Allocator.Allocate(MaxIntOperands);
395 }
396
397 virtual bool equals(const Expression &Other) const override {
398 if (!this->BasicExpression::equals(Other))
399 return false;
400 const AggregateValueExpression &OE = cast(Other);
401 if (NumIntOperands != OE.NumIntOperands)
402 return false;
403 if (!std::equal(int_ops_begin(), int_ops_end(), OE.int_ops_begin()))
404 return false;
405 return true;
406 }
407
408 virtual hash_code getHashValue() const override {
409 return hash_combine(this->BasicExpression::getHashValue(),
410 hash_combine_range(int_ops_begin(), int_ops_end()));
411 }
412
413 //
414 // Debugging support
415 //
416 virtual void printInternal(raw_ostream &OS, bool PrintEType) const override {
417 if (PrintEType)
418 OS << "ExpressionTypeAggregateValue, ";
419 this->BasicExpression::printInternal(OS, false);
420 OS << ", intoperands = {";
421 for (unsigned i = 0, e = int_ops_size(); i != e; ++i) {
422 OS << "[" << i << "] = " << IntOperands[i] << " ";
423 }
424 OS << "}";
425 }
426 };
427
428 class PHIExpression final : public BasicExpression {
429 private:
430 BasicBlock *BB;
431
432 public:
433 static bool classof(const Expression *EB) {
434 return EB->getExpressionType() == ET_Phi;
435 }
436
437 PHIExpression(unsigned NumOperands, BasicBlock *B)
438 : BasicExpression(NumOperands, ET_Phi), BB(B) {}
439 void operator=(const PHIExpression &) = delete;
440 PHIExpression(const PHIExpression &) = delete;
441 PHIExpression() = delete;
442 virtual ~PHIExpression() override;
443
444 virtual bool equals(const Expression &Other) const override {
445 if (!this->BasicExpression::equals(Other))
446 return false;
447 const PHIExpression &OE = cast(Other);
448 if (BB != OE.BB)
449 return false;
450 return true;
451 }
452
453 virtual hash_code getHashValue() const override {
454 return hash_combine(this->BasicExpression::getHashValue(), BB);
455 }
456
457 //
458 // Debugging support
459 //
460 virtual void printInternal(raw_ostream &OS, bool PrintEType) const override {
461 if (PrintEType)
462 OS << "ExpressionTypePhi, ";
463 this->BasicExpression::printInternal(OS, false);
464 OS << "bb = " << BB;
465 }
466 };
467
468 class VariableExpression final : public Expression {
469 private:
470 Value *VariableValue;
471
472 public:
473 static bool classof(const Expression *EB) {
474 return EB->getExpressionType() == ET_Variable;
475 }
476
477 VariableExpression(Value *V)
478 : Expression(ET_Variable), VariableValue(V) {}
479 void operator=(const VariableExpression &) = delete;
480 VariableExpression(const VariableExpression &) = delete;
481 VariableExpression() = delete;
482
483 Value *getVariableValue() const { return VariableValue; }
484 void setVariableValue(Value *V) { VariableValue = V; }
485 virtual bool equals(const Expression &Other) const override {
486 const VariableExpression &OC = cast(Other);
487 if (VariableValue != OC.VariableValue)
488 return false;
489 return true;
490 }
491
492 virtual hash_code getHashValue() const override {
493 return hash_combine(getExpressionType(), VariableValue->getType(),
494 VariableValue);
495 }
496
497 //
498 // Debugging support
499 //
500 virtual void printInternal(raw_ostream &OS, bool PrintEType) const override {
501 if (PrintEType)
502 OS << "ExpressionTypeVariable, ";
503 this->Expression::printInternal(OS, false);
504 OS << " variable = " << *VariableValue;
505 }
506 };
507
508 class ConstantExpression final : public Expression {
509 private:
510 Constant *ConstantValue;
511
512 public:
513 static bool classof(const Expression *EB) {
514 return EB->getExpressionType() == ET_Constant;
515 }
516
517 ConstantExpression()
518 : Expression(ET_Constant), ConstantValue(NULL) {}
519 ConstantExpression(Constant *constantValue)
520 : Expression(ET_Constant), ConstantValue(constantValue) {}
521 void operator=(const ConstantExpression &) = delete;
522 ConstantExpression(const ConstantExpression &) = delete;
523
524 Constant *getConstantValue() const { return ConstantValue; }
525 void setConstantValue(Constant *V) { ConstantValue = V; }
526
527 virtual bool equals(const Expression &Other) const override {
528 const ConstantExpression &OC = cast(Other);
529 return ConstantValue == OC.ConstantValue;
530 }
531
532 virtual hash_code getHashValue() const override {
533 return hash_combine(getExpressionType(), ConstantValue->getType(),
534 ConstantValue);
535 }
536
537 //
538 // Debugging support
539 //
540 virtual void printInternal(raw_ostream &OS, bool PrintEType) const override {
541 if (PrintEType)
542 OS << "ExpressionTypeConstant, ";
543 this->Expression::printInternal(OS, false);
544 OS << " constant = " << *ConstantValue;
545 }
546 };
547 }
548 }
549
550 #endif
0 //===----- NewGVN.h - Global Value Numbering Pass ---------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This file provides the interface for LLVM's Global Value Numbering pass.
10 ///
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_TRANSFORMS_SCALAR_NEWGVN_H
14 #define LLVM_TRANSFORMS_SCALAR_NEWGVN_H
15
16 #include "llvm/IR/PassManager.h"
17
18 namespace llvm {
19 class NewGVNPass : public PassInfoMixin {
20 public:
21 /// \brief Run the pass over the function.
22 PreservedAnalyses run(Function &F, AnalysisManager &AM);
23 };
24 }
25
26 #endif // LLVM_TRANSFORMS_SCALAR_NEWGVN_H
27
347347
348348 //===----------------------------------------------------------------------===//
349349 //
350 // GVN - This pass performs global value numbering and redundant load
351 // elimination cotemporaneously.
352 //
353 FunctionPass *createNewGVNPass();
354
355 //===----------------------------------------------------------------------===//
356 //
350357 // MemCpyOpt - This pass performs optimizations related to eliminating memcpy
351358 // calls and/or combining multiple stores into memset's.
352359 //
5454
5555 /** See llvm::createGVNPass function. */
5656 void LLVMAddGVNPass(LLVMPassManagerRef PM);
57
58 /** See llvm::createGVNPass function. */
59 void LLVMAddNewGVNPass(LLVMPassManagerRef PM);
5760
5861 /** See llvm::createIndVarSimplifyPass function. */
5962 void LLVMAddIndVarSimplifyPass(LLVMPassManagerRef PM);
3838 MemCpyOptimizer.cpp
3939 MergedLoadStoreMotion.cpp
4040 NaryReassociate.cpp
41 NewGVN.cpp
4142 PartiallyInlineLibCalls.cpp
4243 PlaceSafepoints.cpp
4344 Reassociate.cpp
0 //===---- NewGVN.cpp - Global Value Numbering Pass --------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This file implements the new LLVM's Global Value Numbering pass.
10 /// GVN partitions values computed by a function into congruence classes.
11 /// Values ending up in the same congruence class are guaranteed to be the same
12 /// for every execution of the program. In that respect, congruency is a
13 /// compile-time approximation of equivalence of values at runtime.
14 /// The algorithm implemented here uses a sparse formulation and it's based
15 /// on the ideas described in the paper:
16 /// "A Sparse Algorithm for Predicated Global Value Numbering" from
17 /// Karthik Gargi.
18 ///
19 //===----------------------------------------------------------------------===//
20
21 #include "llvm/Transforms/Scalar/NewGVN.h"
22 #include "llvm/ADT/BitVector.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/DenseSet.h"
25 #include "llvm/ADT/DepthFirstIterator.h"
26 #include "llvm/ADT/Hashing.h"
27 #include "llvm/ADT/MapVector.h"
28 #include "llvm/ADT/PostOrderIterator.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/SmallSet.h"
31 #include "llvm/ADT/SparseBitVector.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/ADT/TinyPtrVector.h"
34 #include "llvm/Analysis/AliasAnalysis.h"
35 #include "llvm/Analysis/AssumptionCache.h"
36 #include "llvm/Analysis/CFG.h"
37 #include "llvm/Analysis/CFGPrinter.h"
38 #include "llvm/Analysis/ConstantFolding.h"
39 #include "llvm/Analysis/GlobalsModRef.h"
40 #include "llvm/Analysis/InstructionSimplify.h"
41 #include "llvm/Analysis/Loads.h"
42 #include "llvm/Analysis/MemoryBuiltins.h"
43 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
44 #include "llvm/Analysis/MemoryLocation.h"
45 #include "llvm/Analysis/PHITransAddr.h"
46 #include "llvm/Analysis/TargetLibraryInfo.h"
47 #include "llvm/Analysis/ValueTracking.h"
48 #include "llvm/IR/DataLayout.h"
49 #include "llvm/IR/Dominators.h"
50 #include "llvm/IR/GlobalVariable.h"
51 #include "llvm/IR/IRBuilder.h"
52 #include "llvm/IR/IntrinsicInst.h"
53 #include "llvm/IR/LLVMContext.h"
54 #include "llvm/IR/Metadata.h"
55 #include "llvm/IR/PatternMatch.h"
56 #include "llvm/IR/PredIteratorCache.h"
57 #include "llvm/IR/Type.h"
58 #include "llvm/Support/Allocator.h"
59 #include "llvm/Support/CommandLine.h"
60 #include "llvm/Support/Debug.h"
61 #include "llvm/Transforms/Scalar.h"
62 #include "llvm/Transforms/Scalar/GVNExpression.h"
63 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
64 #include "llvm/Transforms/Utils/Local.h"
65 #include "llvm/Transforms/Utils/MemorySSA.h"
66 #include "llvm/Transforms/Utils/SSAUpdater.h"
67 #include
68 #include
69 #include
70 using namespace llvm;
71 using namespace PatternMatch;
72 using namespace llvm::GVNExpression;
73
74 #define DEBUG_TYPE "newgvn"
75
76 STATISTIC(NumGVNInstrDeleted, "Number of instructions deleted");
77 STATISTIC(NumGVNBlocksDeleted, "Number of blocks deleted");
78 STATISTIC(NumGVNOpsSimplified, "Number of Expressions simplified");
79 STATISTIC(NumGVNPhisAllSame, "Number of PHIs whos arguments are all the same");
80
81 //===----------------------------------------------------------------------===//
82 // GVN Pass
83 //===----------------------------------------------------------------------===//
84
85 // Anchor methods.
86 namespace llvm {
87 namespace GVNExpression {
88 Expression::~Expression() = default;
89 BasicExpression::~BasicExpression() = default;
90 CallExpression::~CallExpression() = default;
91 LoadExpression::~LoadExpression() = default;
92 StoreExpression::~StoreExpression() = default;
93 AggregateValueExpression::~AggregateValueExpression() = default;
94 PHIExpression::~PHIExpression() = default;
95 }
96 }
97
98 // Congruence classes represent the set of expressions/instructions
99 // that are all the same *during some scope in the function*.
100 // That is, because of the way we perform equality propagation, and
101 // because of memory value numbering, it is not correct to assume
102 // you can willy-nilly replace any member with any other at any
103 // point in the function.
104 //
105 // For any Value in the Member set, it is valid to replace any dominated member
106 // with that Value.
107 //
108 // Every congruence class has a leader, and the leader is used to
109 // symbolize instructions in a canonical way (IE every operand of an
110 // instruction that is a member of the same congruence class will
111 // always be replaced with leader during symbolization).
112 // To simplify symbolization, we keep the leader as a constant if class can be
113 // proved to be a constant value.
114 // Otherwise, the leader is a randomly chosen member of the value set, it does
115 // not matter which one is chosen.
116 // Each congruence class also has a defining expression,
117 // though the expression may be null. If it exists, it can be used for forward
118 // propagation and reassociation of values.
119 //
120 struct CongruenceClass {
121 typedef SmallPtrSet MemberSet;
122 unsigned ID;
123 // Representative leader.
124 Value *RepLeader;
125 // Defining Expression.
126 const Expression *DefiningExpr;
127 // Actual members of this class.
128 MemberSet Members;
129
130 // True if this class has no members left. This is mainly used for assertion
131 // purposes, and for skipping empty classes.
132 bool Dead;
133
134 explicit CongruenceClass(unsigned ID)
135 : ID(ID), RepLeader(0), DefiningExpr(0), Dead(false) {}
136 CongruenceClass(unsigned ID, Value *Leader, const Expression *E)
137 : ID(ID), RepLeader(Leader), DefiningExpr(E), Dead(false) {}
138 };
139
140 namespace llvm {
141 template <> struct DenseMapInfo {
142 static const Expression *getEmptyKey() {
143 uintptr_t Val = static_cast(-1);
144 Val <<= PointerLikeTypeTraits::NumLowBitsAvailable;
145 return reinterpret_cast(Val);
146 }
147 static const Expression *getTombstoneKey() {
148 uintptr_t Val = static_cast(~1U);
149 Val <<= PointerLikeTypeTraits::NumLowBitsAvailable;
150 return reinterpret_cast(Val);
151 }
152 static unsigned getHashValue(const Expression *V) {
153 return static_cast(V->getHashValue());
154 }
155 static bool isEqual(const Expression *LHS, const Expression *RHS) {
156 if (LHS == RHS)
157 return true;
158 if (LHS == getTombstoneKey() || RHS == getTombstoneKey() ||
159 LHS == getEmptyKey() || RHS == getEmptyKey())
160 return false;
161 return *LHS == *RHS;
162 }
163 };
164 } // end namespace llvm
165
166 class NewGVN : public FunctionPass {
167 DominatorTree *DT;
168 const DataLayout *DL;
169 const TargetLibraryInfo *TLI;
170 AssumptionCache *AC;
171 AliasAnalysis *AA;
172 MemorySSA *MSSA;
173 MemorySSAWalker *MSSAWalker;
174 BumpPtrAllocator ExpressionAllocator;
175 ArrayRecycler ArgRecycler;
176
177 // Congruence class info.
178 CongruenceClass *InitialClass;
179 std::vector CongruenceClasses;
180 unsigned NextCongruenceNum;
181
182 // Value Mappings.
183 DenseMap ValueToClass;
184 DenseMap ValueToExpression;
185
186 // Expression to class mapping.
187 typedef DenseMap ExpressionClassMap;
188 ExpressionClassMap ExpressionToClass;
189
190 // Which values have changed as a result of leader changes.
191 SmallPtrSet ChangedValues;
192
193 // Reachability info.
194 typedef BasicBlockEdge BlockEdge;
195 DenseSet ReachableEdges;
196 SmallPtrSet ReachableBlocks;
197
198 // This is a bitvector because, on larger functions, we may have
199 // thousands of touched instructions at once (entire blocks,
200 // instructions with hundreds of uses, etc). Even with optimization
201 // for when we mark whole blocks as touched, when this was a
202 // SmallPtrSet or DenseSet, for some functions, we spent >20% of all
203 // the time in GVN just managing this list. The bitvector, on the
204 // other hand, efficiently supports test/set/clear of both
205 // individual and ranges, as well as "find next element" This
206 // enables us to use it as a worklist with essentially 0 cost.
207 BitVector TouchedInstructions;
208
209 DenseMap> BlockInstRange;
210 DenseMap>
211 DominatedInstRange;
212
213 #ifndef NDEBUG
214 // Debugging for how many times each block and instruction got processed.
215 DenseMap ProcessedCount;
216 #endif
217
218 // DFS info.
219 DenseMap> DFSDomMap;
220 DenseMap InstrDFS;
221 std::vector DFSToInstr;
222
223 // Deletion info.
224 SmallPtrSet InstructionsToErase;
225
226 public:
227 static char ID; // Pass identification, replacement for typeid.
228 NewGVN() : FunctionPass(ID) {
229 initializeNewGVNPass(*PassRegistry::getPassRegistry());
230 }
231
232 bool runOnFunction(Function &F) override;
233 bool runGVN(Function &F, DominatorTree *DT, AssumptionCache *AC,
234 TargetLibraryInfo *TLI, AliasAnalysis *AA,
235 MemorySSA *MSSA);
236
237 private:
238 // This transformation requires dominator postdominator info.
239 void getAnalysisUsage(AnalysisUsage &AU) const override {
240 AU.addRequired();
241 AU.addRequired();
242 AU.addRequired();
243 AU.addRequired();
244 AU.addRequired();
245
246 AU.addPreserved();
247 AU.addPreserved();
248 }
249
250 // Expression handling.
251 const Expression *createExpression(Instruction *, const BasicBlock *);
252 const Expression *createBinaryExpression(unsigned, Type *, Value *, Value *,
253 const BasicBlock *);
254 PHIExpression *createPHIExpression(Instruction *);
255 const VariableExpression *createVariableExpression(Value *);
256 const ConstantExpression *createConstantExpression(Constant *);
257 const Expression *createVariableOrConstant(Value *V, const BasicBlock *B);
258 const StoreExpression *createStoreExpression(StoreInst *, MemoryAccess *,
259 const BasicBlock *);
260 LoadExpression *createLoadExpression(Type *, Value *, LoadInst *,
261 MemoryAccess *, const BasicBlock *);
262
263 const CallExpression *createCallExpression(CallInst *, MemoryAccess *,
264 const BasicBlock *);
265 const AggregateValueExpression *
266 createAggregateValueExpression(Instruction *, const BasicBlock *);
267 bool setBasicExpressionInfo(Instruction *, BasicExpression *,
268 const BasicBlock *);
269
270 // Congruence class handling.
271 CongruenceClass *createCongruenceClass(Value *Leader, const Expression *E) {
272 CongruenceClass *result =
273 new CongruenceClass(NextCongruenceNum++, Leader, E);
274 CongruenceClasses.emplace_back(result);
275 return result;
276 }
277
278 CongruenceClass *createSingletonCongruenceClass(Value *Member) {
279 CongruenceClass *CClass = createCongruenceClass(Member, NULL);
280 CClass->Members.insert(Member);
281 ValueToClass[Member] = CClass;
282 return CClass;
283 }
284 void initializeCongruenceClasses(Function &F);
285
286 // Symbolic evaluation.
287 const Expression *checkSimplificationResults(Expression *, Instruction *,
288 Value *);
289 const Expression *performSymbolicEvaluation(Value *, const BasicBlock *);
290 const Expression *performSymbolicLoadEvaluation(Instruction *,
291 const BasicBlock *);
292 const Expression *performSymbolicStoreEvaluation(Instruction *,
293 const BasicBlock *);
294 const Expression *performSymbolicCallEvaluation(Instruction *,
295 const BasicBlock *);
296 const Expression *performSymbolicPHIEvaluation(Instruction *,
297 const BasicBlock *);
298 const Expression *performSymbolicAggrValueEvaluation(Instruction *,
299 const BasicBlock *);
300
301 // Congruence finding.
302 // Templated to allow them to work both on BB's and BB-edges.
303 template
304 Value *lookupOperandLeader(Value *, const User *, const T &) const;
305 void performCongruenceFinding(Value *, const Expression *);
306
307 // Reachability handling.
308 void updateReachableEdge(BasicBlock *, BasicBlock *);
309 void processOutgoingEdges(TerminatorInst *, BasicBlock *);
310 bool isOnlyReachableViaThisEdge(const BasicBlockEdge &);
311 Value *findConditionEquivalence(Value *, BasicBlock *) const;
312
313 // Elimination.
314 struct ValueDFS;
315 void convertDenseToDFSOrdered(CongruenceClass::MemberSet &,
316 std::vector &);
317
318 bool eliminateInstructions(Function &);
319 void replaceInstruction(Instruction *, Value *);
320 void markInstructionForDeletion(Instruction *);
321 void deleteInstructionsInBlock(BasicBlock *);
322
323 // New instruction creation.
324 void handleNewInstruction(Instruction *){};
325 void markUsersTouched(Value *);
326 void markMemoryUsersTouched(MemoryAccess *);
327
328 // Utilities.
329 void cleanupTables();
330 std::pair assignDFSNumbers(BasicBlock *, unsigned);
331 void updateProcessedCount(Value *V);
332 };
333
334 char NewGVN::ID = 0;
335
336 // createGVNPass - The public interface to this file.
337 FunctionPass *llvm::createNewGVNPass() { return new NewGVN(); }
338
339 bool LoadExpression::equals(const Expression &Other) const {
340 if (!isa(Other) && !isa(Other))
341 return false;
342 if (!this->BasicExpression::equals(Other))
343 return false;
344 if (const auto *OtherL = dyn_cast(&Other)) {
345 if (DefiningAccess != OtherL->getDefiningAccess())
346 return false;
347 } else if (const auto *OtherS = dyn_cast(&Other)) {
348 if (DefiningAccess != OtherS->getDefiningAccess())
349 return false;
350 }
351
352 return true;
353 }
354
355 bool StoreExpression::equals(const Expression &Other) const {
356 if (!isa(Other) && !isa(Other))
357 return false;
358 if (!this->BasicExpression::equals(Other))
359 return false;
360 if (const auto *OtherL = dyn_cast(&Other)) {
361 if (DefiningAccess != OtherL->getDefiningAccess())
362 return false;
363 } else if (const auto *OtherS = dyn_cast(&Other)) {
364 if (DefiningAccess != OtherS->getDefiningAccess())
365 return false;
366 }
367
368 return true;
369 }
370
371 #ifndef NDEBUG
372 static std::string getBlockName(const BasicBlock *B) {
373 return DOTGraphTraits::getSimpleNodeLabel(B, NULL);
374 }
375 #endif
376
377 INITIALIZE_PASS_BEGIN(NewGVN, "newgvn", "Global Value Numbering", false, false)
378 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
379 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
380 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
381 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
382 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
383 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
384 INITIALIZE_PASS_END(NewGVN, "newgvn", "Global Value Numbering", false, false)
385
386 PHIExpression *NewGVN::createPHIExpression(Instruction *I) {
387 BasicBlock *PhiBlock = I->getParent();
388 PHINode *PN = cast(I);
389 PHIExpression *E = new (ExpressionAllocator)
390 PHIExpression(PN->getNumOperands(), I->getParent());
391
392 E->allocateOperands(ArgRecycler, ExpressionAllocator);
393 E->setType(I->getType());
394 E->setOpcode(I->getOpcode());
395 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
396 BasicBlock *B = PN->getIncomingBlock(i);
397 if (!ReachableBlocks.count(B)) {
398 DEBUG(dbgs() << "Skipping unreachable block " << getBlockName(B)
399 << " in PHI node " << *PN << "\n");
400 continue;
401 }
402 if (I->getOperand(i) != I) {
403 const BasicBlockEdge BBE(B, PhiBlock);
404 auto Operand = lookupOperandLeader(I->getOperand(i), I, BBE);
405 E->ops_push_back(Operand);
406 } else {
407 E->ops_push_back(I->getOperand(i));
408 }
409 }
410 return E;
411 }
412
413 // Set basic expression info (Arguments, type, opcode) for Expression
414 // E from Instruction I in block B.
415 bool NewGVN::setBasicExpressionInfo(Instruction *I, BasicExpression *E,
416 const BasicBlock *B) {
417 bool AllConstant = true;
418 if (auto *GEP = dyn_cast(I))
419 E->setType(GEP->getSourceElementType());
420 else
421 E->setType(I->getType());
422 E->setOpcode(I->getOpcode());
423 E->allocateOperands(ArgRecycler, ExpressionAllocator);
424
425 for (auto &O : I->operands()) {
426 auto Operand = lookupOperandLeader(O, I, B);
427 if (!isa(Operand))
428 AllConstant = false;
429 E->ops_push_back(Operand);
430 }
431 return AllConstant;
432 }
433
434 const Expression *NewGVN::createBinaryExpression(unsigned Opcode, Type *T,
435 Value *Arg1, Value *Arg2,
436 const BasicBlock *B) {
437 BasicExpression *E = new (ExpressionAllocator) BasicExpression(2);
438
439 E->setType(T);
440 E->setOpcode(Opcode);
441 E->allocateOperands(ArgRecycler, ExpressionAllocator);
442 if (Instruction::isCommutative(Opcode)) {
443 // Ensure that commutative instructions that only differ by a permutation
444 // of their operands get the same value number by sorting the operand value
445 // numbers. Since all commutative instructions have two operands it is more
446 // efficient to sort by hand rather than using, say, std::sort.
447 if (Arg1 > Arg2)
448 std::swap(Arg1, Arg2);
449 }
450 E->ops_push_back(lookupOperandLeader(Arg1, nullptr, B));
451 E->ops_push_back(lookupOperandLeader(Arg2, nullptr, B));
452
453 Value *V = SimplifyBinOp(Opcode, E->getOperand(0), E->getOperand(1), *DL, TLI,
454 DT, AC);
455 if (const Expression *SimplifiedE = checkSimplificationResults(E, nullptr, V))
456 return SimplifiedE;
457 return E;
458 }
459
460 // Take a Value returned by simplification of Expression E/Instruction
461 // I, and see if it resulted in a simpler expression. If so, return
462 // that expression.
463 // TODO: Once finished, this should not take an Instruction, we only
464 // use it for printing.
465 const Expression *NewGVN::checkSimplificationResults(Expression *E,
466 Instruction *I, Value *V) {
467 if (!V)
468 return nullptr;
469 if (auto *C = dyn_cast(V)) {
470 if (I)
471 DEBUG(dbgs() << "Simplified " << *I << " to "
472 << " constant " << *C << "\n");
473 NumGVNOpsSimplified++;
474 assert(isa(E) &&
475 "We should always have had a basic expression here");
476
477 cast(E)->deallocateOperands(ArgRecycler);
478 ExpressionAllocator.Deallocate(E);
479 return createConstantExpression(C);
480 } else if (isa(V) || isa(V)) {
481 if (I)
482 DEBUG(dbgs() << "Simplified " << *I << " to "
483 << " variable " << *V << "\n");
484 cast(E)->deallocateOperands(ArgRecycler);
485 ExpressionAllocator.Deallocate(E);
486 return createVariableExpression(V);
487 }
488
489 CongruenceClass *CC = ValueToClass.lookup(V);
490 if (CC && CC->DefiningExpr) {
491 if (I)
492 DEBUG(dbgs() << "Simplified " << *I << " to "
493 << " expression " << *V << "\n");
494 NumGVNOpsSimplified++;
495 assert(isa(E) &&
496 "We should always have had a basic expression here");
497 cast(E)->deallocateOperands(ArgRecycler);
498 ExpressionAllocator.Deallocate(E);
499 return CC->DefiningExpr;
500 }
501 return nullptr;
502 }
503
504 const Expression *NewGVN::createExpression(Instruction *I,
505 const BasicBlock *B) {
506
507 BasicExpression *E =
508 new (ExpressionAllocator) BasicExpression(I->getNumOperands());
509
510 bool AllConstant = setBasicExpressionInfo(I, E, B);
511
512 if (I->isCommutative()) {
513 // Ensure that commutative instructions that only differ by a permutation
514 // of their operands get the same value number by sorting the operand value
515 // numbers. Since all commutative instructions have two operands it is more
516 // efficient to sort by hand rather than using, say, std::sort.
517 assert(I->getNumOperands() == 2 && "Unsupported commutative instruction!");
518 if (E->getOperand(0) > E->getOperand(1))
519 E->swapOperands(0, 1);
520 }
521
522 // Perform simplificaiton
523 // TODO: Right now we only check to see if we get a constant result.
524 // We may get a less than constant, but still better, result for
525 // some operations.
526 // IE
527 // add 0, x -> x
528 // and x, x -> x
529 // We should handle this by simply rewriting the expression.
530 if (auto *CI = dyn_cast(I)) {
531 // Sort the operand value numbers so xx get the same value
532 // number.
533 CmpInst::Predicate Predicate = CI->getPredicate();
534 if (E->getOperand(0) > E->getOperand(1)) {
535 E->swapOperands(0, 1);
536 Predicate = CmpInst::getSwappedPredicate(Predicate);
537 }
538 E->setOpcode((CI->getOpcode() << 8) | Predicate);
539 // TODO: 25% of our time is spent in SimplifyCmpInst with pointer operands
540 // TODO: Since we noop bitcasts, we may need to check types before
541 // simplifying, so that we don't end up simplifying based on a wrong
542 // type assumption. We should clean this up so we can use constants of the
543 // wrong type
544
545 assert(I->getOperand(0)->getType() == I->getOperand(1)->getType() &&
546 "Wrong types on cmp instruction");
547 if ((E->getOperand(0)->getType() == I->getOperand(0)->getType() &&
548 E->getOperand(1)->getType() == I->getOperand(1)->getType())) {
549 Value *V = SimplifyCmpInst(Predicate, E->getOperand(0), E->getOperand(1),
550 *DL, TLI, DT, AC);
551 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
552 return SimplifiedE;
553 }
554 } else if (isa(I)) {
555 if (isa(E->getOperand(0)) ||
556 (E->getOperand(1)->getType() == I->getOperand(1)->getType() &&
557 E->getOperand(2)->getType() == I->getOperand(2)->getType())) {
558 Value *V = SimplifySelectInst(E->getOperand(0), E->getOperand(1),
559 E->getOperand(2), *DL, TLI, DT, AC);
560 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
561 return SimplifiedE;
562 }
563 } else if (I->isBinaryOp()) {
564 Value *V = SimplifyBinOp(E->getOpcode(), E->getOperand(0), E->getOperand(1),
565 *DL, TLI, DT, AC);
566 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
567 return SimplifiedE;
568 } else if (auto *BI = dyn_cast(I)) {
569 Value *V = SimplifyInstruction(BI, *DL, TLI, DT, AC);
570 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
571 return SimplifiedE;
572 } else if (isa(I)) {
573 Value *V = SimplifyGEPInst(E->getType(),
574 ArrayRef(E->ops_begin(), E->ops_end()),
575 *DL, TLI, DT, AC);
576 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
577 return SimplifiedE;
578 } else if (AllConstant) {
579 // We don't bother trying to simplify unless all of the operands
580 // were constant.
581 // TODO: There are a lot of Simplify*'s we could call here, if we
582 // wanted to. The original motivating case for this code was a
583 // zext i1 false to i8, which we don't have an interface to
584 // simplify (IE there is no SimplifyZExt).
585
586 SmallVector C;
587 for (Value *Arg : E->operands())
588 C.emplace_back(cast(Arg));
589
590 if (Value *V = ConstantFoldInstOperands(I, C, *DL, TLI))
591 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
592 return SimplifiedE;
593 }
594 return E;
595 }
596
597 const AggregateValueExpression *
598 NewGVN::createAggregateValueExpression(Instruction *I, const BasicBlock *B) {
599 if (auto *II = dyn_cast(I)) {
600 AggregateValueExpression *E = new (ExpressionAllocator)
601 AggregateValueExpression(I->getNumOperands(), II->getNumIndices());
602 setBasicExpressionInfo(I, E, B);
603 E->allocateIntOperands(ExpressionAllocator);
604
605 for (auto &Index : II->indices())
606 E->int_ops_push_back(Index);
607 return E;
608
609 } else if (auto *EI = dyn_cast(I)) {
610 AggregateValueExpression *E = new (ExpressionAllocator)
611 AggregateValueExpression(I->getNumOperands(), EI->getNumIndices());
612 setBasicExpressionInfo(EI, E, B);
613 E->allocateIntOperands(ExpressionAllocator);
614
615 for (auto &Index : EI->indices())
616 E->int_ops_push_back(Index);
617 return E;
618 }
619 llvm_unreachable("Unhandled type of aggregate value operation");
620 }
621
622 const VariableExpression *
623 NewGVN::createVariableExpression(Value *V) {
624 VariableExpression *E = new (ExpressionAllocator) VariableExpression(V);
625 E->setOpcode(V->getValueID());
626 return E;
627 }
628
629 const Expression *NewGVN::createVariableOrConstant(Value *V,
630 const BasicBlock *B) {
631 auto Leader = lookupOperandLeader(V, nullptr, B);
632 if (auto *C = dyn_cast(Leader))
633 return createConstantExpression(C);
634 return createVariableExpression(Leader);
635 }
636
637 const ConstantExpression *
638 NewGVN::createConstantExpression(Constant *C) {
639 ConstantExpression *E = new (ExpressionAllocator) ConstantExpression(C);
640 E->setOpcode(C->getValueID());
641 return E;
642 }
643
644 const CallExpression *NewGVN::createCallExpression(CallInst *CI,
645 MemoryAccess *HV,
646 const BasicBlock *B) {
647 // FIXME: Add operand bundles for calls.
648 CallExpression *E =
649 new (ExpressionAllocator) CallExpression(CI->getNumOperands(), CI, HV);
650 setBasicExpressionInfo(CI, E, B);
651 return E;
652 }
653
654 // See if we have a congruence class and leader for this operand, and if so,
655 // return it. Otherwise, return the operand itself.
656 template
657 Value *NewGVN::lookupOperandLeader(Value *V, const User *U,
658 const T &B) const {
659 CongruenceClass *CC = ValueToClass.lookup(V);
660 if (CC && (CC != InitialClass))
661 return CC->RepLeader;
662 return V;
663 }
664
665 LoadExpression *NewGVN::createLoadExpression(Type *LoadType, Value *PointerOp,
666 LoadInst *LI, MemoryAccess *DA,
667 const BasicBlock *B) {
668 LoadExpression *E = new (ExpressionAllocator) LoadExpression(1, LI, DA);
669 E->allocateOperands(ArgRecycler, ExpressionAllocator);
670 E->setType(LoadType);
671
672 // Give store and loads same opcode so they value number together.
673 E->setOpcode(0);
674 auto Operand = lookupOperandLeader(PointerOp, LI, B);
675 E->ops_push_back(Operand);
676 if (LI)
677 E->setAlignment(LI->getAlignment());
678
679 // TODO: Value number heap versions. We may be able to discover
680 // things alias analysis can't on it's own (IE that a store and a
681 // load have the same value, and thus, it isn't clobbering the load).
682 return E;
683 }
684
685 const StoreExpression *NewGVN::createStoreExpression(StoreInst *SI,
686 MemoryAccess *DA,
687 const BasicBlock *B) {
688 StoreExpression *E =
689 new (ExpressionAllocator) StoreExpression(SI->getNumOperands(), SI, DA);
690 E->allocateOperands(ArgRecycler, ExpressionAllocator);
691 E->setType(SI->getValueOperand()->getType());
692
693 // Give store and loads same opcode so they value number together.
694 E->setOpcode(0);
695 E->ops_push_back(lookupOperandLeader(SI->getPointerOperand(), SI, B));
696
697 // TODO: Value number heap versions. We may be able to discover
698 // things alias analysis can't on it's own (IE that a store and a
699 // load have the same value, and thus, it isn't clobbering the load).
700 return E;
701 }
702
703 const Expression *NewGVN::performSymbolicStoreEvaluation(Instruction *I,
704 const BasicBlock *B) {
705 StoreInst *SI = cast(I);
706 const Expression *E = createStoreExpression(SI, MSSA->getMemoryAccess(SI), B);
707 return E;
708 }
709
710 const Expression *NewGVN::performSymbolicLoadEvaluation(Instruction *I,
711 const BasicBlock *B) {
712 LoadInst *LI = cast(I);
713
714 // We can eliminate in favor of non-simple loads, but we won't be able to
715 // eliminate them.
716 if (!LI->isSimple())
717 return nullptr;
718
719 Value *LoadAddressLeader =
720 lookupOperandLeader(LI->getPointerOperand(), I, B);
721 // Load of undef is undef.
722 if (isa(LoadAddressLeader))
723 return createConstantExpression(UndefValue::get(LI->getType()));
724
725 MemoryAccess *DefiningAccess = MSSAWalker->getClobberingMemoryAccess(I);
726
727 if (!MSSA->isLiveOnEntryDef(DefiningAccess)) {
728 if (auto *MD = dyn_cast(DefiningAccess)) {
729 Instruction *DefiningInst = MD->getMemoryInst();
730 // If the defining instruction is not reachable, replace with undef.
731 if (!ReachableBlocks.count(DefiningInst->getParent()))
732 return createConstantExpression(UndefValue::get(LI->getType()));
733 }
734 }
735
736 const Expression *E = createLoadExpression(
737 LI->getType(), LI->getPointerOperand(), LI, DefiningAccess, B);
738 return E;
739 }
740
741 // Evaluate read only and pure calls, and create an expression result.
742 const Expression *NewGVN::performSymbolicCallEvaluation(Instruction *I,
743 const BasicBlock *B) {
744 CallInst *CI = cast(I);
745 if (AA->doesNotAccessMemory(CI))
746 return createCallExpression(CI, nullptr, B);
747 else if (AA->onlyReadsMemory(CI))
748 return createCallExpression(CI, MSSAWalker->getClobberingMemoryAccess(CI),
749 B);
750 else
751 return nullptr;
752 }
753
754 // Evaluate PHI nodes symbolically, and create an expression result.
755 const Expression *NewGVN::performSymbolicPHIEvaluation(Instruction *I,
756 const BasicBlock *B) {
757 PHIExpression *E = cast(createPHIExpression(I));
758 if (E->ops_empty()) {
759 DEBUG(dbgs() << "Simplified PHI node " << *I << " to undef"
760 << "\n");
761 E->deallocateOperands(ArgRecycler);
762 ExpressionAllocator.Deallocate(E);
763 return createConstantExpression(UndefValue::get(I->getType()));
764 }
765
766 Value *AllSameValue = E->getOperand(0);
767
768 // See if all arguments are the same, ignoring undef arguments, because we can
769 // choose a value that is the same for them.
770 for (const Value *Arg : E->operands())
771 if (Arg != AllSameValue && !isa(Arg)) {
772 AllSameValue = NULL;
773 break;
774 }
775
776 if (AllSameValue) {
777 // It's possible to have phi nodes with cycles (IE dependent on
778 // other phis that are .... dependent on the original phi node),
779 // especially in weird CFG's where some arguments are unreachable, or
780 // uninitialized along certain paths.
781 // This can cause infinite loops during evaluation (even if you disable
782 // the recursion below, you will simply ping-pong between congruence
783 // classes). If a phi node symbolically evaluates to another phi node,
784 // just leave it alone. If they are really the same, we will still
785 // eliminate them in favor of each other.
786 if (isa(AllSameValue))
787 return E;
788 NumGVNPhisAllSame++;
789 DEBUG(dbgs() << "Simplified PHI node " << *I << " to " << *AllSameValue
790 << "\n");
791 E->deallocateOperands(ArgRecycler);
792 ExpressionAllocator.Deallocate(E);
793 if (auto *C = dyn_cast(AllSameValue))
794 return createConstantExpression(C);
795 return createVariableExpression(AllSameValue);
796 }
797 return E;
798 }
799
800 const Expression *
801 NewGVN::performSymbolicAggrValueEvaluation(Instruction *I,
802 const BasicBlock *B) {
803 if (auto *EI = dyn_cast(I)) {
804 auto *II = dyn_cast(EI->getAggregateOperand());
805 if (II && EI->getNumIndices() == 1 && *EI->idx_begin() == 0) {
806 unsigned Opcode = 0;
807 // EI might be an extract from one of our recognised intrinsics. If it
808 // is we'll synthesize a semantically equivalent expression instead on
809 // an extract value expression.
810 switch (II->getIntrinsicID()) {
811 case Intrinsic::sadd_with_overflow:
812 case Intrinsic::uadd_with_overflow:
813 Opcode = Instruction::Add;
814 break;
815 case Intrinsic::ssub_with_overflow:
816 case Intrinsic::usub_with_overflow:
817 Opcode = Instruction::Sub;
818 break;
819 case Intrinsic::smul_with_overflow:
820 case Intrinsic::umul_with_overflow:
821 Opcode = Instruction::Mul;
822 break;
823 default:
824 break;
825 }
826
827 if (Opcode != 0) {
828 // Intrinsic recognized. Grab its args to finish building the
829 // expression.
830 assert(II->getNumArgOperands() == 2 &&
831 "Expect two args for recognised intrinsics.");
832 return createBinaryExpression(Opcode, EI->getType(),
833 II->getArgOperand(0),
834 II->getArgOperand(1), B);
835 }
836 }
837 }
838
839 return createAggregateValueExpression(I, B);
840 }
841
842 // Substitute and symbolize the value before value numbering.
843 const Expression *NewGVN::performSymbolicEvaluation(Value *V,
844 const BasicBlock *B) {
845 const Expression *E = NULL;
846 if (auto *C = dyn_cast(V))
847 E = createConstantExpression(C);
848 else if (isa(V) || isa(V)) {
849 E = createVariableExpression(V);
850 } else {
851 // TODO: memory intrinsics.
852 // TODO: Some day, we should do the forward propagation and reassociation
853 // parts of the algorithm.
854 Instruction *I = cast(V);
855 switch (I->getOpcode()) {
856 case Instruction::ExtractValue:
857 case Instruction::InsertValue:
858 E = performSymbolicAggrValueEvaluation(I, B);
859 break;
860 case Instruction::PHI:
861 E = performSymbolicPHIEvaluation(I, B);
862 break;
863 case Instruction::Call:
864 E = performSymbolicCallEvaluation(I, B);
865 break;
866 case Instruction::Store:
867 E = performSymbolicStoreEvaluation(I, B);
868 break;
869 case Instruction::Load:
870 E = performSymbolicLoadEvaluation(I, B);
871 break;
872 case Instruction::BitCast: {
873 E = createExpression(I, B);
874 } break;
875
876 case Instruction::Add:
877 case Instruction::FAdd:
878 case Instruction::Sub:
879 case Instruction::FSub:
880 case Instruction::Mul:
881 case Instruction::FMul:
882 case Instruction::UDiv:
883 case Instruction::SDiv:
884 case Instruction::FDiv:
885 case Instruction::URem:
886 case Instruction::SRem:
887 case Instruction::FRem:
888 case Instruction::Shl:
889 case Instruction::LShr:
890 case Instruction::AShr:
891 case Instruction::And:
892 case Instruction::Or:
893 case Instruction::Xor:
894 case Instruction::ICmp:
895 case Instruction::FCmp:
896 case Instruction::Trunc:
897 case Instruction::ZExt:
898 case Instruction::SExt:
899 case Instruction::FPToUI:
900 case Instruction::FPToSI:
901 case Instruction::UIToFP:
902 case Instruction::SIToFP:
903 case Instruction::FPTrunc:
904 case Instruction::FPExt:
905 case Instruction::PtrToInt:
906 case Instruction::IntToPtr:
907 case Instruction::Select:
908 case Instruction::ExtractElement:
909 case Instruction::InsertElement:
910 case Instruction::ShuffleVector:
911 case Instruction::GetElementPtr:
912 E = createExpression(I, B);
913 break;
914 default:
915 return nullptr;
916 }
917 }
918 if (!E)
919 return nullptr;
920 return E;
921 }
922
923 // There is an edge from 'Src' to 'Dst'. Return true if every path from
924 // the entry block to 'Dst' passes via this edge. In particular 'Dst'
925 // must not be reachable via another edge from 'Src'.
926 bool NewGVN::isOnlyReachableViaThisEdge(const BasicBlockEdge &E) {
927
928 // While in theory it is interesting to consider the case in which Dst has
929 // more than one predecessor, because Dst might be part of a loop which is
930 // only reachable from Src, in practice it is pointless since at the time
931 // GVN runs all such loops have preheaders, which means that Dst will have
932 // been changed to have only one predecessor, namely Src.
933 const BasicBlock *Pred = E.getEnd()->getSinglePredecessor();
934 const BasicBlock *Src = E.getStart();
935 assert((!Pred || Pred == Src) && "No edge between these basic blocks!");
936 (void)Src;
937 return Pred != nullptr;
938 }
939
940 void NewGVN::markUsersTouched(Value *V) {
941 // Now mark the users as touched.
942 for (auto &U : V->uses()) {
943 auto *User = dyn_cast(U.getUser());
944 assert(User && "Use of value not within an instruction?");
945 TouchedInstructions.set(InstrDFS[User]);
946 }
947 }
948
949 void NewGVN::markMemoryUsersTouched(MemoryAccess *MA) {
950 for (auto U : MA->users()) {
951 if (auto *MUD = dyn_cast(U))
952 TouchedInstructions.set(InstrDFS[MUD->getMemoryInst()]);
953 else
954 TouchedInstructions.set(InstrDFS[MA]);
955 }
956 }
957
958 // Perform congruence finding on a given value numbering expression.
959 void NewGVN::performCongruenceFinding(Value *V, const Expression *E) {
960
961 ValueToExpression[V] = E;
962 // This is guaranteed to return something, since it will at least find
963 // INITIAL.
964 CongruenceClass *VClass = ValueToClass[V];
965 assert(VClass && "Should have found a vclass");
966 // Dead classes should have been eliminated from the mapping.
967 assert(!VClass->Dead && "Found a dead class");
968
969 CongruenceClass *EClass;
970 // Expressions we can't symbolize are always in their own unique
971 // congruence class.
972 if (E == NULL) {
973 // We may have already made a unique class.
974 if (VClass->Members.size() != 1 || VClass->RepLeader != V) {
975 CongruenceClass *NewClass = createCongruenceClass(V, NULL);
976 // We should always be adding the member in the below code.
977 EClass = NewClass;
978 DEBUG(dbgs() << "Created new congruence class for " << *V
979 << " due to NULL expression\n");
980 } else {
981 EClass = VClass;
982 }
983 } else if (const auto *VE = dyn_cast(E)) {
984 EClass = ValueToClass[VE->getVariableValue()];
985 } else {
986 auto lookupResult = ExpressionToClass.insert({E, nullptr});
987
988 // If it's not in the value table, create a new congruence class.
989 if (lookupResult.second) {
990 CongruenceClass *NewClass = createCongruenceClass(NULL, E);
991 auto place = lookupResult.first;
992 place->second = NewClass;
993
994 // Constants and variables should always be made the leader.
995 if (const auto *CE = dyn_cast(E))
996 NewClass->RepLeader = CE->getConstantValue();
997 else if (const auto *VE = dyn_cast(E))
998 NewClass->RepLeader = VE->getVariableValue();
999 else if (const auto *SE = dyn_cast(E))
1000 NewClass->RepLeader = SE->getStoreInst()->getValueOperand();
1001 else
1002 NewClass->RepLeader = V;
1003
1004 EClass = NewClass;
1005 DEBUG(dbgs() << "Created new congruence class for " << *V
1006 << " using expression " << *E << " at " << NewClass->ID
1007 << "\n");
1008 DEBUG(dbgs() << "Hash value was " << E->getHashValue() << "\n");
1009 } else {
1010 EClass = lookupResult.first->second;
1011 assert(EClass && "Somehow don't have an eclass");
1012
1013 assert(!EClass->Dead && "We accidentally looked up a dead class");
1014 }
1015 }
1016 bool WasInChanged = ChangedValues.erase(V);
1017 if (VClass != EClass || WasInChanged) {
1018 DEBUG(dbgs() << "Found class " << EClass->ID << " for expression " << E
1019 << "\n");
1020
1021 if (VClass != EClass) {
1022 DEBUG(dbgs() << "New congruence class for " << V << " is " << EClass->ID
1023 << "\n");
1024
1025 VClass->Members.erase(V);
1026 EClass->Members.insert(V);
1027 ValueToClass[V] = EClass;
1028 // See if we destroyed the class or need to swap leaders.
1029 if (VClass->Members.empty() && VClass != InitialClass) {
1030 if (VClass->DefiningExpr) {
1031 VClass->Dead = true;
1032 DEBUG(dbgs() << "Erasing expression " << *E << " from table\n");
1033 ExpressionToClass.erase(VClass->DefiningExpr);
1034 }
1035 } else if (VClass->RepLeader == V) {
1036 // FIXME: When the leader changes, the value numbering of
1037 // everything may change, so we need to reprocess.
1038 VClass->RepLeader = *(VClass->Members.begin());
1039 for (auto M : VClass->Members) {
1040 if (auto *I = dyn_cast(M))
1041 TouchedInstructions.set(InstrDFS[I]);
1042 ChangedValues.insert(M);
1043 }
1044 }
1045 }
1046 markUsersTouched(V);
1047 if (Instruction *I = dyn_cast(V))
1048 if (MemoryAccess *MA = MSSA->getMemoryAccess(I))
1049 markMemoryUsersTouched(MA);
1050 }
1051 }
1052
1053 // Process the fact that Edge (from, to) is reachable, including marking
1054 // any newly reachable blocks and instructions for processing.
1055 void NewGVN::updateReachableEdge(BasicBlock *From, BasicBlock *To) {
1056 // Check if the Edge was reachable before.
1057 if (ReachableEdges.insert({From, To}).second) {
1058 // If this block wasn't reachable before, all instructions are touched.
1059 if (ReachableBlocks.insert(To).second) {
1060 DEBUG(dbgs() << "Block " << getBlockName(To) << " marked reachable\n");
1061 const auto &InstRange = BlockInstRange.lookup(To);
1062 TouchedInstructions.set(InstRange.first, InstRange.second);
1063 } else {
1064 DEBUG(dbgs() << "Block " << getBlockName(To)
1065 << " was reachable, but new edge {" << getBlockName(From)
1066 << "," << getBlockName(To) << "} to it found\n");
1067
1068 // We've made an edge reachable to an existing block, which may
1069 // impact predicates. Otherwise, only mark the phi nodes as touched, as
1070 // they are the only thing that depend on new edges. Anything using their
1071 // values will get propagated to if necessary.
1072 auto BI = To->begin();
1073 while (isa(BI)) {
1074 TouchedInstructions.set(InstrDFS[&*BI]);
1075 ++BI;
1076 }
1077 }
1078 }
1079 }
1080
1081 // Given a predicate condition (from a switch, cmp, or whatever) and a block,
1082 // see if we know some constant value for it already.
1083 Value *NewGVN::findConditionEquivalence(Value *Cond, BasicBlock *B) const {
1084 auto Result = lookupOperandLeader(Cond, nullptr, B);
1085 if (isa(Result))
1086 return Result;
1087 return nullptr;
1088 }
1089
1090 // Process the outgoing edges of a block for reachability.
1091 void NewGVN::processOutgoingEdges(TerminatorInst *TI, BasicBlock *B) {
1092 // Evaluate reachability of terminator instruction.
1093 BranchInst *BR;
1094 if ((BR = dyn_cast(TI)) && BR->isConditional()) {
1095 Value *Cond = BR->getCondition();
1096 Value *CondEvaluated = findConditionEquivalence(Cond, B);
1097 if (!CondEvaluated) {
1098 if (auto *I = dyn_cast(Cond)) {
1099 const Expression *E = createExpression(I, B);
1100 if (const auto *CE = dyn_cast(E)) {
1101 CondEvaluated = CE->getConstantValue();
1102 }
1103 } else if (isa(Cond)) {
1104 CondEvaluated = Cond;
1105 }
1106 }
1107 ConstantInt *CI;
1108 BasicBlock *TrueSucc = BR->getSuccessor(0);
1109 BasicBlock *FalseSucc = BR->getSuccessor(1);
1110 if (CondEvaluated && (CI = dyn_cast(CondEvaluated))) {
1111 if (CI->isOne()) {
1112 DEBUG(dbgs() << "Condition for Terminator " << *TI
1113 << " evaluated to true\n");
1114 updateReachableEdge(B, TrueSucc);
1115 } else if (CI->isZero()) {
1116 DEBUG(dbgs() << "Condition for Terminator " << *TI
1117 << " evaluated to false\n");
1118 updateReachableEdge(B, FalseSucc);
1119 }
1120 } else {
1121 updateReachableEdge(B, TrueSucc);
1122 updateReachableEdge(B, FalseSucc);
1123 }
1124 } else if (auto *SI = dyn_cast(TI)) {
1125 // For switches, propagate the case values into the case
1126 // destinations.
1127
1128 // Remember how many outgoing edges there are to every successor.
1129 SmallDenseMap SwitchEdges;
1130
1131 bool MultipleEdgesOneReachable = false;
1132 Value *SwitchCond = SI->getCondition();
1133 Value *CondEvaluated = findConditionEquivalence(SwitchCond, B);
1134 // See if we were able to turn this switch statement into a constant.
1135 if (CondEvaluated && isa(CondEvaluated)) {
1136 ConstantInt *CondVal = cast(CondEvaluated);
1137 // We should be able to get case value for this.
1138 auto CaseVal = SI->findCaseValue(CondVal);
1139 if (CaseVal.getCaseSuccessor() == SI->getDefaultDest()) {
1140 // We proved the value is outside of the range of the case.
1141 // We can't do anything other than mark the default dest as reachable,
1142 // and go home.
1143 updateReachableEdge(B, SI->getDefaultDest());
1144 return;
1145 }
1146 // Now get where it goes and mark it reachable.
1147 BasicBlock *TargetBlock = CaseVal.getCaseSuccessor();
1148 updateReachableEdge(B, TargetBlock);
1149 unsigned WhichSucc = CaseVal.getSuccessorIndex();
1150 // Calculate whether our single reachable edge is really a single edge to
1151 // the target block. If not, and the block has multiple predecessors, we
1152 // can only replace phi node values.
1153 for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
1154 if (i == WhichSucc)
1155 continue;
1156 BasicBlock *Block = SI->getSuccessor(i);
1157 if (Block == TargetBlock)
1158 MultipleEdgesOneReachable = true;
1159 }
1160 } else {
1161 for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
1162 BasicBlock *TargetBlock = SI->getSuccessor(i);
1163 ++SwitchEdges[TargetBlock];
1164 updateReachableEdge(B, TargetBlock);
1165 }
1166 }
1167 } else {
1168 // Otherwise this is either unconditional, or a type we have no
1169 // idea about. Just mark successors as reachable.
1170 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1171 BasicBlock *TargetBlock = TI->getSuccessor(i);
1172 updateReachableEdge(B, TargetBlock);
1173 }
1174 }
1175 }
1176
1177 // The algorithm initially places the values of the routine in the INITIAL congruence
1178 // class. The leader of INITIAL is the undetermined value `TOP`.
1179 // When the algorithm has finished, values still in INITIAL are unreachable.
1180 void NewGVN::initializeCongruenceClasses(Function &F) {
1181 // FIXME now i can't remember why this is 2
1182 NextCongruenceNum = 2;
1183 // Initialize all other instructions to be in INITIAL class.
1184 CongruenceClass::MemberSet InitialValues;
1185 for (auto &B : F)
1186 for (auto &I : B)
1187 InitialValues.insert(&I);
1188
1189 InitialClass = createCongruenceClass(NULL, NULL);
1190 for (auto L : InitialValues)
1191 ValueToClass[L] = InitialClass;
1192 InitialClass->Members.swap(InitialValues);
1193
1194 // Initialize arguments to be in their own unique congruence classes
1195 for (auto &FA : F.args())
1196 createSingletonCongruenceClass(&FA);
1197 }
1198
1199 void NewGVN::cleanupTables() {
1200 for (unsigned i = 0, e = CongruenceClasses.size(); i != e; ++i) {
1201 DEBUG(dbgs() << "Congruence class " << CongruenceClasses[i]->ID << " has "
1202 << CongruenceClasses[i]->Members.size() << " members\n");
1203 // Make sure we delete the congruence class (probably worth switching to
1204 // a unique_ptr at some point.
1205 delete CongruenceClasses[i];
1206 CongruenceClasses[i] = NULL;
1207 }
1208
1209 ValueToClass.clear();
1210 ArgRecycler.clear(ExpressionAllocator);
1211 ExpressionAllocator.Reset();
1212 CongruenceClasses.clear();
1213 ExpressionToClass.clear();
1214 ValueToExpression.clear();
1215 ReachableBlocks.clear();
1216 ReachableEdges.clear();
1217 #ifndef NDEBUG
1218 ProcessedCount.clear();
1219 #endif
1220 DFSDomMap.clear();
1221 InstrDFS.clear();
1222 InstructionsToErase.clear();
1223
1224 DFSToInstr.clear();
1225 BlockInstRange.clear();
1226 TouchedInstructions.clear();
1227 DominatedInstRange.clear();
1228 }
1229
1230 std::pair NewGVN::assignDFSNumbers(BasicBlock *B,
1231 unsigned Start) {
1232 unsigned End = Start;
1233 for (auto &I : *B) {
1234 InstrDFS[&I] = End++;
1235 DFSToInstr.emplace_back(&I);
1236 }
1237
1238 // All of the range functions taken half-open ranges (open on the end side).
1239 // So we do not subtract one from count, because at this point it is one
1240 // greater than the last instruction.
1241 return std::make_pair(Start, End);
1242 }
1243
1244 void NewGVN::updateProcessedCount(Value *V) {
1245 #ifndef NDEBUG
1246 if (ProcessedCount.count(V) == 0) {
1247 ProcessedCount.insert({V, 1});
1248 } else {
1249 ProcessedCount[V] += 1;
1250 assert(ProcessedCount[V] < 100 &&
1251 "Seem to have processed the same Value a lot\n");
1252 }
1253 #endif
1254 }
1255
1256 // This is the main transformation entry point.
1257 bool NewGVN::runGVN(Function &F, DominatorTree *_DT, AssumptionCache *_AC,
1258 TargetLibraryInfo *_TLI, AliasAnalysis *_AA,
1259 MemorySSA *_MSSA) {
1260 bool Changed = false;
1261 DT = _DT;
1262 AC = _AC;
1263 TLI = _TLI;
1264 AA = _AA;
1265 MSSA = _MSSA;
1266 DL = &F.getParent()->getDataLayout();
1267 MSSAWalker = MSSA->getWalker();
1268
1269 // Count number of instructions for sizing of hash tables, and come
1270 // up with a global dfs numbering for instructions.
1271 unsigned ICount = 0;
1272 SmallPtrSet VisitedBlocks;
1273
1274 // Note: We want RPO traversal of the blocks, which is not quite the same as
1275 // dominator tree order, particularly with regard whether backedges get
1276 // visited first or second, given a block with multiple successors.
1277 // If we visit in the wrong order, we will end up performing N times as many
1278 // iterations.
1279 ReversePostOrderTraversal RPOT(&F);
1280 for (auto &B : RPOT) {
1281 VisitedBlocks.insert(B);
1282 const auto &BlockRange = assignDFSNumbers(B, ICount);
1283 BlockInstRange.insert({B, BlockRange});
1284 ICount += BlockRange.second - BlockRange.first;
1285 }
1286
1287 // Handle forward unreachable blocks and figure out which blocks
1288 // have single preds.
1289 for (auto &B : F) {
1290 // Assign numbers to unreachable blocks.
1291 if (!VisitedBlocks.count(&B)) {
1292 const auto &BlockRange = assignDFSNumbers(&B, ICount);
1293 BlockInstRange.insert({&B, BlockRange});
1294 ICount += BlockRange.second - BlockRange.first;
1295 }
1296 }
1297
1298 TouchedInstructions.resize(ICount + 1);
1299 DominatedInstRange.reserve(F.size());
1300 // Ensure we don't end up resizing the expressionToClass map, as
1301 // that can be quite expensive. At most, we have one expression per
1302 // instruction.
1303 ExpressionToClass.reserve(ICount + 1);
1304
1305 // Initialize the touched instructions to include the entry block.
1306 const auto &InstRange = BlockInstRange.lookup(&F.getEntryBlock());
1307 TouchedInstructions.set(InstRange.first, InstRange.second);
1308 ReachableBlocks.insert(&F.getEntryBlock());
1309
1310 initializeCongruenceClasses(F);
1311
1312 // We start out in the entry block.
1313 BasicBlock *LastBlock = &F.getEntryBlock();
1314 while (TouchedInstructions.any()) {
1315 // Walk through all the instructions in all the blocks in RPO.
1316 for (int InstrNum = TouchedInstructions.find_first(); InstrNum != -1;
1317 InstrNum = TouchedInstructions.find_next(InstrNum)) {
1318 Instruction *I = DFSToInstr[InstrNum];
1319 BasicBlock *CurrBlock = I->getParent();
1320
1321 // If we hit a new block, do reachability processing.
1322 if (CurrBlock != LastBlock) {
1323 LastBlock = CurrBlock;
1324 bool BlockReachable = ReachableBlocks.count(CurrBlock);
1325 const auto &CurrInstRange = BlockInstRange.lookup(CurrBlock);
1326
1327 // If it's not reachable, erase any touched instructions and move on.
1328 if (!BlockReachable) {
1329 TouchedInstructions.reset(CurrInstRange.first, CurrInstRange.second);
1330 DEBUG(dbgs() << "Skipping instructions in block "
1331 << getBlockName(CurrBlock)
1332 << " because it is unreachable\n");
1333 continue;
1334 }
1335 updateProcessedCount(CurrBlock);
1336 }
1337 DEBUG(dbgs() << "Processing instruction " << *I << "\n");
1338 if (I->use_empty() && !I->getType()->isVoidTy()) {
1339 DEBUG(dbgs() << "Skipping unused instruction\n");
1340 if (isInstructionTriviallyDead(I, TLI))
1341 markInstructionForDeletion(I);
1342 TouchedInstructions.reset(InstrNum);
1343 continue;
1344 }
1345 updateProcessedCount(I);
1346
1347 if (!I->isTerminator()) {
1348 const Expression *Symbolized = performSymbolicEvaluation(I, CurrBlock);
1349 performCongruenceFinding(I, Symbolized);
1350 } else {
1351 processOutgoingEdges(dyn_cast(I), CurrBlock);
1352 }
1353 // Reset after processing (because we may mark ourselves as touched when
1354 // we propagate equalities).
1355 TouchedInstructions.reset(InstrNum);
1356 }
1357 }
1358
1359 Changed |= eliminateInstructions(F);
1360
1361 // Delete all instructions marked for deletion.
1362 for (Instruction *ToErase : InstructionsToErase) {
1363 if (!ToErase->use_empty())
1364 ToErase->replaceAllUsesWith(UndefValue::get(ToErase->getType()));
1365
1366 ToErase->eraseFromParent();
1367 }
1368
1369 // Delete all unreachable blocks.
1370 for (auto &B : F) {
1371 BasicBlock *BB = &B;
1372 if (!ReachableBlocks.count(BB)) {
1373 DEBUG(dbgs() << "We believe block " << getBlockName(BB)
1374 << " is unreachable\n");
1375 deleteInstructionsInBlock(BB);
1376 Changed = true;
1377 }
1378 }
1379
1380 cleanupTables();
1381 return Changed;
1382 }
1383
1384 bool NewGVN::runOnFunction(Function &F) {
1385 if (skipFunction(F))
1386 return false;
1387 return runGVN(F, &getAnalysis().getDomTree(),
1388 &getAnalysis().getAssumptionCache(F),
1389 &getAnalysis().getTLI(),
1390 &getAnalysis().getAAResults(),
1391 &getAnalysis().getMSSA());
1392 }
1393
1394 PreservedAnalyses NewGVNPass::run(Function &F,
1395 AnalysisManager &AM) {
1396 NewGVN Impl;
1397
1398 // Apparently the order in which we get these results matter for
1399 // the old GVN (see Chandler's comment in GVN.cpp). I'll keep
1400 // the same order here, just in case.
1401 auto &AC = AM.getResult(F);
1402 auto &DT = AM.getResult(F);
1403 auto &TLI = AM.getResult(F);
1404 auto &AA = AM.getResult(F);
1405 auto &MSSA = AM.getResult(F).getMSSA();
1406 bool Changed = Impl.runGVN(F, &DT, &AC, &TLI, &AA, &MSSA);
1407 if (!Changed)
1408 return PreservedAnalyses::all();
1409 PreservedAnalyses PA;
1410 PA.preserve();
1411 PA.preserve();
1412 return PA;
1413 }
1414
1415 // Return true if V is a value that will always be available (IE can
1416 // be placed anywhere) in the function. We don't do globals here
1417 // because they are often worse to put in place.
1418 // TODO: Separate cost from availability
1419 static bool alwaysAvailable(Value *V) {
1420 return isa(V) || isa(V);
1421 }
1422
1423 // Get the basic block from an instruction/value.
1424 static BasicBlock *getBlockForValue(Value *V) {
1425 if (auto *I = dyn_cast(V))
1426 return I->getParent();
1427 return nullptr;
1428 }
1429
1430 struct NewGVN::ValueDFS {
1431 int DFSIn;
1432 int DFSOut;
1433 int LocalNum;
1434 // Only one of these will be set.
1435 Value *Val;
1436 Use *U;
1437 ValueDFS()
1438 : DFSIn(0), DFSOut(0), LocalNum(0), Val(nullptr), U(nullptr) {}
1439
1440 bool operator<(const ValueDFS &Other) const {
1441 // It's not enough that any given field be less than - we have sets
1442 // of fields that need to be evaluated together to give a proper ordering.
1443 // For example, if you have;
1444 // DFS (1, 3)
1445 // Val 0
1446 // DFS (1, 2)
1447 // Val 50
1448 // We want the second to be less than the first, but if we just go field
1449 // by field, we will get to Val 0 < Val 50 and say the first is less than
1450 // the second. We only want it to be less than if the DFS orders are equal.
1451 //
1452 // Each LLVM instruction only produces one value, and thus the lowest-level
1453 // differentiator that really matters for the stack (and what we use as as a
1454 // replacement) is the local dfs number.
1455 // Everything else in the structure is instruction level, and only affects the
1456 // order in which we will replace operands of a given instruction.
1457 //
1458 // For a given instruction (IE things with equal dfsin, dfsout, localnum),
1459 // the order of replacement of uses does not matter.
1460 // IE given,
1461 // a = 5
1462 // b = a + a
1463 // When you hit b, you will have two valuedfs with the same dfsin, out, and localnum.
1464 // The .val will be the same as well.
1465 // The .u's will be different.
1466 // You will replace both, and it does not matter what order you replace them in
1467 // (IE whether you replace operand 2, then operand 1, or operand 1, then operand 2).
1468 // Similarly for the case of same dfsin, dfsout, localnum, but different .val's
1469 // a = 5
1470 // b = 6
1471 // c = a + b
1472 // in c, we will a valuedfs for a, and one for b,with everything the same but
1473 // .val and .u.
1474 // It does not matter what order we replace these operands in.
1475 // You will always end up with the same IR, and this is guaranteed.
1476 return std::tie(DFSIn, DFSOut, LocalNum, Val, U) <
1477 std::tie(Other.DFSIn, Other.DFSOut, Other.LocalNum, Other.Val,
1478 Other.U);
1479 }
1480 };
1481
1482 void NewGVN::convertDenseToDFSOrdered(CongruenceClass::MemberSet &Dense,
1483 std::vector &DFSOrderedSet) {
1484 for (auto D : Dense) {
1485 // First add the value.
1486 BasicBlock *BB = getBlockForValue(D);
1487 // Constants are handled prior to ever calling this function, so
1488 // we should only be left with instructions as members.
1489 assert(BB || "Should have figured out a basic block for value");
1490 ValueDFS VD;
1491
1492 std::pair DFSPair = DFSDomMap[BB];
1493 assert(DFSPair.first != -1 && DFSPair.second != -1 && "Invalid DFS Pair");
1494 VD.DFSIn = DFSPair.first;
1495 VD.DFSOut = DFSPair.second;
1496 VD.Val = D;
1497 // If it's an instruction, use the real local dfs number.
1498 if (auto *I = dyn_cast(D))
1499 VD.LocalNum = InstrDFS[I];
1500 else
1501 llvm_unreachable("Should have been an instruction");
1502
1503 DFSOrderedSet.emplace_back(VD);
1504
1505 // Now add the users.
1506 for (auto &U : D->uses()) {
1507 if (auto *I = dyn_cast(U.getUser())) {
1508 ValueDFS VD;
1509 // Put the phi node uses in the incoming block.
1510 BasicBlock *IBlock;
1511 if (auto *P = dyn_cast(I)) {
1512 IBlock = P->getIncomingBlock(U);
1513 // Make phi node users appear last in the incoming block
1514 // they are from.
1515 VD.LocalNum = InstrDFS.size() + 1;
1516 } else {
1517 IBlock = I->getParent();
1518 VD.LocalNum = InstrDFS[I];
1519 }
1520 std::pair DFSPair = DFSDomMap[IBlock];
1521 VD.DFSIn = DFSPair.first;
1522 VD.DFSOut = DFSPair.second;
1523 VD.U = &U;
1524 DFSOrderedSet.emplace_back(VD);
1525 }
1526 }
1527 }
1528 }
1529
1530 static void patchReplacementInstruction(Instruction *I, Value *Repl) {
1531 // Patch the replacement so that it is not more restrictive than the value
1532 // being replaced.
1533 auto *Op = dyn_cast(I);
1534 auto *ReplOp = dyn_cast(Repl);
1535
1536 if (Op && ReplOp)
1537 ReplOp->andIRFlags(Op);
1538
1539 if (auto *ReplInst = dyn_cast(Repl)) {
1540 // FIXME: If both the original and replacement value are part of the
1541 // same control-flow region (meaning that the execution of one
1542 // guarentees the executation of the other), then we can combine the
1543 // noalias scopes here and do better than the general conservative
1544 // answer used in combineMetadata().
1545
1546 // In general, GVN unifies expressions over different control-flow
1547 // regions, and so we need a conservative combination of the noalias
1548 // scopes.
1549 unsigned KnownIDs[] = {
1550 LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
1551 LLVMContext::MD_noalias, LLVMContext::MD_range,
1552 LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load,
1553 LLVMContext::MD_invariant_group};
1554 combineMetadata(ReplInst, I, KnownIDs);
1555 }
1556 }
1557
1558 static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) {
1559 patchReplacementInstruction(I, Repl);
1560 I->replaceAllUsesWith(Repl);
1561 }
1562
1563 void NewGVN::deleteInstructionsInBlock(BasicBlock *BB) {
1564 DEBUG(dbgs() << " BasicBlock Dead:" << *BB);
1565 ++NumGVNBlocksDeleted;
1566
1567 // Check to see if there are non-terminating instructions to delete.
1568 if (isa(BB->begin()))
1569 return;
1570
1571 // Delete the instructions backwards, as it has a reduced likelihood of having
1572 // to update as many def-use and use-def chains. Start after the terminator.
1573 auto StartPoint = BB->rbegin();
1574 ++StartPoint;
1575 // Note that we explicitly recalculate BB->rend() on each iteration,
1576 // as it may change when we remove the first instruction.
1577 for (BasicBlock::reverse_iterator I(StartPoint); I != BB->rend();) {
1578 Instruction &Inst = *I++;
1579 if (!Inst.use_empty())
1580 Inst.replaceAllUsesWith(UndefValue::get(Inst.getType()));
1581 if (isa(Inst))
1582 continue;
1583
1584 Inst.eraseFromParent();
1585 ++NumGVNInstrDeleted;
1586 }
1587 }
1588
1589 void NewGVN::markInstructionForDeletion(Instruction *I) {
1590 DEBUG(dbgs() << "Marking " << *I << " for deletion\n");
1591 InstructionsToErase.insert(I);
1592 }
1593
1594 void NewGVN::replaceInstruction(Instruction *I, Value *V) {
1595
1596 DEBUG(dbgs() << "Replacing " << *I << " with " << *V << "\n");
1597 patchAndReplaceAllUsesWith(I, V);
1598 // We save the actual erasing to avoid invalidating memory
1599 // dependencies until we are done with everything.
1600 markInstructionForDeletion(I);
1601 }
1602
1603 namespace {
1604
1605 // This is a stack that contains both the value and dfs info of where
1606 // that value is valid.
1607 class ValueDFSStack {
1608 public:
1609 Value *back() const { return ValueStack.back(); }
1610 std::pair dfs_back() const { return DFSStack.back(); }
1611
1612 void push_back(Value *V, int DFSIn, int DFSOut) {
1613 ValueStack.emplace_back(V);
1614 DFSStack.emplace_back(DFSIn, DFSOut);
1615 }
1616 bool empty() const { return DFSStack.empty(); }
1617 bool isInScope(int DFSIn, int DFSOut) const {
1618 if (empty())
1619 return false;
1620 return DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second;
1621 }
1622
1623 void popUntilDFSScope(int DFSIn, int DFSOut) {
1624
1625 // These two should always be in sync at this point.
1626 assert(ValueStack.size() == DFSStack.size() &&
1627 "Mismatch between ValueStack and DFSStack");
1628 while (
1629 !DFSStack.empty() &&
1630 !(DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second)) {
1631 DFSStack.pop_back();
1632 ValueStack.pop_back();
1633 }
1634 }
1635
1636 private:
1637 SmallVector ValueStack;
1638 SmallVector, 8> DFSStack;
1639 };
1640 }
1641
1642 bool NewGVN::eliminateInstructions(Function &F) {
1643 // This is a non-standard eliminator. The normal way to eliminate is
1644 // to walk the dominator tree in order, keeping track of available
1645 // values, and eliminating them. However, this is mildly
1646 // pointless. It requires doing lookups on every instruction,
1647 // regardless of whether we will ever eliminate it. For
1648 // instructions part of most singleton congruence class, we know we
1649 // will never eliminate it.
1650
1651 // Instead, this eliminator looks at the congruence classes directly, sorts
1652 // them into a DFS ordering of the dominator tree, and then we just
1653 // perform eliminate straight on the sets by walking the congruence
1654 // class member uses in order, and eliminate the ones dominated by the
1655 // last member. This is technically O(N log N) where N = number of
1656 // instructions (since in theory all instructions may be in the same
1657 // congruence class).
1658 // When we find something not dominated, it becomes the new leader
1659 // for elimination purposes
1660
1661 bool AnythingReplaced = false;
1662
1663 // Since we are going to walk the domtree anyway, and we can't guarantee the
1664 // DFS numbers are updated, we compute some ourselves.
1665 DT->updateDFSNumbers();
1666
1667 for (auto &B : F) {
1668 if (!ReachableBlocks.count(&B)) {
1669 for (const auto S : successors(&B)) {
1670 for (auto II = S->begin(); isa(II); ++II) {
1671 PHINode &Phi = cast(*II);
1672 DEBUG(dbgs() << "Replacing incoming value of " << *II << " for block "
1673 << getBlockName(&B)
1674 << " with undef due to it being unreachable\n");
1675 for (auto &Operand : Phi.incoming_values())
1676 if (Phi.getIncomingBlock(Operand) == &B)
1677 Operand.set(UndefValue::get(Phi.getType()));
1678 }
1679 }
1680 }
1681 DomTreeNode *Node = DT->getNode(&B);
1682 if (Node)
1683 DFSDomMap[&B] = {Node->getDFSNumIn(), Node->getDFSNumOut()};
1684 }
1685
1686 for (CongruenceClass *CC : CongruenceClasses) {
1687 // FIXME: We should eventually be able to replace everything still
1688 // in the initial class with undef, as they should be unreachable.
1689 // Right now, initial still contains some things we skip value
1690 // numbering of (UNREACHABLE's, for example).
1691 if (CC == InitialClass || CC->Dead)
1692 continue;
1693 assert(CC->RepLeader && "We should have had a leader");
1694
1695 // If this is a leader that is always available, and it's a
1696 // constant or has no equivalences, just replace everything with
1697 // it. We then update the congruence class with whatever members
1698 // are left.
1699 if (alwaysAvailable(CC->RepLeader)) {
1700 SmallPtrSet MembersLeft;
1701 for (auto M : CC->Members) {
1702
1703 Value *Member = M;
1704
1705 // Void things have no uses we can replace.
1706 if (Member == CC->RepLeader || Member->getType()->isVoidTy()) {
1707 MembersLeft.insert(Member);
1708 continue;
1709 }
1710
1711 DEBUG(dbgs() << "Found replacement " << *(CC->RepLeader) << " for "
1712 << *Member << "\n");
1713 // Due to equality propagation, these may not always be
1714 // instructions, they may be real values. We don't really
1715 // care about trying to replace the non-instructions.
1716 if (auto *I = dyn_cast(Member)) {
1717 assert(CC->RepLeader != I &&
1718 "About to accidentally remove our leader");
1719 replaceInstruction(I, CC->RepLeader);
1720 AnythingReplaced = true;
1721
1722 continue;
1723 } else {
1724 MembersLeft.insert(I);
1725 }
1726 }
1727 CC->Members.swap(MembersLeft);
1728
1729 } else {
1730 DEBUG(dbgs() << "Eliminating in congruence class " << CC->ID << "\n");
1731 // If this is a singleton, we can skip it.
1732 if (CC->Members.size() != 1) {
1733
1734 // This is a stack because equality replacement/etc may place
1735 // constants in the middle of the member list, and we want to use
1736 // those constant values in preference to the current leader, over
1737 // the scope of those constants.
1738 ValueDFSStack EliminationStack;
1739
1740 // Convert the members to DFS ordered sets and then merge them.
1741 std::vector DFSOrderedSet;
1742 convertDenseToDFSOrdered(CC->Members, DFSOrderedSet);
1743
1744 // Sort the whole thing.
1745 sort(DFSOrderedSet.begin(), DFSOrderedSet.end());
1746
1747 for (auto &C : DFSOrderedSet) {
1748 int MemberDFSIn = C.DFSIn;
1749 int MemberDFSOut = C.DFSOut;
1750 Value *Member = C.Val;
1751 Use *MemberUse = C.U;
1752
1753 // We ignore void things because we can't get a value from them.
1754 if (Member && Member->getType()->isVoidTy())
1755 continue;
1756
1757 if (EliminationStack.empty()) {
1758 DEBUG(dbgs() << "Elimination Stack is empty\n");
1759 } else {
1760 DEBUG(dbgs() << "Elimination Stack Top DFS numbers are ("
1761 << EliminationStack.dfs_back().first << ","
1762 << EliminationStack.dfs_back().second << ")\n");
1763 }
1764 if (Member && isa(Member))
1765 assert(isa(CC->RepLeader));
1766
1767 DEBUG(dbgs() << "Current DFS numbers are (" << MemberDFSIn << ","
1768 << MemberDFSOut << ")\n");
1769 // First, we see if we are out of scope or empty. If so,
1770 // and there equivalences, we try to replace the top of
1771 // stack with equivalences (if it's on the stack, it must
1772 // not have been eliminated yet).
1773 // Then we synchronize to our current scope, by
1774 // popping until we are back within a DFS scope that
1775 // dominates the current member.
1776 // Then, what happens depends on a few factors
1777 // If the stack is now empty, we need to push
1778 // If we have a constant or a local equivalence we want to
1779 // start using, we also push.
1780 // Otherwise, we walk along, processing members who are
1781 // dominated by this scope, and eliminate them.
1782 bool ShouldPush =
1783 Member && (EliminationStack.empty() || isa(Member));
1784 bool OutOfScope =
1785 !EliminationStack.isInScope(MemberDFSIn, MemberDFSOut);
1786
1787 if (OutOfScope || ShouldPush) {
1788 // Sync to our current scope.
1789 EliminationStack.popUntilDFSScope(MemberDFSIn, MemberDFSOut);
1790 ShouldPush |= Member && EliminationStack.empty();
1791 if (ShouldPush) {
1792 EliminationStack.push_back(Member, MemberDFSIn, MemberDFSOut);
1793 }
1794 }
1795
1796 // If we get to this point, and the stack is empty we must have a use
1797 // with nothing we can use to eliminate it, just skip it.
1798 if (EliminationStack.empty())
1799 continue;
1800
1801 // Skip the Value's, we only want to eliminate on their uses.
1802 if (Member)
1803 continue;
1804 Value *Result = EliminationStack.back();
1805
1806 // Don't replace our existing users with ourselves.
1807 if (MemberUse->get() == Result)
1808 continue;
1809
1810 DEBUG(dbgs() << "Found replacement " << *Result << " for "
1811 << *MemberUse->get() << " in " << *(MemberUse->getUser())
1812 << "\n");
1813
1814 // If we replaced something in an instruction, handle the patching of
1815 // metadata.
1816 if (auto *ReplacedInst =
1817 dyn_cast(MemberUse->get()))
1818 patchReplacementInstruction(ReplacedInst, Result);
1819
1820 assert(isa(MemberUse->getUser()));
1821 MemberUse->set(Result);
1822 AnythingReplaced = true;
1823 }
1824 }
1825 }
1826
1827 // Cleanup the congruence class.
1828 SmallPtrSet MembersLeft;
1829 for (auto MI = CC->Members.begin(), ME = CC->Members.end(); MI != ME;) {
1830 auto CurrIter = MI;
1831 ++MI;
1832 Value *Member = *CurrIter;
1833 if (Member->getType()->isVoidTy()) {
1834 MembersLeft.insert(Member);
1835 continue;
1836 }
1837
1838 if (auto *MemberInst = dyn_cast(Member)) {
1839 if (isInstructionTriviallyDead(MemberInst)) {
1840 // TODO: Don't mark loads of undefs.
1841 markInstructionForDeletion(MemberInst);
1842 continue;
1843 }
1844 }
1845 MembersLeft.insert(Member);
1846 }
1847 CC->Members.swap(MembersLeft);
1848 }
1849
1850 return AnythingReplaced;
1851 }
1852
4242 initializeDSELegacyPassPass(Registry);
4343 initializeGuardWideningLegacyPassPass(Registry);
4444 initializeGVNLegacyPassPass(Registry);
45 initializeNewGVNPass(Registry);
4546 initializeEarlyCSELegacyPassPass(Registry);
4647 initializeEarlyCSEMemSSALegacyPassPass(Registry);
4748 initializeGVNHoistLegacyPassPass(Registry);
125126 unwrap(PM)->add(createGVNPass());
126127 }
127128
129 void LLVMAddNewGVNPass(LLVMPassManagerRef PM) {
130 unwrap(PM)->add(createNewGVNPass());
131 }
132
128133 void LLVMAddMergedLoadStoreMotionPass(LLVMPassManagerRef PM) {
129134 unwrap(PM)->add(createMergedLoadStoreMotionPass());
130135 }
0 ; RUN: opt < %s -newgvn | llvm-dis
1
2 %struct.PerlInterpreter = type { i8 }
3 @PL_sv_count = external global i32 ; [#uses=2]
4
5 define void @perl_destruct(%struct.PerlInterpreter* %sv_interp) {
6 entry:
7 br i1 false, label %cond_next25, label %cond_true16
8
9 cond_true16: ; preds = %entry
10 ret void
11
12 cond_next25: ; preds = %entry
13 br i1 false, label %cond_next33, label %cond_true32
14
15 cond_true32: ; preds = %cond_next25
16 ret void
17
18 cond_next33: ; preds = %cond_next25
19 br i1 false, label %cond_next61, label %cond_true.i46
20
21 cond_true.i46: ; preds = %cond_next33
22 ret void
23
24 cond_next61: ; preds = %cond_next33
25 br i1 false, label %cond_next69, label %cond_true66
26
27 cond_true66: ; preds = %cond_next61
28 ret void
29
30 cond_next69: ; preds = %cond_next61
31 br i1 false, label %Perl_safefree.exit52, label %cond_true.i50
32
33 cond_true.i50: ; preds = %cond_next69
34 ret void
35
36 Perl_safefree.exit52: ; preds = %cond_next69
37 br i1 false, label %cond_next80, label %cond_true77
38
39 cond_true77: ; preds = %Perl_safefree.exit52
40 ret void
41
42 cond_next80: ; preds = %Perl_safefree.exit52
43 br i1 false, label %Perl_safefree.exit56, label %cond_true.i54
44
45 cond_true.i54: ; preds = %cond_next80
46 ret void
47
48 Perl_safefree.exit56: ; preds = %cond_next80
49 br i1 false, label %Perl_safefree.exit60, label %cond_true.i58
50
51 cond_true.i58: ; preds = %Perl_safefree.exit56
52 ret void
53
54 Perl_safefree.exit60: ; preds = %Perl_safefree.exit56
55 br i1 false, label %Perl_safefree.exit64, label %cond_true.i62
56
57 cond_true.i62: ; preds = %Perl_safefree.exit60
58 ret void
59
60 Perl_safefree.exit64: ; preds = %Perl_safefree.exit60
61 br i1 false, label %Perl_safefree.exit68, label %cond_true.i66
62
63 cond_true.i66: ; preds = %Perl_safefree.exit64
64 ret void
65
66 Perl_safefree.exit68: ; preds = %Perl_safefree.exit64
67 br i1 false, label %cond_next150, label %cond_true23.i
68
69 cond_true23.i: ; preds = %Perl_safefree.exit68
70 ret void
71
72 cond_next150: ; preds = %Perl_safefree.exit68
73 %tmp16092 = load i32, i32* @PL_sv_count, align 4 ; [#uses=0]
74 br label %cond_next165
75
76 bb157: ; preds = %cond_next165
77 %tmp158 = load i32, i32* @PL_sv_count, align 4 ; [#uses=0]
78 br label %cond_next165
79
80 cond_next165: ; preds = %bb157, %cond_next150
81 br i1 false, label %bb171, label %bb157
82
83 bb171: ; preds = %cond_next165
84 ret void
85 }
0 ; RUN: opt < %s -basicaa -newgvn -S | FileCheck %s
1
2 %struct.INT2 = type { i32, i32 }
3 @blkshifts = external global %struct.INT2* ; <%struct.INT2**> [#uses=2]
4
5 define i32 @xcompact() {
6 entry:
7 store %struct.INT2* null, %struct.INT2** @blkshifts, align 4
8 br label %bb
9
10 bb: ; preds = %bb, %entry
11 %tmp10 = load %struct.INT2*, %struct.INT2** @blkshifts, align 4 ; <%struct.INT2*> [#uses=0]
12 ; CHECK-NOT: %tmp10
13 br label %bb
14 }
0 ; RUN: opt < %s -newgvn | llvm-dis
1
2 %struct.s_segment_inf = type { float, i32, i16, i16, float, float, i32, float, float }
3
4 define void @print_arch(i8* %arch_file, i32 %route_type, i64 %det_routing_arch.0.0, i64 %det_routing_arch.0.1, i64 %det_routing_arch.0.2, i64 %det_routing_arch.0.3, i64 %det_routing_arch.0.4, %struct.s_segment_inf* %segment_inf, i64 %timing_inf.0.0, i64 %timing_inf.0.1, i64 %timing_inf.0.2, i64 %timing_inf.0.3, i64 %timing_inf.0.4, i32 %timing_inf.1) {
5 entry:
6 br i1 false, label %bb278, label %bb344
7
8 bb278: ; preds = %bb278, %entry
9 br i1 false, label %bb278, label %bb344
10
11 bb344: ; preds = %bb278, %entry
12 %tmp38758 = load i16, i16* null, align 2 ; [#uses=0]
13 ret void
14 }
0 ; RUN: opt < %s -newgvn | llvm-dis
1
2 %struct.TypHeader = type { i32, %struct.TypHeader**, [3 x i8], i8 }
3
4 define %struct.TypHeader* @LtRec(%struct.TypHeader* %hdL, %struct.TypHeader* %hdR) {
5 entry:
6 br i1 false, label %bb556.preheader, label %bb534.preheader
7
8 bb534.preheader: ; preds = %entry
9 ret %struct.TypHeader* null
10
11 bb556.preheader: ; preds = %entry
12 %tmp56119 = getelementptr %struct.TypHeader, %struct.TypHeader* %hdR, i32 0, i32 0 ; [#uses=1]
13 %tmp56220 = load i32, i32* %tmp56119 ; [#uses=0]
14 br i1 false, label %bb.nph23, label %bb675.preheader
15
16 bb.nph23: ; preds = %bb556.preheader
17 ret %struct.TypHeader* null
18
19 bb656: ; preds = %bb675.outer, %bb656
20 %tmp678 = load i32, i32* %tmp677 ; [#uses=0]
21 br i1 false, label %bb684, label %bb656
22
23 bb684: ; preds = %bb675.outer, %bb656
24 br i1 false, label %bb924.preheader, label %bb675.outer
25
26 bb675.outer: ; preds = %bb675.preheader, %bb684
27 %tmp67812 = load i32, i32* %tmp67711 ; [#uses=0]
28 br i1 false, label %bb684, label %bb656
29
30 bb675.preheader: ; preds = %bb556.preheader
31 %tmp67711 = getelementptr %struct.TypHeader, %struct.TypHeader* %hdR, i32 0, i32 0 ; [#uses=1]
32 %tmp677 = getelementptr %struct.TypHeader, %struct.TypHeader* %hdR, i32 0, i32 0 ; [#uses=1]
33 br label %bb675.outer
34
35 bb924.preheader: ; preds = %bb684
36 ret %struct.TypHeader* null
37 }
0 ; RUN: opt < %s -newgvn | llvm-dis
1
2 %struct.ggBRDF = type { i32 (...)** }
3 %struct.ggBox3 = type { %struct.ggPoint3, %struct.ggPoint3 }
4 %struct.ggMaterialRecord = type { %struct.ggPoint2, %struct.ggBox3, %struct.ggBox3, %struct.ggSpectrum, %struct.ggSpectrum, %struct.ggSpectrum, %struct.ggBRDF*, i32, i32, i32, i32 }
5 %struct.ggONB3 = type { %struct.ggPoint3, %struct.ggPoint3, %struct.ggPoint3 }
6 %struct.ggPoint2 = type { [2 x double] }
7 %struct.ggPoint3 = type { [3 x double] }
8 %struct.ggSpectrum = type { [8 x float] }
9 %struct.mrViewingHitRecord = type { double, %struct.ggPoint3, %struct.ggONB3, %struct.ggPoint2, double, %struct.ggSpectrum, %struct.ggSpectrum, i32, i32, i32, i32 }
10 %struct.mrXEllipticalCylinder = type { %struct.ggBRDF, float, float, float, float, float, float }
11
12 define i32 @_ZNK21mrZEllipticalCylinder10viewingHitERK6ggRay3dddR18mrViewingHitRecordR16ggMaterialRecord(%struct.mrXEllipticalCylinder* %this, %struct.ggBox3* %ray, double %unnamed_arg, double %tmin, double %tmax, %struct.mrViewingHitRecord* %VHR, %struct.ggMaterialRecord* %unnamed_arg2) {
13 entry:
14 %tmp80.i = getelementptr %struct.mrViewingHitRecord, %struct.mrViewingHitRecord* %VHR, i32 0, i32 1, i32 0, i32 0 ; [#uses=1]
15 store double 0.000000e+00, double* %tmp80.i
16 br i1 false, label %return, label %cond_next.i
17
18 cond_next.i: ; preds = %entry
19 br i1 false, label %return, label %cond_true
20
21 cond_true: ; preds = %cond_next.i
22 %tmp3.i8 = getelementptr %struct.mrViewingHitRecord, %struct.mrViewingHitRecord* %VHR, i32 0, i32 1, i32 0, i32 0 ; [#uses=1]
23 %tmp46 = load double, double* %tmp3.i8 ; [#uses=0]
24 ret i32 1
25
26 return: ; preds = %cond_next.i, %entry
27 ret i32 0
28 }
0 ; RUN: opt < %s -basicaa -newgvn -S | FileCheck %s
1
2 @last = external global [65 x i32*]
3
4 define i32 @NextRootMove(i32 %wtm, i32 %x, i32 %y, i32 %z) {
5 entry:
6 %A = alloca i32*
7 %tmp17618 = load i32*, i32** getelementptr ([65 x i32*], [65 x i32*]* @last, i32 0, i32 1), align 4
8 store i32* %tmp17618, i32** %A
9 ; CHECK: entry:
10 ; CHECK-NEXT: alloca i32
11 ; CHECK-NEXT: %tmp17618 = load
12 ; CHECK-NOT: load
13 ; CHECK-NOT: phi
14 br label %cond_true116
15
16 cond_true116:
17 %cmp = icmp eq i32 %x, %y
18 br i1 %cmp, label %cond_true128, label %cond_true145
19
20 cond_true128:
21 %tmp17625 = load i32*, i32** getelementptr ([65 x i32*], [65 x i32*]* @last, i32 0, i32 1), align 4
22 store i32* %tmp17625, i32** %A
23 %cmp1 = icmp eq i32 %x, %z
24 br i1 %cmp1 , label %bb98.backedge, label %return.loopexit
25
26 bb98.backedge:
27 br label %cond_true116
28
29 cond_true145:
30 %tmp17631 = load i32*, i32** getelementptr ([65 x i32*], [65 x i32*]* @last, i32 0, i32 1), align 4
31 store i32* %tmp17631, i32** %A
32 br i1 false, label %bb98.backedge, label %return.loopexit
33
34 return.loopexit:
35 br label %return
36
37 return:
38 ret i32 0
39 }
0 ; RUN: opt < %s -newgvn | llvm-dis
1
2 @bsLive = external global i32 ; [#uses=2]
3
4 define i32 @bsR(i32 %n) {
5 entry:
6 br i1 false, label %cond_next, label %bb19
7
8 cond_next: ; preds = %entry
9 store i32 0, i32* @bsLive, align 4
10 br label %bb19
11
12 bb19: ; preds = %cond_next, %entry
13 %tmp29 = load i32, i32* @bsLive, align 4 ; [#uses=0]
14 ret i32 0
15 }
0 ; XFAIL: *
1 ; RUN: opt < %s -newgvn -S | FileCheck %s
2
3 %struct..0anon = type { i32 }
4 %struct.FILE = type { i8*, i32, i32, i16, i16, %struct.__sbuf, i32, i8*, i32 (i8*)*, i32 (i8*, i8*, i32)*, i64 (i8*, i64, i32)*, i32 (i8*, i8*, i32)*, %struct.__sbuf, %struct.__sFILEX*, i32, [3 x i8], [1 x i8], %struct.__sbuf, i32, i64 }
5 %struct.__sFILEX = type opaque
6 %struct.__sbuf = type { i8*, i32 }
7 %struct.rtx_def = type { i16, i8, i8, [1 x %struct..0anon] }
8 @n_spills = external global i32 ; [#uses=2]
9
10 define i32 @reload(%struct.rtx_def* %first, i32 %global, %struct.FILE* %dumpfile) {
11 cond_next2835.1: ; preds = %cond_next2861
12 %tmp2922 = load i32, i32* @n_spills, align 4 ; [#uses=0]
13 br label %bb2928
14
15 bb2928: ; preds = %cond_next2835.1, %cond_next2943
16 br i1 false, label %cond_next2943, label %cond_true2935
17
18 cond_true2935: ; preds = %bb2928
19 br label %cond_next2943
20
21 cond_next2943: ; preds = %cond_true2935, %bb2928
22 br i1 false, label %bb2982.preheader, label %bb2928
23
24 bb2982.preheader: ; preds = %cond_next2943
25 %tmp298316 = load i32, i32* @n_spills, align 4 ; [#uses=0]
26 ret i32 %tmp298316
27
28 }
29
30 ; CHECK: define i32 @reload(%struct.rtx_def* %first, i32 %global, %struct.FILE* %dumpfile) {
31 ; CHECK-NEXT: cond_next2835.1:
32 ; CHECK-NEXT: br label %bb2928
33 ; CHECK: bb2928:
34 ; CHECK-NEXT: br i1 false, label %bb2928.cond_next2943_crit_edge, label %cond_true2935
35 ; CHECK: bb2928.cond_next2943_crit_edge:
36 ; CHECK-NEXT: br label %cond_next2943
37 ; CHECK: cond_true2935:
38 ; CHECK-NEXT: br label %cond_next2943
39 ; CHECK: cond_next2943:
40 ; CHECK-NEXT: br i1 false, label %bb2982.preheader, label %bb2928
41 ; CHECK: bb2982.preheader:
42 ; CHECK-NEXT: %tmp298316 = load i32, i32* @n_spills, align 4
43 ; CHECK-NEXT: ret i32 %tmp298316
44 ; CHECK-NEXT: }
0 ; RUN: opt < %s -newgvn | llvm-dis
1
2 %"struct.Block::$_16" = type { i32 }
3 %struct.Exp = type { %struct.Exp_*, i32, i32, i32, %struct.Exp*, %struct.Exp*, %"struct.Exp::$_10", %"struct.Block::$_16", %"struct.Exp::$_12" }
4 %"struct.Exp::$_10" = type { %struct.Exp* }
5 %"struct.Exp::$_12" = type { %struct.Exp** }
6 %struct.Exp_ = type { i32, i32, i32, i32, %struct.Id* }
7 %struct.Id = type { i8*, i32, i32, i32, %"struct.Id::$_13" }
8 %"struct.Id::$_13" = type { double }
9
10 define i8* @_ZN3Exp8toStringEj(%struct.Exp* %this, i32 %nextpc) {
11 entry:
12 switch i32 0, label %bb970 [
13 i32 1, label %bb
14 i32 2, label %bb39
15 i32 3, label %bb195
16 i32 4, label %bb270
17 i32 5, label %bb418
18 i32 6, label %bb633
19 i32 7, label %bb810
20 i32 8, label %bb882
21 i32 9, label %bb925
22 ]
23
24 bb: ; preds = %entry
25 store i8* null, i8** null
26 br label %return
27
28 bb39: ; preds = %entry
29 br i1 false, label %cond_true, label %cond_false132
30
31 cond_true: ; preds = %bb39
32 br i1 false, label %cond_true73, label %cond_false
33
34 cond_true73: ; preds = %cond_true
35 br i1 false, label %cond_true108, label %cond_next
36
37 cond_true108: ; preds = %cond_true73
38 br label %cond_next
39
40 cond_next: ; preds = %cond_true108, %cond_true73
41 br label %cond_next131
42
43 cond_false: ; preds = %cond_true
44 br label %cond_next131
45
46 cond_next131: ; preds = %cond_false, %cond_next
47 br label %cond_next141
48
49 cond_false132: ; preds = %bb39
50 br label %cond_next141
51
52 cond_next141: ; preds = %cond_false132, %cond_next131
53 br i1 false, label %cond_true169, label %cond_false175
54
55 cond_true169: ; preds = %cond_next141
56 br label %cond_next181
57
58 cond_false175: ; preds = %cond_next141
59 br label %cond_next181
60
61 cond_next181: ; preds = %cond_false175, %cond_true169
62 br i1 false, label %cond_true189, label %cond_next191
63
64 cond_true189: ; preds = %cond_next181
65 br label %cond_next191
66
67 cond_next191: ; preds = %cond_true189, %cond_next181
68 store i8* null, i8** null
69 br label %return
70
71 bb195: ; preds = %entry
72 br i1 false, label %cond_true248, label %cond_false250
73
74 cond_true248: ; preds = %bb195
75 br label %cond_next252
76
77 cond_false250: ; preds = %bb195
78 br label %cond_next252
79
80 cond_next252: ; preds = %cond_false250, %cond_true248
81 br i1 false, label %cond_true265, label %cond_next267
82
83 cond_true265: ; preds = %cond_next252
84 br label %cond_next267
85
86 cond_next267: ; preds = %cond_true265, %cond_next252
87 store i8* null, i8** null
88 br label %return
89
90 bb270: ; preds = %entry
91 br i1 false, label %cond_true338, label %cond_false340
92
93 cond_true338: ; preds = %bb270
94 br label %cond_next342
95
96 cond_false340: ; preds = %bb270
97 br label %cond_next342
98
99 cond_next342: ; preds = %cond_false340, %cond_true338
100 br i1 false, label %cond_true362, label %cond_false364
101
102 cond_true362: ; preds = %cond_next342
103 br label %cond_next366
104
105 cond_false364: ; preds = %cond_next342
106 br label %cond_next366
107
108 cond_next366: ; preds = %cond_false364, %cond_true362
109 br i1 false, label %cond_true393, label %cond_next395
110
111 cond_true393: ; preds = %cond_next366
112 br label %cond_next395
113
114 cond_next395: ; preds = %cond_true393, %cond_next366
115 br i1 false, label %cond_true406, label %cond_next408
116
117 cond_true406: ; preds = %cond_next395
118 br label %cond_next408
119
120 cond_next408: ; preds = %cond_true406, %cond_next395
121 br i1 false, label %cond_true413, label %cond_next415
122
123 cond_true413: ; preds = %cond_next408
124 br label %cond_next415
125
126 cond_next415: ; preds = %cond_true413, %cond_next408
127 store i8* null, i8** null
128 br label %return
129
130 bb418: ; preds = %entry
131 br i1 false, label %cond_true512, label %cond_false514
132
133 cond_true512: ; preds = %bb418
134 br label %cond_next516
135
136 cond_false514: ; preds = %bb418
137 br label %cond_next516
138
139 cond_next516: ; preds = %cond_false514, %cond_true512
140 br i1 false, label %cond_true536, label %cond_false538
141
142 cond_true536: ; preds = %cond_next516
143 br label %cond_next540
144
145 cond_false538: ; preds = %cond_next516
146 br label %cond_next540
147
148 cond_next540: ; preds = %cond_false538, %cond_true536
149 br i1 false, label %cond_true560, label %cond_false562
150
151 cond_true560: ; preds = %cond_next540
152 br label %cond_next564
153
154 cond_false562: ; preds = %cond_next540
155 br label %cond_next564
156
157 cond_next564: ; preds = %cond_false562, %cond_true560
158 br i1 false, label %cond_true597, label %cond_next599
159
160 cond_true597: ; preds = %cond_next564
161 br label %cond_next599
162
163 cond_next599: ; preds = %cond_true597, %cond_next564
164 br i1 false, label %cond_true614, label %cond_next616
165
166 cond_true614: ; preds = %cond_next599
167 br label %cond_next616
168
169 cond_next616: ; preds = %cond_true614, %cond_next599
170 br i1 false, label %cond_true621, label %cond_next623
171
172 cond_true621: ; preds = %cond_next616
173 br label %cond_next623
174
175 cond_next623: ; preds = %cond_true621, %cond_next616
176 br i1 false, label %cond_true628, label %cond_next630
177
178 cond_true628: ; preds = %cond_next623
179 br label %cond_next630
180
181 cond_next630: ; preds = %cond_true628, %cond_next623
182 store i8* null, i8** null
183 br label %return
184
185 bb633: ; preds = %entry
186 br i1 false, label %cond_true667, label %cond_next669
187
188 cond_true667: ; preds = %bb633
189 br label %cond_next669
190
191 cond_next669: ; preds = %cond_true667, %bb633
192 br i1 false, label %cond_true678, label %cond_next791
193
194 cond_true678: ; preds = %cond_next669
195 br label %bb735
196
197 bb679: ; preds = %bb735
198 br i1 false, label %cond_true729, label %cond_next731
199
200 cond_true729: ; preds = %bb679
201 br label %cond_next731
202
203 cond_next731: ; preds = %cond_true729, %bb679
204 br label %bb735
205
206 bb735: ; preds = %cond_next731, %cond_true678
207 br i1 false, label %bb679, label %bb743
208
209 bb743: ; preds = %bb735
210 br i1 false, label %cond_true788, label %cond_next790
211
212 cond_true788: ; preds = %bb743
213 br label %cond_next790
214
215 cond_next790: ; preds = %cond_true788, %bb743
216 br label %cond_next791
217
218 cond_next791: ; preds = %cond_next790, %cond_next669
219 br i1 false, label %cond_true805, label %cond_next807
220
221 cond_true805: ; preds = %cond_next791
222 br label %cond_next807
223
224 cond_next807: ; preds = %cond_true805, %cond_next791
225 store i8* null, i8** null
226 br label %return
227
228 bb810: ; preds = %entry
229 br i1 false, label %cond_true870, label %cond_next872
230
231 cond_true870: ; preds = %bb810
232 br label %cond_next872
233
234 cond_next872: ; preds = %cond_true870, %bb810
235 br i1 false, label %cond_true877, label %cond_next879
236
237 cond_true877: ; preds = %cond_next872
238 br label %cond_next879
239
240 cond_next879: ; preds = %cond_true877, %cond_next872
241 store i8* null, i8** null
242 br label %return
243
244 bb882: ; preds = %entry
245 br i1 false, label %cond_true920, label %cond_next922
246
247 cond_true920: ; preds = %bb882
248 br label %cond_next922
249
250 cond_next922: ; preds = %cond_true920, %bb882
251 store i8* null, i8** null
252 br label %return
253
254 bb925: ; preds = %entry
255 br i1 false, label %cond_true965, label %cond_next967
256
257 cond_true965: ; preds = %bb925
258 br label %cond_next967
259
260 cond_next967: ; preds = %cond_true965, %bb925
261 store i8* null, i8** null
262 br label %return
263
264 bb970: ; preds = %entry
265 unreachable
266 ; No predecessors!
267 store i8* null, i8** null
268 br label %return
269
270 return: ; preds = %0, %cond_next967, %cond_next922, %cond_next879, %cond_next807, %cond_next630, %cond_next415, %cond_next267, %cond_next191, %bb
271 %retval980 = load i8*, i8** null ; [#uses=1]
272 ret i8* %retval980
273 }
0 ; XFAIL: *
1 ; RUN: opt < %s -basicaa -newgvn -S | FileCheck %s
2
3 %struct.anon = type { i32 (i32, i32, i32)*, i32, i32, [3 x i32], i8*, i8*, i8* }
4 @debug = external constant i32 ; [#uses=0]
5 @counters = external constant i32 ; [#uses=1]
6 @trialx = external global [17 x i32] ; <[17 x i32]*> [#uses=1]
7 @dummy1 = external global [7 x i32] ; <[7 x i32]*> [#uses=0]
8 @dummy2 = external global [4 x i32] ; <[4 x i32]*> [#uses=0]
9 @unacceptable = external global i32 ; [#uses=0]
10 @isa = external global [13 x %struct.anon] ; <[13 x %struct.anon]*> [#uses=3]
11 @.str = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
12 @.str1 = external constant [3 x i8] ; <[3 x i8]*> [#uses=0]
13 @.str2 = external constant [1 x i8] ; <[1 x i8]*> [#uses=0]
14 @.str3 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
15 @.str4 = external constant [3 x i8] ; <[3 x i8]*> [#uses=0]
16 @.str5 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
17 @.str6 = external constant [2 x i8] ; <[2 x i8]*> [#uses=0]
18 @.str7 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
19 @.str8 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
20 @.str9 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
21 @.str10 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
22 @.str11 = external constant [2 x i8] ; <[2 x i8]*> [#uses=0]
23 @.str12 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
24 @.str13 = external constant [2 x i8] ; <[2 x i8]*> [#uses=0]
25 @.str14 = external constant [5 x i8] ; <[5 x i8]*> [#uses=0]
26 @.str15 = external constant [5 x i8] ; <[5 x i8]*> [#uses=0]
27 @.str16 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
28 @.str17 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
29 @.str18 = external constant [3 x i8] ; <[3 x i8]*> [#uses=0]
30 @.str19 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
31 @.str20 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
32 @.str21 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
33 @.str22 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
34 @.str23 = external constant [5 x i8] ; <[5 x i8]*> [#uses=0]
35 @.str24 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
36 @.str25 = external constant [6 x i8] ; <[6 x i8]*> [#uses=0]
37 @.str26 = external constant [5 x i8] ; <[5 x i8]*> [#uses=0]
38 @.str27 = external constant [6 x i8] ; <[6 x i8]*> [#uses=0]
39 @r = external global [17 x i32] ; <[17 x i32]*> [#uses=0]
40 @.str28 = external constant [3 x i8] ; <[3 x i8]*> [#uses=0]
41 @.str29 = external constant [5 x i8] ; <[5 x i8]*> [#uses=0]
42 @pgm = external global [5 x { i32, [3 x i32] }] ; <[5 x { i32, [3 x i32] }]*> [#uses=4]
43 @.str30 = external constant [3 x i8] ; <[3 x i8]*> [#uses=0]
44 @.str31 = external constant [13 x i8] ; <[13 x i8]*> [#uses=0]
45 @.str32 = external constant [3 x i8] ; <[3 x i8]*> [#uses=0]
46 @.str33 = external constant [4 x i8] ; <[4 x i8]*> [#uses=0]
47 @.str34 = external constant [20 x i8] ; <[20 x i8]*> [#uses=0]
48 @numi = external global i32 ; [#uses=7]
49 @.str35 = external constant [10 x i8] ; <[10 x i8]*> [#uses=0]
50 @counter = external global [5 x i32] ; <[5 x i32]*> [#uses=2]
51 @itrialx.2510 = external global i32 ; [#uses=0]
52 @.str36 = external constant [43 x i8] ; <[43 x i8]*> [#uses=0]
53 @.str37 = external constant [42 x i8] ; <[42 x i8]*> [#uses=0]
54 @corr_result = external global i32 ; [#uses=0]
55 @.str38 = external constant [3 x i8] ; <[3 x i8]*> [#uses=0]
56 @.str39 = external constant [5 x i8] ; <[5 x i8]*> [#uses=0]
57 @.str40 = external constant [47 x i8] ; <[47 x i8]*> [#uses=0]
58 @correct_result = external global [17 x i32] ; <[17 x i32]*> [#uses=1]
59 @.str41 = external constant [46 x i8] ; <[46 x i8]*> [#uses=0]
60 @.str42 = external constant [32 x i8] ; <[32 x i8]*> [#uses=0]
61 @.str43 = external constant [44 x i8] ; <[44 x i8]*> [#uses=1]
62 @.str44 = external constant [21 x i8] ; <[21 x i8]*> [#uses=1]
63 @.str45 = external constant [12 x i8] ; <[12 x i8]*> [#uses=1]
64 @.str46 = external constant [5 x i8] ; <[5 x i8]*> [#uses=1]
65 @.str47 = external constant [12 x i8] ; <[12 x i8]*> [#uses=1]
66
67 declare i32 @neg(i32, i32, i32)
68
69 declare i32 @Not(i32, i32, i32)
70
71 declare i32 @pop(i32, i32, i32)
72
73 declare i32 @nlz(i32, i32, i32)
74
75 declare i32 @rev(i32, i32, i32)
76
77 declare i32 @add(i32, i32, i32)
78
79 declare i32 @sub(i32, i32, i32)
80
81 declare i32 @mul(i32, i32, i32)
82
83 declare i32 @divide(i32, i32, i32)
84
85 declare i32 @divu(i32, i32, i32)
86
87 declare i32 @And(i32, i32, i32)
88
89 declare i32 @Or(i32, i32, i32)
90
91 declare i32 @Xor(i32, i32, i32)
92
93 declare i32 @rotl(i32, i32, i32)
94
95 declare i32 @shl(i32, i32, i32)
96
97 declare i32 @shr(i32, i32, i32)
98
99 declare i32 @shrs(i32, i32, i32)
100
101 declare i32 @cmpeq(i32, i32, i32)
102
103 declare i32 @cmplt(i32, i32, i32)
104
105 declare i32 @cmpltu(i32, i32, i32)
106
107 declare i32 @seleq(i32, i32, i32)
108
109 declare i32 @sellt(i32, i32, i32)
110
111 declare i32 @selle(i32, i32, i32)
112
113 declare void @print_expr(i32)
114
115 declare i32 @printf(i8*, ...)
116
117 declare i32 @putchar(i32)
118
119 declare void @print_pgm()
120
121 declare void @simulate_one_instruction(i32)
122
123 declare i32 @check(i32)
124
125 declare i32 @puts(i8*)
126
127 declare void @fix_operands(i32)
128
129 declare void @abort()
130
131 declare i32 @increment()
132
133 declare i32 @search()
134
135 define i32 @main(i32 %argc, i8** %argv) {
136 entry:
137 %argc_addr = alloca i32 ; [#uses=1]
138 %argv_addr = alloca i8** ; [#uses=1]
139 %retval = alloca i32, align 4 ; [#uses=2]
140 %tmp = alloca i32, align 4 ; [#uses=2]
141 %i = alloca i32, align 4 ; [#uses=21]
142 %num_sol = alloca i32, align 4 ; [#uses=4]
143 %total = alloca i32, align 4 ; [#uses=4]
144 %"alloca point" = bitcast i32 0 to i32 ; [#uses=0]
145 store i32 %argc, i32* %argc_addr
146 store i8** %argv, i8*** %argv_addr
147 store i32 0, i32* %num_sol
148 store i32 1, i32* @numi
149 br label %bb91
150
151 bb: ; preds = %cond_next97
152 %tmp1 = load i32, i32* @numi ; [#uses=1]
153 %tmp2 = getelementptr [44 x i8], [44 x i8]* @.str43, i32 0, i32 0 ; [#uses=1]
154 %tmp3 = call i32 (i8*, ...) @printf( i8* %tmp2, i32 %tmp1 ) ; [#uses=0]
155 store i32 0, i32* %i
156 br label %bb13
157
158 bb4: ; preds = %bb13
159 %tmp5 = load i32, i32* %i ; [#uses=1]
160 %tmp6 = load i32, i32* %i ; [#uses=1]
161 %tmp7 = getelementptr [17 x i32], [17 x i32]* @trialx, i32 0, i32 %tmp6 ; [#uses=1]
162 %tmp8 = load i32, i32* %tmp7 ; [#uses=1]
163 %tmp9 = call i32 @userfun( i32 %tmp8 ) ; [#uses=1]
164 %tmp10 = getelementptr [17 x i32], [17 x i32]* @correct_result, i32 0, i32 %tmp5 ; [#uses=1]
165 store i32 %tmp9, i32* %tmp10
166 %tmp11 = load i32, i32* %i ; [#uses=1]
167 %tmp12 = add i32 %tmp11, 1 ; [#uses=1]
168 store i32 %tmp12, i32* %i
169 br label %bb13
170
171 bb13: ; preds = %bb4, %bb
172 %tmp14 = load i32, i32* %i ; [#uses=1]
173 %tmp15 = icmp sle i32 %tmp14, 16 ; [#uses=1]
174 %tmp1516 = zext i1 %tmp15 to i32 ; [#uses=1]
175 %toBool = icmp ne i32 %tmp1516, 0 ; [#uses=1]
176 br i1 %toBool, label %bb4, label %bb17
177
178 bb17: ; preds = %bb13
179 store i32 0, i32* %i
180 br label %bb49
181
182 bb18: ; preds = %bb49
183 %tmp19 = load i32, i32* %i ; [#uses=1]
184 %tmp20 = getelementptr [5 x { i32, [3 x i32] }], [5 x { i32, [3 x i32] }]* @pgm, i32 0, i32 %tmp19 ; <{ i32, [3 x i32] }*> [#uses=1]
185 %tmp21 = getelementptr { i32, [3 x i32] }, { i32, [3 x i32] }* %tmp20, i32 0, i32 0 ; [#uses=1]
186 store i32 0, i32* %tmp21
187 %tmp22 = load i32, i32* %i ; [#uses=1]
188 %tmp23 = getelementptr [13 x %struct.anon], [13 x %struct.anon]* @isa, i32 0, i32 0 ; <%struct.anon*> [#uses=1]
189 %tmp24 = getelementptr %struct.anon, %struct.anon* %tmp23, i32 0, i32 3 ; <[3 x i32]*> [#uses=1]
190 %tmp25 = getelementptr [3 x i32], [3 x i32]* %tmp24, i32 0, i32 0 ; [#uses=1]
191 %tmp26 = load i32, i32* %tmp25 ; [#uses=1]
192 %tmp27 = getelementptr [5 x { i32, [3 x i32] }], [5 x { i32, [3 x i32] }]* @pgm, i32 0, i32 %tmp22 ; <{ i32, [3 x i32] }*> [#uses=1]
193 %tmp28 = getelementptr { i32, [3 x i32] }, { i32, [3 x i32] }* %tmp27, i32 0, i32 1 ; <[3 x i32]*> [#uses=1]
194 %tmp29 = getelementptr [3 x i32], [3 x i32]* %tmp28, i32 0, i32 0 ; [#uses=1]
195 store i32 %tmp26, i32* %tmp29
196 %tmp30 = load i32, i32* %i ; [#uses=1]
197 %tmp31 = getelementptr [13 x %struct.anon], [13 x %struct.anon]* @isa, i32 0, i32 0 ; <%struct.anon*> [#uses=1]
198 %tmp32 = getelementptr %struct.anon, %struct.anon* %tmp31, i32 0, i32 3 ; <[3 x i32]*> [#uses=1]
199 %tmp33 = getelementptr [3 x i32], [3 x i32]* %tmp32, i32 0, i32 1 ; [#uses=1]
200 %tmp34 = load i32, i32* %tmp33 ; [#uses=1]
201 %tmp35 = getelementptr [5 x { i32, [3 x i32] }], [5 x { i32, [3 x i32] }]* @pgm, i32 0, i32 %tmp30 ; <{ i32, [3 x i32] }*> [#uses=1]
202 %tmp36 = getelementptr { i32, [3 x i32] }, { i32, [3 x i32] }* %tmp35, i32 0, i32 1 ; <[3 x i32]*> [#uses=1]
203 %tmp37 = getelementptr [3 x i32], [3 x i32]* %tmp36, i32 0, i32 1 ; [#uses=1]
204 store i32 %tmp34, i32* %tmp37
205 %tmp38 = load i32, i32* %i ; [#uses=1]
206 %tmp39 = getelementptr [13 x %struct.anon], [13 x %struct.anon]* @isa, i32 0, i32 0 ; <%struct.anon*> [#uses=1]
207 %tmp40 = getelementptr %struct.anon, %struct.anon* %tmp39, i32 0, i32 3 ; <[3 x i32]*> [#uses=1]
208 %tmp41 = getelementptr [3 x i32], [3 x i32]* %tmp40, i32 0, i32 2 ; [#uses=1]
209 %tmp42 = load i32, i32* %tmp41 ; [#uses=1]
210 %tmp43 = getelementptr [5 x { i32, [3 x i32] }], [5 x { i32, [3 x i32] }]* @pgm, i32 0, i32 %tmp38 ; <{ i32, [3 x i32] }*> [#uses=1]
211 %tmp44 = getelementptr { i32, [3 x i32] }, { i32, [3 x i32] }* %tmp43, i32 0, i32 1 ; <[3 x i32]*> [#uses=1]
212 %tmp45 = getelementptr [3 x i32], [3 x i32]* %tmp44, i32 0, i32 2 ; [#uses=1]
213 store i32 %tmp42, i32* %tmp45
214 %tmp46 = load i32, i32* %i ; [#uses=1]
215 call void @fix_operands( i32 %tmp46 )
216 %tmp47 = load i32, i32* %i ; [#uses=1]
217 ; CHECK: %tmp47 = phi i32 [ %tmp48, %bb18 ], [ 0, %bb17 ]
218 %tmp48 = add i32 %tmp47, 1 ; [#uses=1]
219 store i32 %tmp48, i32* %i
220 br label %bb49
221
222 bb49: ; preds = %bb18, %bb17
223 %tmp50 = load i32, i32* @numi ; [#uses=1]
224 %tmp51 = load i32, i32* %i ; [#uses=1]
225 %tmp52 = icmp slt i32 %tmp51, %tmp50 ; [#uses=1]
226 %tmp5253 = zext i1 %tmp52 to i32 ; [#uses=1]
227 %toBool54 = icmp ne i32 %tmp5253, 0 ; [#uses=1]
228 br i1 %toBool54, label %bb18, label %bb55
229
230 bb55: ; preds = %bb49
231 %tmp56 = call i32 @search( ) ; [#uses=1]
232 store i32 %tmp56, i32* %num_sol
233 %tmp57 = getelementptr [21 x i8], [21 x i8]* @.str44, i32 0, i32 0 ; [#uses=1]
234 %tmp58 = load i32, i32* %num_sol ; [#uses=1]
235 %tmp59 = call i32 (i8*, ...) @printf( i8* %tmp57, i32 %tmp58 ) ; [#uses=0]
236 %tmp60 = load i32, i32* @counters ; [#uses=1]
237 %tmp61 = icmp ne i32 %tmp60, 0 ; [#uses=1]
238 %tmp6162 = zext i1 %tmp61 to i32 ; [#uses=1]
239 %toBool63 = icmp ne i32 %tmp6162, 0 ; [#uses=1]
240 br i1 %toBool63, label %cond_true, label %cond_next
241
242 cond_true: ; preds = %bb55
243 store i32 0, i32* %total
244 %tmp64 = getelementptr [12 x i8], [12 x i8]* @.str45, i32 0, i32 0 ; [#uses=1]
245 %tmp65 = call i32 (i8*, ...) @printf( i8* %tmp64 ) ; [#uses=0]
246 store i32 0, i32* %i
247 br label %bb79
248
249 bb66: ; preds = %bb79
250 %tmp67 = load i32, i32* %i ; [#uses=1]
251 %tmp68 = getelementptr [5 x i32], [5 x i32]* @counter, i32 0, i32 %tmp67 ; [#uses=1]
252 %tmp69 = load i32, i32* %tmp68 ; [#uses=1]
253 %tmp70 = getelementptr [5 x i8], [5 x i8]* @.str46, i32 0, i32 0 ; [#uses=1]
254 %tmp71 = call i32 (i8*, ...) @printf( i8* %tmp70, i32 %tmp69 ) ; [#uses=0]
255 %tmp72 = load i32, i32* %i ; [#uses=1]
256 %tmp73 = getelementptr [5 x i32], [5 x i32]* @counter, i32 0, i32 %tmp72 ; [#uses=1]
257 %tmp74 = load i32, i32* %tmp73 ; [#uses=1]
258 %tmp75 = load i32, i32* %total ; [#uses=1]
259 %tmp76 = add i32 %tmp74, %tmp75 ; [#uses=1]
260 store i32 %tmp76, i32* %total
261 %tmp77 = load i32, i32* %i ; [#uses=1]
262 %tmp78 = add i32 %tmp77, 1 ; [#uses=1]
263 store i32 %tmp78, i32* %i
264 br label %bb79
265
266 bb79: ; preds = %bb66, %cond_true
267 %tmp80 = load i32, i32* @numi ; [#uses=1]
268 %tmp81 = load i32, i32* %i ; [#uses=1]
269 %tmp82 = icmp slt i32 %tmp81, %tmp80 ; [#uses=1]
270 %tmp8283 = zext i1 %tmp82 to i32 ; [#uses=1]
271 %toBool84 = icmp ne i32 %tmp8283, 0 ; [#uses=1]
272 br i1 %toBool84, label %bb66, label %bb85
273
274 bb85: ; preds = %bb79
275 %tmp86 = getelementptr [12 x i8], [12 x i8]* @.str47, i32 0, i32 0 ; [#uses=1]
276 %tmp87 = load i32, i32* %total ; [#uses=1]
277 %tmp88 = call i32 (i8*, ...) @printf( i8* %tmp86, i32 %tmp87 ) ; [#uses=0]
278 br label %cond_next
279
280 cond_next: ; preds = %bb85, %bb55
281 %tmp89 = load i32, i32* @numi ; [#uses=1]
282 %tmp90 = add i32 %tmp89, 1 ; [#uses=1]
283 store i32 %tmp90, i32* @numi
284 br label %bb91
285
286 bb91: ; preds = %cond_next, %entry
287 %tmp92 = load i32, i32* @numi ; [#uses=1]
288 %tmp93 = icmp sgt i32 %tmp92, 5 ; [#uses=1]
289 %tmp9394 = zext i1 %tmp93 to i32 ; [#uses=1]
290 %toBool95 = icmp ne i32 %tmp9394, 0 ; [#uses=1]
291 br i1 %toBool95, label %cond_true96, label %cond_next97
292
293 cond_true96: ; preds = %bb91
294 br label %bb102
295
296 cond_next97: ; preds = %bb91
297 %tmp98 = load i32, i32* %num_sol ; [#uses=1]
298 %tmp99 = icmp eq i32 %tmp98, 0 ; [#uses=1]
299 %tmp99100 = zext i1 %tmp99 to i32 ; [#uses=1]
300 %toBool101 = icmp ne i32 %tmp99100, 0 ; [#uses=1]
301 br i1 %toBool101, label %bb, label %bb102
302
303 bb102: ; preds = %cond_next97, %cond_true96
304 store i32 0, i32* %tmp
305 %tmp103 = load i32, i32* %tmp ; [#uses=1]
306 store i32 %tmp103, i32* %retval
307 br label %return
308
309 return: ; preds = %bb102
310 %retval104 = load i32, i32* %retval ; [#uses=1]
311 ret i32 %retval104
312 }
313
314 declare i32 @userfun(i32)
0 ; RUN: opt < %s -basicaa -newgvn -S | FileCheck %s
1
2 @img_width = external global i16 ; [#uses=2]
3
4 define i32 @smpUMHEXBipredIntegerPelBlockMotionSearch(i16* %cur_pic, i16 signext %ref, i32 %list, i32 %pic_pix_x, i32 %pic_pix_y, i32 %blocktype, i16 signext %pred_mv_x1, i16 signext %pred_mv_y1, i16 signext %pred_mv_x2, i16 signext %pred_mv_y2, i16* %mv_x, i16* %mv_y, i16* %s_mv_x, i16* %s_mv_y, i32 %search_range, i32 %min_mcost, i32 %lambda_factor) {
5 cond_next143: ; preds = %entry
6 store i16 0, i16* @img_width, align 2
7 br i1 false, label %cond_next449, label %cond_false434
8
9 cond_false434: ; preds = %cond_true415
10 br label %cond_next449
11
12 cond_next449: ; preds = %cond_false434, %cond_true415
13 br i1 false, label %cond_next698, label %cond_false470
14
15 cond_false470: ; preds = %cond_next449
16 br label %cond_next698
17
18 cond_next698: ; preds = %cond_true492
19 %tmp701 = load i16, i16* @img_width, align 2 ; [#uses=0]
20 ; CHECK-NOT: %tmp701 =
21 ret i32 0
22 }
0 ; XFAIL: *
1 ; RUN: opt < %s -newgvn -S | FileCheck %s
2 ; PR1996
3
4 %struct.anon = type { i32, i8, i8, i8, i8 }
5
6 define i32 @a() {
7 entry:
8 %c = alloca %struct.anon ; <%struct.anon*> [#uses=2]
9 %tmp = getelementptr %struct.anon, %struct.anon* %c, i32 0, i32 0 ; [#uses=1]
10 %tmp1 = getelementptr i32, i32* %tmp, i32 1 ; [#uses=2]
11 %tmp2 = load i32, i32* %tmp1, align 4 ; [#uses=1]
12 ; CHECK-NOT: load
13 %tmp3 = or i32 %tmp2, 11 ; [#uses=1]
14 %tmp4 = and i32 %tmp3, -21 ; [#uses=1]
15 store i32 %tmp4, i32* %tmp1, align 4
16 %call = call i32 (...) @x( %struct.anon* %c ) ; [#uses=0]
17 ret i32 undef
18 }
19
20
21 declare i32 @x(...)
0 ; RUN: opt < %s -newgvn
1 ; PR2032
2
3 define i32 @sscal(i32 %n, double %sa1, float* %sx, i32 %incx) {
4 entry:
5 %sx_addr = alloca float* ; [#uses=3]
6 store float* %sx, float** %sx_addr, align 4
7 br label %bb33
8
9 bb: ; preds = %bb33
10 %tmp27 = load float*, float** %sx_addr, align 4 ; [#uses=1]
11 store float 0.000000e+00, float* %tmp27, align 4
12 store float* null, float** %sx_addr, align 4
13 br label %bb33
14
15 bb33: ; preds = %bb, %entry
16 br i1 false, label %bb, label %return
17
18 return: ; preds = %bb33
19 %retval59 = load i32, i32* null, align 4 ; [#uses=1]
20 ret i32 %retval59
21