summaryrefslogtreecommitdiff
path: root/lib/Transforms/Scalar/PredicateSimplifier.cpp
blob: 6578bccff12e1c32deefe8c1183f03bbbd30efef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
//===-- PredicateSimplifier.cpp - Path Sensitive Simplifier ---------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by Nick Lewycky and is distributed under the
// University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Path-sensitive optimizer. In a branch where x == y, replace uses of
// x with y. Permits further optimization, such as the elimination of
// the unreachable call:
//
// void test(int *p, int *q)
// {
//   if (p != q)
//     return;
// 
//   if (*p != *q)
//     foo(); // unreachable
// }
//
//===----------------------------------------------------------------------===//
//
// This pass focusses on four properties; equals, not equals, less-than
// and less-than-or-equals-to. The greater-than forms are also held just
// to allow walking from a lesser node to a greater one. These properties
// are stored in a lattice; LE can become LT or EQ, NE can become LT or GT.
//
// These relationships define a graph between values of the same type. Each
// Value is stored in a map table that retrieves the associated Node. This
// is how EQ relationships are stored; the map contains pointers to the
// same node. The node contains a most canonical Value* form and the list of
// known relationships.
//
// If two nodes are known to be inequal, then they will contain pointers to
// each other with an "NE" relationship. If node getNode(%x) is less than
// getNode(%y), then the %x node will contain <%y, GT> and %y will contain
// <%x, LT>. This allows us to tie nodes together into a graph like this:
//
//   %a < %b < %c < %d
//
// with four nodes representing the properties. The InequalityGraph provides
// queries (such as "isEqual") and mutators (such as "addEqual"). To implement
// "isLess(%a, %c)", we start with getNode(%c) and walk downwards until
// we reach %a or the leaf node. Note that the graph is directed and acyclic,
// but may contain joins, meaning that this walk is not a linear time
// algorithm.
//
// To create these properties, we wait until a branch or switch instruction
// implies that a particular value is true (or false). The VRPSolver is
// responsible for analyzing the variable and seeing what new inferences
// can be made from each property. For example:
//
//   %P = seteq int* %ptr, null
//   %a = or bool %P, %Q
//   br bool %a label %cond_true, label %cond_false
//
// For the true branch, the VRPSolver will start with %a EQ true and look at
// the definition of %a and find that it can infer that %P and %Q are both
// true. From %P being true, it can infer that %ptr NE null. For the false
// branch it can't infer anything from the "or" instruction.
//
// Besides branches, we can also infer properties from instruction that may
// have undefined behaviour in certain cases. For example, the dividend of
// a division may never be zero. After the division instruction, we may assume
// that the dividend is not equal to zero.
//
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "predsimplify"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h"
#include "llvm/Pass.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Analysis/Dominators.h"
#include "llvm/Analysis/ET-Forest.h"
#include "llvm/Assembly/Writer.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/InstVisitor.h"
#include "llvm/Transforms/Utils/Local.h"
#include <algorithm>
#include <deque>
#include <sstream>
#include <map>
using namespace llvm;

STATISTIC(NumVarsReplaced, "Number of argument substitutions");
STATISTIC(NumInstruction , "Number of instructions removed");
STATISTIC(NumSimple      , "Number of simple replacements");

namespace {
  /// The InequalityGraph stores the relationships between values.
  /// Each Value in the graph is assigned to a Node. Nodes are pointer
  /// comparable for equality. The caller is expected to maintain the logical
  /// consistency of the system.
  ///
  /// The InequalityGraph class may invalidate Node*s after any mutator call.
  /// @brief The InequalityGraph stores the relationships between values.
  class VISIBILITY_HIDDEN InequalityGraph {
  public:
    class Node;

    // LT GT EQ
    //  0  0  0 -- invalid (false)
    //  0  0  1 -- invalid (EQ)
    //  0  1  0 -- GT
    //  0  1  1 -- GE
    //  1  0  0 -- LT
    //  1  0  1 -- LE
    //  1  1  0 -- NE
    //  1  1  1 -- invalid (true)
    enum LatticeBits {
      EQ_BIT = 1, GT_BIT = 2, LT_BIT = 4
    };
    enum LatticeVal {
      GT = GT_BIT, GE = GT_BIT | EQ_BIT,
      LT = LT_BIT, LE = LT_BIT | EQ_BIT,
      NE = GT_BIT | LT_BIT
    };

    static bool validPredicate(LatticeVal LV) {
      return LV > 1 && LV < 7;
    }

  private:
    typedef std::map<Value *, Node *> NodeMapType;
    NodeMapType Nodes;

    const InequalityGraph *ConcreteIG;

  public:
    /// A single node in the InequalityGraph. This stores the canonical Value
    /// for the node, as well as the relationships with the neighbours.
    ///
    /// Because the lists are intended to be used for traversal, it is invalid
    /// for the node to list itself in LessEqual or GreaterEqual lists. The
    /// fact that a node is equal to itself is implied, and may be checked
    /// with pointer comparison.
    /// @brief A single node in the InequalityGraph.
    class VISIBILITY_HIDDEN Node {
      friend class InequalityGraph;

      Value *Canonical;

      typedef SmallVector<std::pair<Node *, LatticeVal>, 4> RelationsType;
      RelationsType Relations;
    public:
      typedef RelationsType::iterator       iterator;
      typedef RelationsType::const_iterator const_iterator;

    private:
      /// Updates the lattice value for a given node. Create a new entry if
      /// one doesn't exist, otherwise it merges the values. The new lattice
      /// value must not be inconsistent with any previously existing value.
      void update(Node *N, LatticeVal R) {
        iterator I = find(N);
        if (I == end()) {
          Relations.push_back(std::make_pair(N, R));
        } else {
          I->second = static_cast<LatticeVal>(I->second & R);
          assert(validPredicate(I->second) &&
                 "Invalid union of lattice values.");
        }
      }

      void assign(Node *N, LatticeVal R) {
        iterator I = find(N);
        if (I != end()) I->second = R;

        Relations.push_back(std::make_pair(N, R));
      }

    public:
      iterator begin()       { return Relations.begin(); }
      iterator end()         { return Relations.end();   }
      iterator find(Node *N) {
        iterator I = begin();
        for (iterator E = end(); I != E; ++I)
          if (I->first == N) break;
        return I;
      }

      const_iterator begin()       const { return Relations.begin(); }
      const_iterator end()         const { return Relations.end();   }
      const_iterator find(Node *N) const {
        const_iterator I = begin();
        for (const_iterator E = end(); I != E; ++I)
          if (I->first == N) break;
        return I;
      }

      unsigned findIndex(Node *N) {
        unsigned i = 0;
        iterator I = begin();
        for (iterator E = end(); I != E; ++I, ++i)
          if (I->first == N) return i;
        return (unsigned)-1;
      }

      void erase(iterator i) { Relations.erase(i); }

      Value *getValue() const { return Canonical; }
      void setValue(Value *V) { Canonical = V; }

      void addNotEqual(Node *N)     { update(N, NE); }
      void addLess(Node *N)         { update(N, LT); }
      void addLessEqual(Node *N)    { update(N, LE); }
      void addGreater(Node *N)      { update(N, GT); }
      void addGreaterEqual(Node *N) { update(N, GE); }
    };

    InequalityGraph() : ConcreteIG(NULL) {}

    InequalityGraph(const InequalityGraph &_IG) {
#if 0 // disable COW
      if (_IG.ConcreteIG) ConcreteIG = _IG.ConcreteIG;
      else ConcreteIG = &_IG;
#else
      ConcreteIG = &_IG;
      materialize();
#endif
    }

    ~InequalityGraph();

  private:
    void materialize();

  public:
    /// If the Value is in the graph, return the canonical form. Otherwise,
    /// return the original Value.
    Value *canonicalize(Value *V) const {  
      if (const Node *N = getNode(V))
        return N->getValue();
      else 
        return V;
    }

    /// Returns the node currently representing Value V, or null if no such
    /// node exists.
    Node *getNode(Value *V) {
      materialize();

      NodeMapType::const_iterator I = Nodes.find(V);
      return (I != Nodes.end()) ? I->second : 0;
    }

    const Node *getNode(Value *V) const {
      if (ConcreteIG) return ConcreteIG->getNode(V);

      NodeMapType::const_iterator I = Nodes.find(V);
      return (I != Nodes.end()) ? I->second : 0;
    }

    Node *getOrInsertNode(Value *V) {
      if (Node *N = getNode(V))
        return N;
      else
        return newNode(V);
    }

    Node *newNode(Value *V) {
      //DOUT << "new node: " << *V << "\n";
      materialize();
      Node *&N = Nodes[V];
      assert(N == 0 && "Node already exists for value.");
      N = new Node();
      N->setValue(V);
      return N;
    }

    /// Returns true iff the nodes are provably inequal.
    bool isNotEqual(const Node *N1, const Node *N2) const {
      if (N1 == N2) return false;
      for (Node::const_iterator I = N1->begin(), E = N1->end(); I != E; ++I) {
        if (I->first == N2)
          return (I->second & EQ_BIT) == 0;
      }
      return isLess(N1, N2) || isGreater(N1, N2);
    }

    /// Returns true iff N1 is provably less than N2.
    bool isLess(const Node *N1, const Node *N2) const {
      if (N1 == N2) return false;
      for (Node::const_iterator I = N2->begin(), E = N2->end(); I != E; ++I) {
        if (I->first == N1)
          return I->second == LT;
      }
      for (Node::const_iterator I = N2->begin(), E = N2->end(); I != E; ++I) {
        if ((I->second & (LT_BIT | GT_BIT)) == LT_BIT)
          if (isLess(N1, I->first)) return true;
      }
      return false;
    }

    /// Returns true iff N1 is provably less than or equal to N2.
    bool isLessEqual(const Node *N1, const Node *N2) const {
      if (N1 == N2) return true;
      for (Node::const_iterator I = N2->begin(), E = N2->end(); I != E; ++I) {
        if (I->first == N1)
          return (I->second & (LT_BIT | GT_BIT)) == LT_BIT;
      }
      for (Node::const_iterator I = N2->begin(), E = N2->end(); I != E; ++I) {
        if ((I->second & (LT_BIT | GT_BIT)) == LT_BIT)
          if (isLessEqual(N1, I->first)) return true;
      }
      return false;
    }

    /// Returns true iff N1 is provably greater than N2.
    bool isGreater(const Node *N1, const Node *N2) const {
      return isLess(N2, N1);
    }

    /// Returns true iff N1 is provably greater than or equal to N2.
    bool isGreaterEqual(const Node *N1, const Node *N2) const {
      return isLessEqual(N2, N1);
    }

    // The add* methods assume that your input is logically valid and may 
    // assertion-fail or infinitely loop if you attempt a contradiction.

    void addEqual(Node *N, Value *V) {
      materialize();
      Nodes[V] = N;
    }

    void addNotEqual(Node *N1, Node *N2) {
      assert(N1 != N2 && "A node can't be inequal to itself.");
      materialize();
      N1->addNotEqual(N2);
      N2->addNotEqual(N1);
    }

    /// N1 is less than N2.
    void addLess(Node *N1, Node *N2) {
      assert(N1 != N2 && !isLess(N2, N1) && "Attempt to create < cycle.");
      materialize();
      N2->addLess(N1);
      N1->addGreater(N2);
    }

    /// N1 is less than or equal to N2.
    void addLessEqual(Node *N1, Node *N2) {
      assert(N1 != N2 && "Nodes are equal. Use mergeNodes instead.");
      assert(!isGreater(N1, N2) && "Impossible: Adding x <= y when x > y.");
      materialize();
      N2->addLessEqual(N1);
      N1->addGreaterEqual(N2);
    }

    /// Find the transitive closure starting at a node walking down the edges
    /// of type Val. Type Inserter must be an inserter that accepts Node *.
    template <typename Inserter>
    void transitiveClosure(Node *N, LatticeVal Val, Inserter insert) {
      for (Node::iterator I = N->begin(), E = N->end(); I != E; ++I) {
        if (I->second == Val) {
          *insert = I->first;
          transitiveClosure(I->first, Val, insert);
        }
      }
    }

    /// Kills off all the nodes in Kill by replicating their properties into
    /// node N. The elements of Kill must be unique. After merging, N's new
    /// canonical value is NewCanonical. Type C must be a container of Node *.
    template <typename C>
    void mergeNodes(Node *N, C &Kill, Value *NewCanonical);

    /// Removes a Value from the graph, but does not delete any nodes. As this
    /// method does not delete Nodes, V may not be the canonical choice for
    /// any node.
    void remove(Value *V) {
      materialize();

      for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E;) {
        NodeMapType::iterator J = I++;
        assert(J->second->getValue() != V && "Can't delete canonical choice.");
        if (J->first == V) Nodes.erase(J);
      }
    }

#ifndef NDEBUG
    void debug(std::ostream &os) const {
    std::set<Node *> VisitedNodes;
    for (NodeMapType::const_iterator I = Nodes.begin(), E = Nodes.end();
         I != E; ++I) {
      Node *N = I->second;
      os << *I->first << " == " << *N->getValue() << "\n";
      if (VisitedNodes.insert(N).second) {
        os << *N->getValue() << ":\n";
        for (Node::const_iterator NI = N->begin(), NE = N->end();
             NI != NE; ++NI) {
          static const std::string names[8] =
              { "00", "01", " <", "<=", " >", ">=", "!=", "07" };
          os << "  " << names[NI->second] << " "
             << *NI->first->getValue() << "\n";
        }
      }
    }
  }
#endif
  };

  InequalityGraph::~InequalityGraph() {
    if (ConcreteIG) return;

    std::vector<Node *> Remove;
    for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end();
         I != E; ++I) {
      if (I->first == I->second->getValue())
        Remove.push_back(I->second);
    }
    for (std::vector<Node *>::iterator I = Remove.begin(), E = Remove.end();
         I != E; ++I) {
      delete *I;
    }
  }

  template <typename C>
  void InequalityGraph::mergeNodes(Node *N, C &Kill, Value *NewCanonical) {
    materialize();

    // Merge the relationships from the members of Kill into N.
    for (typename C::iterator KI = Kill.begin(), KE = Kill.end();
         KI != KE; ++KI) {

      for (Node::iterator I = (*KI)->begin(), E = (*KI)->end(); I != E; ++I) {
        if (I->first == N) continue;

        Node::iterator NI = N->find(I->first);
        if (NI == N->end()) {
          N->Relations.push_back(std::make_pair(I->first, I->second));
        } else {
          unsigned char LV = NI->second & I->second;
          if (LV == EQ_BIT) {

            assert(std::find(Kill.begin(), Kill.end(), I->first) != Kill.end()
                    && "Lost EQ property.");
            N->erase(NI);
          } else {
            NI->second = static_cast<LatticeVal>(LV);
            assert(InequalityGraph::validPredicate(NI->second) &&
                   "Invalid union of lattice values.");
          }
        }

        // All edges are reciprocal; every Node that Kill points to also
        // contains a pointer to Kill. Replace those with pointers with N.
        unsigned iter = I->first->findIndex(*KI);
        assert(iter != (unsigned)-1 && "Edge not reciprocal.");
        I->first->assign(N, (I->first->begin()+iter)->second);
        I->first->erase(I->first->begin()+iter);
      }

      // Removing references from N to Kill.
      Node::iterator NI = N->find(*KI);
      if (NI != N->end()) {
        N->erase(NI); // breaks reciprocity until Kill is deleted.
      }
    }

    N->setValue(NewCanonical);

    // Update value mapping to point to the merged node.
    for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end();
         I != E; ++I) {
      if (std::find(Kill.begin(), Kill.end(), I->second) != Kill.end())
        I->second = N;
    }

    for (typename C::iterator KI = Kill.begin(), KE = Kill.end();
         KI != KE; ++KI) {
      delete *KI;
    }
  }

  void InequalityGraph::materialize() {
    if (!ConcreteIG) return;
    const InequalityGraph *IG = ConcreteIG;
    ConcreteIG = NULL;

    for (NodeMapType::const_iterator I = IG->Nodes.begin(),
         E = IG->Nodes.end(); I != E; ++I) {
      if (I->first == I->second->getValue()) {
        Node *N = newNode(I->first);
        N->Relations.reserve(N->Relations.size());
      }
    }
    for (NodeMapType::const_iterator I = IG->Nodes.begin(),
         E = IG->Nodes.end(); I != E; ++I) {
      if (I->first != I->second->getValue()) {
        Nodes[I->first] = getNode(I->second->getValue());
      } else {
        Node *Old = I->second;
        Node *N = getNode(I->first);
        for (Node::const_iterator NI = Old->begin(), NE = Old->end();
             NI != NE; ++NI) {
          N->assign(getNode(NI->first->getValue()), NI->second);
        }
      }
    }
  }

  /// VRPSolver keeps track of how changes to one variable affect other
  /// variables, and forwards changes along to the InequalityGraph. It
  /// also maintains the correct choice for "canonical" in the IG.
  /// @brief VRPSolver calculates inferences from a new relationship.
  class VISIBILITY_HIDDEN VRPSolver {
  private:
    std::deque<Instruction *> WorkList;

    InequalityGraph &IG;
    const InequalityGraph &cIG;
    ETForest *Forest;
    ETNode *Top;

    typedef InequalityGraph::Node Node;

    /// Returns true if V1 is a better canonical value than V2.
    bool compare(Value *V1, Value *V2) const {
      if (isa<Constant>(V1))
        return !isa<Constant>(V2);
      else if (isa<Constant>(V2))
        return false;
      else if (isa<Argument>(V1))
        return !isa<Argument>(V2);
      else if (isa<Argument>(V2))
        return false;

      Instruction *I1 = dyn_cast<Instruction>(V1);
      Instruction *I2 = dyn_cast<Instruction>(V2);

      if (!I1 || !I2) return false;

      BasicBlock *BB1 = I1->getParent(),
                 *BB2 = I2->getParent();
      if (BB1 == BB2) {
        for (BasicBlock::const_iterator I = BB1->begin(), E = BB1->end();
             I != E; ++I) {
          if (&*I == I1) return true;
          if (&*I == I2) return false;
        }
        assert(!"Instructions not found in parent BasicBlock?");
      } else {
        return Forest->properlyDominates(BB1, BB2);
      }
      return false;
    }

    void addToWorklist(Instruction *I) {
      //DOUT << "addToWorklist: " << *I << "\n";

      if (!isa<BinaryOperator>(I) && !isa<SelectInst>(I) && !isa<CmpInst>(I)) 
        return;

      const Type *Ty = I->getType();
      if (Ty == Type::VoidTy || Ty->isFPOrFPVector()) return;

      if (isInstructionTriviallyDead(I)) return;

      WorkList.push_back(I);
    }

    void addRecursive(Value *V) {
      //DOUT << "addRecursive: " << *V << "\n";

      Instruction *I = dyn_cast<Instruction>(V);
      if (I)
        addToWorklist(I);
      else if (!isa<Argument>(V))
        return;

      //DOUT << "addRecursive uses...\n";
      for (Value::use_iterator UI = V->use_begin(), UE = V->use_end();
           UI != UE; ++UI) {
        // Use must be either be dominated by Top, or dominate Top.
        if (Instruction *Inst = dyn_cast<Instruction>(*UI)) {
          ETNode *INode = Forest->getNodeForBlock(Inst->getParent());
          if (INode->DominatedBy(Top) || Top->DominatedBy(INode))
            addToWorklist(Inst);
        }
      }

      if (I) {
        //DOUT << "addRecursive ops...\n";
        for (User::op_iterator OI = I->op_begin(), OE = I->op_end();
             OI != OE; ++OI) {
          if (Instruction *Inst = dyn_cast<Instruction>(*OI))
            addToWorklist(Inst);
        }
      }
      //DOUT << "exit addRecursive (" << *V << ").\n";
    }

  public:
    VRPSolver(InequalityGraph &IG, ETForest *Forest, BasicBlock *TopBB)
      : IG(IG), cIG(IG), Forest(Forest), Top(Forest->getNodeForBlock(TopBB)) {}

    bool isEqual(Value *V1, Value *V2) const {
      if (V1 == V2) return true;
      if (const Node *N1 = cIG.getNode(V1))
        return N1 == cIG.getNode(V2);
      return false;
    }

    bool isNotEqual(Value *V1, Value *V2) const {
      if (V1 == V2) return false;
      if (const Node *N1 = cIG.getNode(V1))
        if (const Node *N2 = cIG.getNode(V2))
          return cIG.isNotEqual(N1, N2);
      return false;
    }

    bool isLess(Value *V1, Value *V2) const {
      if (V1 == V2) return false;
      if (const Node *N1 = cIG.getNode(V1))
        if (const Node *N2 = cIG.getNode(V2))
          return cIG.isLess(N1, N2);
      return false;
    }

    bool isLessEqual(Value *V1, Value *V2) const {
      if (V1 == V2) return true;
      if (const Node *N1 = cIG.getNode(V1))
        if (const Node *N2 = cIG.getNode(V2))
          return cIG.isLessEqual(N1, N2);
      return false;
    }

    bool isGreater(Value *V1, Value *V2) const {
      if (V1 == V2) return false;
      if (const Node *N1 = cIG.getNode(V1))
        if (const Node *N2 = cIG.getNode(V2))
          return cIG.isGreater(N1, N2);
      return false;
    }

    bool isGreaterEqual(Value *V1, Value *V2) const {
      if (V1 == V2) return true;
      if (const Node *N1 = IG.getNode(V1))
        if (const Node *N2 = IG.getNode(V2))
          return cIG.isGreaterEqual(N1, N2);
      return false;
    }

    // All of the add* functions return true if the InequalityGraph represents
    // the property, and false if there is a logical contradiction. On false,
    // you may no longer perform any queries on the InequalityGraph.

    bool addEqual(Value *V1, Value *V2) {
      //DOUT << "addEqual(" << *V1 << ", " << *V2 << ")\n";
      if (isEqual(V1, V2)) return true;

      const Node *cN1 = cIG.getNode(V1), *cN2 = cIG.getNode(V2);

      if (cN1 && cN2 && cIG.isNotEqual(cN1, cN2))
          return false;

      if (compare(V2, V1)) { std::swap(V1, V2); std::swap(cN1, cN2); }

      if (cN1) {
        if (ConstantBool *CB = dyn_cast<ConstantBool>(V1)) {
          Node *N1 = IG.getNode(V1);
           
          // When "addEqual" is performed and the new value is a ConstantBool,
          // iterate through the NE set and fix them up to be EQ of the
          // opposite bool.

          for (Node::iterator I = N1->begin(), E = N1->end(); I != E; ++I)
            if ((I->second & 1) == 0) {
              assert(N1 != I->first && "Node related to itself?");
              addEqual(I->first->getValue(),
                       ConstantBool::get(!CB->getValue()));
            }
        }
      }

      if (!cN2) {
        if (Instruction *I2 = dyn_cast<Instruction>(V2)) {
          ETNode *Node_I2 = Forest->getNodeForBlock(I2->getParent());
          if (Top != Node_I2 && Node_I2->DominatedBy(Top)) {
            Value *V = V1;
            if (cN1 && compare(V1, cN1->getValue())) V = cN1->getValue();
            //DOUT << "Simply removing " << *I2
            //     << ", replacing with " << *V << "\n";
            I2->replaceAllUsesWith(V);
            // leave it dead; it'll get erased later.
            ++NumSimple;
            addRecursive(V1);
            return true;
          }
        }
      }

      Node *N1 = IG.getNode(V1), *N2 = IG.getNode(V2);

      if ( N1 && !N2) {
        IG.addEqual(N1, V2);
        if (compare(V1, N1->getValue())) N1->setValue(V1);
      }
      if (!N1 &&  N2) {
        IG.addEqual(N2, V1);
        if (compare(V1, N2->getValue())) N2->setValue(V1);
      }
      if ( N1 &&  N2) {
        // Suppose we're being told that %x == %y, and %x <= %z and %y >= %z.
        // We can't just merge %x and %y because the relationship with %z would
        // be EQ and that's invalid; they need to be the same Node.
        //
        // What we're doing is looking for any chain of nodes reaching %z such
        // that %x <= %z and %y >= %z, and vice versa. The cool part is that
        // every node in between is also equal because of the squeeze principle.

        std::vector<Node *> N1_GE, N2_LE, N1_LE, N2_GE;
        IG.transitiveClosure(N1, InequalityGraph::GE, back_inserter(N1_GE));
        std::sort(N1_GE.begin(), N1_GE.end());
        N1_GE.erase(std::unique(N1_GE.begin(), N1_GE.end()), N1_GE.end());
        IG.transitiveClosure(N2, InequalityGraph::LE, back_inserter(N2_LE));
        std::sort(N1_LE.begin(), N1_LE.end());
        N1_LE.erase(std::unique(N1_LE.begin(), N1_LE.end()), N1_LE.end());
        IG.transitiveClosure(N1, InequalityGraph::LE, back_inserter(N1_LE));
        std::sort(N2_GE.begin(), N2_GE.end());
        N2_GE.erase(std::unique(N2_GE.begin(), N2_GE.end()), N2_GE.end());
        std::unique(N2_GE.begin(), N2_GE.end());
        IG.transitiveClosure(N2, InequalityGraph::GE, back_inserter(N2_GE));
        std::sort(N2_LE.begin(), N2_LE.end());
        N2_LE.erase(std::unique(N2_LE.begin(), N2_LE.end()), N2_LE.end());

        std::vector<Node *> Set1, Set2;
        std::set_intersection(N1_GE.begin(), N1_GE.end(),
                              N2_LE.begin(), N2_LE.end(),
                              back_inserter(Set1));
        std::set_intersection(N1_LE.begin(), N1_LE.end(),
                              N2_GE.begin(), N2_GE.end(),
                              back_inserter(Set2));

        std::vector<Node *> Equal;
        std::set_union(Set1.begin(), Set1.end(), Set2.begin(), Set2.end(),
                       back_inserter(Equal));

        Value *Best = N1->getValue();
        if (compare(N2->getValue(), Best)) Best = N2->getValue();

        for (std::vector<Node *>::iterator I = Equal.begin(), E = Equal.end();
             I != E; ++I) {
          Value *V = (*I)->getValue();
          if (compare(V, Best)) Best = V;
        }

        Equal.push_back(N2);
        IG.mergeNodes(N1, Equal, Best);
      }
      if (!N1 && !N2) IG.addEqual(IG.newNode(V1), V2);

      addRecursive(V1);
      addRecursive(V2);

      return true;
    }

    bool addNotEqual(Value *V1, Value *V2) {
      //DOUT << "addNotEqual(" << *V1 << ", " << *V2 << ")\n");
      if (isNotEqual(V1, V2)) return true;

      // Never permit %x NE true/false.
      if (ConstantBool *B1 = dyn_cast<ConstantBool>(V1)) {
        return addEqual(ConstantBool::get(!B1->getValue()), V2);
      } else if (ConstantBool *B2 = dyn_cast<ConstantBool>(V2)) {
        return addEqual(V1, ConstantBool::get(!B2->getValue()));
      }

      Node *N1 = IG.getOrInsertNode(V1),
           *N2 = IG.getOrInsertNode(V2);

      if (N1 == N2) return false;

      IG.addNotEqual(N1, N2);

      addRecursive(V1);
      addRecursive(V2);

      return true;
    }

    /// Set V1 less than V2.
    bool addLess(Value *V1, Value *V2) {
      if (isLess(V1, V2)) return true;
      if (isGreaterEqual(V1, V2)) return false;

      Node *N1 = IG.getOrInsertNode(V1), *N2 = IG.getOrInsertNode(V2);

      if (N1 == N2) return false;

      IG.addLess(N1, N2);

      addRecursive(V1);
      addRecursive(V2);

      return true;
    }

    /// Set V1 less than or equal to V2.
    bool addLessEqual(Value *V1, Value *V2) {
      if (isLessEqual(V1, V2)) return true;
      if (V1 == V2) return true;

      if (isLessEqual(V2, V1))
        return addEqual(V1, V2);

      if (isGreater(V1, V2)) return false;

      Node *N1 = IG.getOrInsertNode(V1),
           *N2 = IG.getOrInsertNode(V2);

      if (N1 == N2) return true;

      IG.addLessEqual(N1, N2);

      addRecursive(V1);
      addRecursive(V2);

      return true;
    }

    void solve() {
      DOUT << "WorkList entry, size: " << WorkList.size() << "\n";
      while (!WorkList.empty()) {
        DOUT << "WorkList size: " << WorkList.size() << "\n";

        Instruction *I = WorkList.front();
        WorkList.pop_front();

        Value *Canonical = cIG.canonicalize(I);
        const Type *Ty = I->getType();

        //DOUT << "solving: " << *I << "\n";
        //DEBUG(IG.debug(*cerr.stream()));

        if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
          Value *Op0 = cIG.canonicalize(BO->getOperand(0)),
                *Op1 = cIG.canonicalize(BO->getOperand(1));

          ConstantIntegral *CI1 = dyn_cast<ConstantIntegral>(Op0),
                           *CI2 = dyn_cast<ConstantIntegral>(Op1);

          if (CI1 && CI2)
            addEqual(BO, ConstantExpr::get(BO->getOpcode(), CI1, CI2));

          switch (BO->getOpcode()) {
            case Instruction::And: {
              // "and int %a, %b"  EQ -1   then %a EQ -1   and %b EQ -1
              // "and bool %a, %b" EQ true then %a EQ true and %b EQ true
              ConstantIntegral *CI = ConstantIntegral::getAllOnesValue(Ty);
              if (Canonical == CI) {
                addEqual(CI, Op0);
                addEqual(CI, Op1);
              }
            } break;
            case Instruction::Or: {
              // "or int %a, %b"  EQ 0     then %a EQ 0     and %b EQ 0
              // "or bool %a, %b" EQ false then %a EQ false and %b EQ false
              Constant *Zero = Constant::getNullValue(Ty);
              if (Canonical == Zero) {
                addEqual(Zero, Op0);
                addEqual(Zero, Op1);
              }
            } break;
            case Instruction::Xor: {
              // "xor bool true,  %a" EQ true  then %a EQ false
              // "xor bool true,  %a" EQ false then %a EQ true
              // "xor bool false, %a" EQ true  then %a EQ true
              // "xor bool false, %a" EQ false then %a EQ false
              // "xor int %c, %a" EQ %c then %a EQ 0
              // "xor int %c, %a" NE %c then %a NE 0
              // 1. Repeat all of the above, with order of operands reversed.
              Value *LHS = Op0, *RHS = Op1;
              if (!isa<Constant>(LHS)) std::swap(LHS, RHS);

              if (ConstantBool *CB = dyn_cast<ConstantBool>(Canonical)) {
                if (ConstantBool *A = dyn_cast<ConstantBool>(LHS))
                  addEqual(RHS, ConstantBool::get(A->getValue() ^
                                                  CB->getValue()));
              }
              if (Canonical == LHS) {
                if (isa<ConstantIntegral>(Canonical))
                  addEqual(RHS, Constant::getNullValue(Ty));
              } else if (isNotEqual(LHS, Canonical)) {
                addNotEqual(RHS, Constant::getNullValue(Ty));
              }
            } break;
            default:
              break;
          }

          // "%x = add int %y, %z" and %x EQ %y then %z EQ 0
          // "%x = mul int %y, %z" and %x EQ %y then %z EQ 1
          // 1. Repeat all of the above, with order of operands reversed.
          // "%x = fdiv float %y, %z" and %x EQ %y then %z EQ 1
          Value *Known = Op0, *Unknown = Op1;
          if (Known != BO) std::swap(Known, Unknown);
          if (Known == BO) {
            switch (BO->getOpcode()) {
              default: break;
              case Instruction::Xor:
              case Instruction::Or:
              case Instruction::Add:
              case Instruction::Sub:
                if (!Ty->isFloatingPoint())
                  addEqual(Unknown, Constant::getNullValue(Ty));
                break;
              case Instruction::UDiv:
              case Instruction::SDiv:
              case Instruction::FDiv:
                if (Unknown == Op0) break; // otherwise, fallthrough
              case Instruction::And:
              case Instruction::Mul:
                Constant *One = NULL;
                if (isa<ConstantInt>(Unknown))
                  One = ConstantInt::get(Ty, 1);
                else if (isa<ConstantFP>(Unknown))
                  One = ConstantFP::get(Ty, 1);
                else if (isa<ConstantBool>(Unknown))
                  One = ConstantBool::getTrue();

                if (One) addEqual(Unknown, One);
                break;
            }
          }
        } else if (FCmpInst *CI = dyn_cast<FCmpInst>(I)) {
          Value *Op0 = cIG.canonicalize(CI->getOperand(0)),
                *Op1 = cIG.canonicalize(CI->getOperand(1));

          ConstantFP *CI1 = dyn_cast<ConstantFP>(Op0),
                     *CI2 = dyn_cast<ConstantFP>(Op1);

          if (CI1 && CI2)
            addEqual(CI, ConstantExpr::getFCmp(CI->getPredicate(), CI1, CI2));

          switch (CI->getPredicate()) {
            case FCmpInst::FCMP_OEQ:
            case FCmpInst::FCMP_UEQ:
              // "eq int %a, %b" EQ true  then %a EQ %b
              // "eq int %a, %b" EQ false then %a NE %b
              if (Canonical == ConstantBool::getTrue())
                addEqual(Op0, Op1);
              else if (Canonical == ConstantBool::getFalse())
                addNotEqual(Op0, Op1);

              // %a EQ %b then "eq int %a, %b" EQ true
              // %a NE %b then "eq int %a, %b" EQ false
              if (isEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getTrue());
              else if (isNotEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getFalse());

              break;
            case FCmpInst::FCMP_ONE:
            case FCmpInst::FCMP_UNE:
              // "ne int %a, %b" EQ true  then %a NE %b
              // "ne int %a, %b" EQ false then %a EQ %b
              if (Canonical == ConstantBool::getTrue())
                addNotEqual(Op0, Op1);
              else if (Canonical == ConstantBool::getFalse())
                addEqual(Op0, Op1);

              // %a EQ %b then "ne int %a, %b" EQ false
              // %a NE %b then "ne int %a, %b" EQ true
              if (isEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getFalse());
              else if (isNotEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getTrue());

              break;
            case FCmpInst::FCMP_ULT:
            case FCmpInst::FCMP_OLT:
              // "lt int %a, %b" EQ true  then %a LT %b
              // "lt int %a, %b" EQ false then %b LE %a
              if (Canonical == ConstantBool::getTrue())
                addLess(Op0, Op1);
              else if (Canonical == ConstantBool::getFalse())
                addLessEqual(Op1, Op0);

              // %a LT %b then "lt int %a, %b" EQ true
              // %a GE %b then "lt int %a, %b" EQ false
              if (isLess(Op0, Op1))
                addEqual(CI, ConstantBool::getTrue());
              else if (isGreaterEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getFalse());

              break;
            case FCmpInst::FCMP_ULE:
            case FCmpInst::FCMP_OLE:
              // "le int %a, %b" EQ true  then %a LE %b
              // "le int %a, %b" EQ false then %b LT %a
              if (Canonical == ConstantBool::getTrue())
                addLessEqual(Op0, Op1);
              else if (Canonical == ConstantBool::getFalse())
                addLess(Op1, Op0);

              // %a LE %b then "le int %a, %b" EQ true
              // %a GT %b then "le int %a, %b" EQ false
              if (isLessEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getTrue());
              else if (isGreater(Op0, Op1))
                addEqual(CI, ConstantBool::getFalse());

              break;
            case FCmpInst::FCMP_UGT:
            case FCmpInst::FCMP_OGT:
              // "gt int %a, %b" EQ true  then %b LT %a
              // "gt int %a, %b" EQ false then %a LE %b
              if (Canonical == ConstantBool::getTrue())
                addLess(Op1, Op0);
              else if (Canonical == ConstantBool::getFalse())
                addLessEqual(Op0, Op1);

              // %a GT %b then "gt int %a, %b" EQ true
              // %a LE %b then "gt int %a, %b" EQ false
              if (isGreater(Op0, Op1))
                addEqual(CI, ConstantBool::getTrue());
              else if (isLessEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getFalse());

              break;
            case FCmpInst::FCMP_UGE:
            case FCmpInst::FCMP_OGE:
              // "ge int %a, %b" EQ true  then %b LE %a
              // "ge int %a, %b" EQ false then %a LT %b
              if (Canonical == ConstantBool::getTrue())
                addLessEqual(Op1, Op0);
              else if (Canonical == ConstantBool::getFalse())
                addLess(Op0, Op1);

              // %a GE %b then "ge int %a, %b" EQ true
              // %a LT %b then "lt int %a, %b" EQ false
              if (isGreaterEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getTrue());
              else if (isLess(Op0, Op1))
                addEqual(CI, ConstantBool::getFalse());

              break;
            default:
              break;
          }

          // "%x = add int %y, %z" and %x EQ %y then %z EQ 0
          // "%x = mul int %y, %z" and %x EQ %y then %z EQ 1
          // 1. Repeat all of the above, with order of operands reversed.
          // "%x = fdiv float %y, %z" and %x EQ %y then %z EQ 1
          Value *Known = Op0, *Unknown = Op1;
          if (Known != BO) std::swap(Known, Unknown);
        } else if (ICmpInst *CI = dyn_cast<ICmpInst>(I)) {
          Value *Op0 = cIG.canonicalize(CI->getOperand(0)),
                *Op1 = cIG.canonicalize(CI->getOperand(1));

          ConstantIntegral *CI1 = dyn_cast<ConstantIntegral>(Op0),
                           *CI2 = dyn_cast<ConstantIntegral>(Op1);

          if (CI1 && CI2)
            addEqual(CI, ConstantExpr::getICmp(CI->getPredicate(), CI1, CI2));

          switch (CI->getPredicate()) {
            case ICmpInst::ICMP_EQ:
              // "eq int %a, %b" EQ true  then %a EQ %b
              // "eq int %a, %b" EQ false then %a NE %b
              if (Canonical == ConstantBool::getTrue())
                addEqual(Op0, Op1);
              else if (Canonical == ConstantBool::getFalse())
                addNotEqual(Op0, Op1);

              // %a EQ %b then "eq int %a, %b" EQ true
              // %a NE %b then "eq int %a, %b" EQ false
              if (isEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getTrue());
              else if (isNotEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getFalse());

              break;
            case ICmpInst::ICMP_NE:
              // "ne int %a, %b" EQ true  then %a NE %b
              // "ne int %a, %b" EQ false then %a EQ %b
              if (Canonical == ConstantBool::getTrue())
                addNotEqual(Op0, Op1);
              else if (Canonical == ConstantBool::getFalse())
                addEqual(Op0, Op1);

              // %a EQ %b then "ne int %a, %b" EQ false
              // %a NE %b then "ne int %a, %b" EQ true
              if (isEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getFalse());
              else if (isNotEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getTrue());

              break;
            case ICmpInst::ICMP_ULT:
            case ICmpInst::ICMP_SLT:
              // "lt int %a, %b" EQ true  then %a LT %b
              // "lt int %a, %b" EQ false then %b LE %a
              if (Canonical == ConstantBool::getTrue())
                addLess(Op0, Op1);
              else if (Canonical == ConstantBool::getFalse())
                addLessEqual(Op1, Op0);

              // %a LT %b then "lt int %a, %b" EQ true
              // %a GE %b then "lt int %a, %b" EQ false
              if (isLess(Op0, Op1))
                addEqual(CI, ConstantBool::getTrue());
              else if (isGreaterEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getFalse());

              break;
            case ICmpInst::ICMP_ULE:
            case ICmpInst::ICMP_SLE:
              // "le int %a, %b" EQ true  then %a LE %b
              // "le int %a, %b" EQ false then %b LT %a
              if (Canonical == ConstantBool::getTrue())
                addLessEqual(Op0, Op1);
              else if (Canonical == ConstantBool::getFalse())
                addLess(Op1, Op0);

              // %a LE %b then "le int %a, %b" EQ true
              // %a GT %b then "le int %a, %b" EQ false
              if (isLessEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getTrue());
              else if (isGreater(Op0, Op1))
                addEqual(CI, ConstantBool::getFalse());

              break;
            case ICmpInst::ICMP_UGT:
            case ICmpInst::ICMP_SGT:
              // "gt int %a, %b" EQ true  then %b LT %a
              // "gt int %a, %b" EQ false then %a LE %b
              if (Canonical == ConstantBool::getTrue())
                addLess(Op1, Op0);
              else if (Canonical == ConstantBool::getFalse())
                addLessEqual(Op0, Op1);

              // %a GT %b then "gt int %a, %b" EQ true
              // %a LE %b then "gt int %a, %b" EQ false
              if (isGreater(Op0, Op1))
                addEqual(CI, ConstantBool::getTrue());
              else if (isLessEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getFalse());

              break;
            case ICmpInst::ICMP_UGE:
            case ICmpInst::ICMP_SGE:
              // "ge int %a, %b" EQ true  then %b LE %a
              // "ge int %a, %b" EQ false then %a LT %b
              if (Canonical == ConstantBool::getTrue())
                addLessEqual(Op1, Op0);
              else if (Canonical == ConstantBool::getFalse())
                addLess(Op0, Op1);

              // %a GE %b then "ge int %a, %b" EQ true
              // %a LT %b then "lt int %a, %b" EQ false
              if (isGreaterEqual(Op0, Op1))
                addEqual(CI, ConstantBool::getTrue());
              else if (isLess(Op0, Op1))
                addEqual(CI, ConstantBool::getFalse());

              break;
            default:
              break;
          }

          // "%x = add int %y, %z" and %x EQ %y then %z EQ 0
          // "%x = mul int %y, %z" and %x EQ %y then %z EQ 1
          // 1. Repeat all of the above, with order of operands reversed.
          // "%x = fdiv float %y, %z" and %x EQ %y then %z EQ 1
          Value *Known = Op0, *Unknown = Op1;
          if (Known != BO) std::swap(Known, Unknown);
        } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
          // Given: "%a = select bool %x, int %b, int %c"
          // %a EQ %b then %x EQ true
          // %a EQ %c then %x EQ false
          if (isEqual(I, SI->getTrueValue()) ||
              isNotEqual(I, SI->getFalseValue()))
            addEqual(SI->getCondition(), ConstantBool::getTrue());
          else if (isEqual(I, SI->getFalseValue()) ||
                   isNotEqual(I, SI->getTrueValue()))
            addEqual(SI->getCondition(), ConstantBool::getFalse());

          // %x EQ true  then %a EQ %b
          // %x EQ false then %a NE %b
          if (isEqual(SI->getCondition(), ConstantBool::getTrue()))
            addEqual(SI, SI->getTrueValue());
          else if (isEqual(SI->getCondition(), ConstantBool::getFalse()))
            addEqual(SI, SI->getFalseValue());
        }
      }
    }
  };

  /// PredicateSimplifier - This class is a simplifier that replaces
  /// one equivalent variable with another. It also tracks what
  /// can't be equal and will solve setcc instructions when possible.
  /// @brief Root of the predicate simplifier optimization.
  class VISIBILITY_HIDDEN PredicateSimplifier : public FunctionPass {
    DominatorTree *DT;
    ETForest *Forest;
    bool modified;

    class State {
    public:
      BasicBlock *ToVisit;
      InequalityGraph *IG;

      State(BasicBlock *BB, InequalityGraph *IG) : ToVisit(BB), IG(IG) {}
    };

    std::vector<State> WorkList;

  public:
    bool runOnFunction(Function &F);

    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
      AU.addRequiredID(BreakCriticalEdgesID);
      AU.addRequired<DominatorTree>();
      AU.addRequired<ETForest>();
      AU.setPreservesCFG();
      AU.addPreservedID(BreakCriticalEdgesID);
    }

  private:
    /// Forwards - Adds new properties into PropertySet and uses them to
    /// simplify instructions. Because new properties sometimes apply to
    /// a transition from one BasicBlock to another, this will use the
    /// PredicateSimplifier::proceedToSuccessor(s) interface to enter the
    /// basic block with the new PropertySet.
    /// @brief Performs abstract execution of the program.
    class VISIBILITY_HIDDEN Forwards : public InstVisitor<Forwards> {
      friend class InstVisitor<Forwards>;
      PredicateSimplifier *PS;

    public:
      InequalityGraph &IG;

      Forwards(PredicateSimplifier *PS, InequalityGraph &IG)
        : PS(PS), IG(IG) {}

      void visitTerminatorInst(TerminatorInst &TI);
      void visitBranchInst(BranchInst &BI);
      void visitSwitchInst(SwitchInst &SI);

      void visitAllocaInst(AllocaInst &AI);
      void visitLoadInst(LoadInst &LI);
      void visitStoreInst(StoreInst &SI);

      void visitBinaryOperator(BinaryOperator &BO);
      void visitCmpInst(CmpInst &CI) {}
    };

    // Used by terminator instructions to proceed from the current basic
    // block to the next. Verifies that "current" dominates "next",
    // then calls visitBasicBlock.
    void proceedToSuccessors(const InequalityGraph &IG, BasicBlock *BBCurrent) {
      DominatorTree::Node *Current = DT->getNode(BBCurrent);
      for (DominatorTree::Node::iterator I = Current->begin(),
           E = Current->end(); I != E; ++I) {
        //visitBasicBlock((*I)->getBlock(), IG);
        WorkList.push_back(State((*I)->getBlock(), new InequalityGraph(IG)));
      }
    }

    void proceedToSuccessor(InequalityGraph *NextIG, BasicBlock *Next) {
      //visitBasicBlock(Next, NextIG);
      WorkList.push_back(State(Next, NextIG));
    }

    // Visits each instruction in the basic block.
    void visitBasicBlock(BasicBlock *BB, InequalityGraph &IG) {
      DOUT << "Entering Basic Block: " << BB->getName() << "\n";
      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
        visitInstruction(I++, IG);
      }
    }

    // Tries to simplify each Instruction and add new properties to
    // the PropertySet.
    void visitInstruction(Instruction *I, InequalityGraph &IG) {
      DOUT << "Considering instruction " << *I << "\n";
      DEBUG(IG.debug(*cerr.stream()));

      // Sometimes instructions are made dead due to earlier analysis.
      if (isInstructionTriviallyDead(I)) {
        I->eraseFromParent();
        return;
      }

      // Try to replace the whole instruction.
      Value *V = IG.canonicalize(I);
      if (V != I) {
        modified = true;
        ++NumInstruction;
        DOUT << "Removing " << *I << ", replacing with " << *V << "\n";
        IG.remove(I);
        I->replaceAllUsesWith(V);
        I->eraseFromParent();
        return;
      }

      // Try to substitute operands.
      for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
        Value *Oper = I->getOperand(i);
        Value *V = IG.canonicalize(Oper);
        if (V != Oper) {
          modified = true;
          ++NumVarsReplaced;
          DOUT << "Resolving " << *I;
          I->setOperand(i, V);
          DOUT << " into " << *I;
        }
      }

      //DOUT << "push (%" << I->getParent()->getName() << ")\n";
      Forwards visit(this, IG);
      visit.visit(*I);
      //DOUT << "pop (%" << I->getParent()->getName() << ")\n";
    }
  };

  bool PredicateSimplifier::runOnFunction(Function &F) {
    DT = &getAnalysis<DominatorTree>();
    Forest = &getAnalysis<ETForest>();

    DOUT << "Entering Function: " << F.getName() << "\n";

    modified = false;
    WorkList.push_back(State(DT->getRoot(), new InequalityGraph()));

    do {
      State S = WorkList.back();
      WorkList.pop_back();
      visitBasicBlock(S.ToVisit, *S.IG);
      delete S.IG;
    } while (!WorkList.empty());

    //DEBUG(F.viewCFG());

    return modified;
  }

  void PredicateSimplifier::Forwards::visitTerminatorInst(TerminatorInst &TI) {
    PS->proceedToSuccessors(IG, TI.getParent());
  }

  void PredicateSimplifier::Forwards::visitBranchInst(BranchInst &BI) {
    BasicBlock *BB = BI.getParent();

    if (BI.isUnconditional()) {
      PS->proceedToSuccessors(IG, BB);
      return;
    }

    Value *Condition = BI.getCondition();
    BasicBlock *TrueDest  = BI.getSuccessor(0),
               *FalseDest = BI.getSuccessor(1);

    if (isa<ConstantBool>(Condition) || TrueDest == FalseDest) {
      PS->proceedToSuccessors(IG, BB);
      return;
    }

    DominatorTree::Node *Node = PS->DT->getNode(BB);
    for (DominatorTree::Node::iterator I = Node->begin(), E = Node->end();
         I != E; ++I) {
      BasicBlock *Dest = (*I)->getBlock();
      InequalityGraph *DestProperties = new InequalityGraph(IG);
      VRPSolver Solver(*DestProperties, PS->Forest, Dest);

      if (Dest == TrueDest) {
        DOUT << "(" << BB->getName() << ") true set:\n";
        if (!Solver.addEqual(ConstantBool::getTrue(), Condition)) continue;
        Solver.solve();
        DEBUG(DestProperties->debug(*cerr.stream()));
      } else if (Dest == FalseDest) {
        DOUT << "(" << BB->getName() << ") false set:\n";
        if (!Solver.addEqual(ConstantBool::getFalse(), Condition)) continue;
        Solver.solve();
        DEBUG(DestProperties->debug(*cerr.stream()));
      }

      PS->proceedToSuccessor(DestProperties, Dest);
    }
  }

  void PredicateSimplifier::Forwards::visitSwitchInst(SwitchInst &SI) {
    Value *Condition = SI.getCondition();

    // Set the EQProperty in each of the cases BBs, and the NEProperties
    // in the default BB.
    // InequalityGraph DefaultProperties(IG);

    DominatorTree::Node *Node = PS->DT->getNode(SI.getParent());
    for (DominatorTree::Node::iterator I = Node->begin(), E = Node->end();
         I != E; ++I) {
      BasicBlock *BB = (*I)->getBlock();

      InequalityGraph *BBProperties = new InequalityGraph(IG);
      VRPSolver Solver(*BBProperties, PS->Forest, BB);
      if (BB == SI.getDefaultDest()) {
        for (unsigned i = 1, e = SI.getNumCases(); i < e; ++i)
          if (SI.getSuccessor(i) != BB)
            if (!Solver.addNotEqual(Condition, SI.getCaseValue(i))) continue;
        Solver.solve();
      } else if (ConstantInt *CI = SI.findCaseDest(BB)) {
        if (!Solver.addEqual(Condition, CI)) continue;
        Solver.solve();
      }
      PS->proceedToSuccessor(BBProperties, BB);
    }
  }

  void PredicateSimplifier::Forwards::visitAllocaInst(AllocaInst &AI) {
    VRPSolver VRP(IG, PS->Forest, AI.getParent());
    VRP.addNotEqual(Constant::getNullValue(AI.getType()), &AI);
    VRP.solve();
  }

  void PredicateSimplifier::Forwards::visitLoadInst(LoadInst &LI) {
    Value *Ptr = LI.getPointerOperand();
    // avoid "load uint* null" -> null NE null.
    if (isa<Constant>(Ptr)) return;

    VRPSolver VRP(IG, PS->Forest, LI.getParent());
    VRP.addNotEqual(Constant::getNullValue(Ptr->getType()), Ptr);
    VRP.solve();
  }

  void PredicateSimplifier::Forwards::visitStoreInst(StoreInst &SI) {
    Value *Ptr = SI.getPointerOperand();
    if (isa<Constant>(Ptr)) return;

    VRPSolver VRP(IG, PS->Forest, SI.getParent());
    VRP.addNotEqual(Constant::getNullValue(Ptr->getType()), Ptr);
    VRP.solve();
  }

  void PredicateSimplifier::Forwards::visitBinaryOperator(BinaryOperator &BO) {
    Instruction::BinaryOps ops = BO.getOpcode();

    switch (ops) {
    case Instruction::URem:
    case Instruction::SRem:
    case Instruction::FRem:
    case Instruction::UDiv:
    case Instruction::SDiv:
    case Instruction::FDiv: {
      Value *Divisor = BO.getOperand(1);
      VRPSolver VRP(IG, PS->Forest, BO.getParent());
      VRP.addNotEqual(Constant::getNullValue(Divisor->getType()), Divisor);
      VRP.solve();
      break;
    }
    default:
      break;
    }
  }


  RegisterPass<PredicateSimplifier> X("predsimplify",
                                      "Predicate Simplifier");
}

FunctionPass *llvm::createPredicateSimplifierPass() {
  return new PredicateSimplifier();
}