klee
Expr.h
Go to the documentation of this file.
1//===-- Expr.h --------------------------------------------------*- C++ -*-===//
2//
3// The KLEE Symbolic Virtual Machine
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef KLEE_EXPR_H
11#define KLEE_EXPR_H
12
13#include "klee/ADT/Bits.h"
14#include "klee/ADT/Ref.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/raw_ostream.h"
21
22#include <sstream>
23#include <set>
24#include <vector>
25#include <map>
26
27namespace llvm {
28 class Type;
29 class raw_ostream;
30}
31
32namespace klee {
33
34class Array;
35class ArrayCache;
36class ConstantExpr;
37class ObjectState;
38
39template<class T> class ref;
40
41extern llvm::cl::OptionCategory ExprCat;
42
44
91class Expr {
92public:
93 static unsigned count;
94 static const unsigned MAGIC_HASH_CONSTANT = 39;
95
97 typedef unsigned Width;
98
99 static const Width InvalidWidth = 0;
100 static const Width Bool = 1;
101 static const Width Int8 = 8;
102 static const Width Int16 = 16;
103 static const Width Int32 = 32;
104 static const Width Int64 = 64;
105 static const Width Fl80 = 80;
106
107
108 enum Kind {
110
111 // Primitive
112
114
115 // Special
116
121
127
128 // Casting,
131
132 // Bit
134
135 // All subsequent kinds are binary.
136
137 // Arithmetic
145
146 // Bit
153
154 // Compare
165
167
174 };
175
178
179protected:
180 unsigned hashValue;
181
204 virtual int compareContents(const Expr &b) const = 0;
205
206public:
208 virtual ~Expr() { Expr::count--; }
209
210 virtual Kind getKind() const = 0;
211 virtual Width getWidth() const = 0;
212
213 virtual unsigned getNumKids() const = 0;
214 virtual ref<Expr> getKid(unsigned i) const = 0;
215
216 virtual void print(llvm::raw_ostream &os) const;
217
219 void dump() const;
220
222 virtual unsigned hash() const { return hashValue; }
223
226 virtual unsigned computeHash();
227
241 int compare(const Expr &b) const;
242
243 // Given an array of new kids return a copy of the expression
244 // but using those children.
245 virtual ref<Expr> rebuild(ref<Expr> kids[/* getNumKids() */]) const = 0;
246
248 bool isZero() const;
249
251 bool isTrue() const;
252
254 bool isFalse() const;
255
256 /* Static utility methods */
257
258 static void printKind(llvm::raw_ostream &os, Kind k);
259 static void printWidth(llvm::raw_ostream &os, Expr::Width w);
260
262 static inline unsigned getMinBytesForWidth(Width w) {
263 return (w + 7) / 8;
264 }
265
266 /* Kind utilities */
267
268 /* Utility creation functions */
271 static ref<Expr> createImplies(ref<Expr> hyp, ref<Expr> conc);
273
276 static ref<Expr> createTempRead(const Array *array, Expr::Width w);
277
278 static ref<ConstantExpr> createPointer(uint64_t v);
279
280 struct CreateArg;
281 static ref<Expr> createFromKind(Kind k, std::vector<CreateArg> args);
282
283 static bool isValidKidWidth(unsigned kid, Width w) { return true; }
284 static bool needsResultType() { return false; }
285
286 static bool classof(const Expr *) { return true; }
287
288private:
289 typedef llvm::DenseSet<std::pair<const Expr *, const Expr *> > ExprEquivSet;
290 int compare(const Expr &b, ExprEquivSet &equivs) const;
291};
292
296
297 CreateArg(Width w = Bool) : expr(0), width(w) {}
299
300 bool isExpr() { return !isWidth(); }
301 bool isWidth() { return width != Expr::InvalidWidth; }
302};
303
304// Comparison operators
305
306inline bool operator==(const Expr &lhs, const Expr &rhs) {
307 return lhs.compare(rhs) == 0;
308}
309
310inline bool operator<(const Expr &lhs, const Expr &rhs) {
311 return lhs.compare(rhs) < 0;
312}
313
314inline bool operator!=(const Expr &lhs, const Expr &rhs) {
315 return !(lhs == rhs);
316}
317
318inline bool operator>(const Expr &lhs, const Expr &rhs) {
319 return rhs < lhs;
320}
321
322inline bool operator<=(const Expr &lhs, const Expr &rhs) {
323 return !(lhs > rhs);
324}
325
326inline bool operator>=(const Expr &lhs, const Expr &rhs) {
327 return !(lhs < rhs);
328}
329
330// Printing operators
331
332inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Expr &e) {
333 e.print(os);
334 return os;
335}
336
337inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Expr::Kind kind) {
338 Expr::printKind(os, kind);
339 return os;
340}
341
342inline std::stringstream &operator<<(std::stringstream &os, const Expr &e) {
343 std::string str;
344 llvm::raw_string_ostream TmpStr(str);
345 e.print(TmpStr);
346 os << TmpStr.str();
347 return os;
348}
349
350inline std::stringstream &operator<<(std::stringstream &os, const Expr::Kind kind) {
351 std::string str;
352 llvm::raw_string_ostream TmpStr(str);
353 Expr::printKind(TmpStr, kind);
354 os << TmpStr.str();
355 return os;
356}
357
358// Utility classes
359
360class NonConstantExpr : public Expr {
361public:
362 static bool classof(const Expr *E) {
363 return E->getKind() != Expr::Constant;
364 }
365 static bool classof(const NonConstantExpr *) { return true; }
366};
367
369public:
371
372public:
373 unsigned getNumKids() const { return 2; }
374 ref<Expr> getKid(unsigned i) const {
375 if(i == 0)
376 return left;
377 if(i == 1)
378 return right;
379 return 0;
380 }
381
382protected:
383 BinaryExpr(const ref<Expr> &l, const ref<Expr> &r) : left(l), right(r) {}
384
385public:
386 static bool classof(const Expr *E) {
387 Kind k = E->getKind();
389 }
390 static bool classof(const BinaryExpr *) { return true; }
391};
392
393
394class CmpExpr : public BinaryExpr {
395
396protected:
398
399public:
400 Width getWidth() const { return Bool; }
401
402 static bool classof(const Expr *E) {
403 Kind k = E->getKind();
404 return Expr::CmpKindFirst <= k && k <= Expr::CmpKindLast;
405 }
406 static bool classof(const CmpExpr *) { return true; }
407};
408
409// Special
410
412public:
413 static const Kind kind = NotOptimized;
414 static const unsigned numKids = 1;
416
417 static ref<Expr> alloc(const ref<Expr> &src) {
419 r->computeHash();
420 return r;
421 }
422
424
425 Width getWidth() const { return src->getWidth(); }
426 Kind getKind() const { return NotOptimized; }
427
428 unsigned getNumKids() const { return 1; }
429 ref<Expr> getKid(unsigned i) const { return src; }
430
431 virtual ref<Expr> rebuild(ref<Expr> kids[]) const { return create(kids[0]); }
432
433private:
434 NotOptimizedExpr(const ref<Expr> &_src) : src(_src) {}
435
436protected:
437 virtual int compareContents(const Expr &b) const {
438 // No attributes to compare.
439 return 0;
440 }
441
442public:
443 static bool classof(const Expr *E) {
444 return E->getKind() == Expr::NotOptimized;
445 }
446 static bool classof(const NotOptimizedExpr *) { return true; }
447};
448
449
452 friend class UpdateList;
453
454 // cache instead of recalc
455 unsigned hashValue;
456
457public:
460
463
464private:
466 unsigned size;
467
468public:
469 UpdateNode(const ref<UpdateNode> &_next, const ref<Expr> &_index,
470 const ref<Expr> &_value);
471
472 unsigned getSize() const { return size; }
473
474 int compare(const UpdateNode &b) const;
475 unsigned hash() const { return hashValue; }
476
477 UpdateNode() = delete;
478 ~UpdateNode() = default;
479
480 unsigned computeHash();
481};
482
483class Array {
484public:
485 // Name of the array
486 const std::string name;
487
488 // FIXME: Not 64-bit clean.
489 const unsigned size;
490
494
498 const std::vector<ref<ConstantExpr> > constantValues;
499
500private:
501 unsigned hashValue;
502
503 // FIXME: Make =delete when we switch to C++11
504 Array(const Array& array);
505
506 // FIXME: Make =delete when we switch to C++11
507 Array& operator =(const Array& array);
508
509 ~Array();
510
518 Array(const std::string &_name, uint64_t _size,
519 const ref<ConstantExpr> *constantValuesBegin = 0,
520 const ref<ConstantExpr> *constantValuesEnd = 0,
521 Expr::Width _domain = Expr::Int32, Expr::Width _range = Expr::Int8);
522
523public:
524 bool isSymbolicArray() const { return constantValues.empty(); }
525 bool isConstantArray() const { return !isSymbolicArray(); }
526
527 const std::string getName() const { return name; }
528 unsigned getSize() const { return size; }
529 Expr::Width getDomain() const { return domain; }
530 Expr::Width getRange() const { return range; }
531
533 unsigned computeHash();
534 unsigned hash() const { return hashValue; }
535 friend class ArrayCache;
536};
537
540 friend class ReadExpr; // for default constructor
541
542public:
543 const Array *root;
544
547
548public:
549 UpdateList(const Array *_root, const ref<UpdateNode> &_head);
550 UpdateList(const UpdateList &b) = default;
551 ~UpdateList() = default;
552
553 UpdateList &operator=(const UpdateList &b) = default;
554
556 unsigned getSize() const { return head ? head->getSize() : 0; }
557
558 void extend(const ref<Expr> &index, const ref<Expr> &value);
559
560 int compare(const UpdateList &b) const;
561 unsigned hash() const;
562};
563
565class ReadExpr : public NonConstantExpr {
566public:
567 static const Kind kind = Read;
568 static const unsigned numKids = 1;
569
570public:
573
574public:
577 r->computeHash();
578 return r;
579 }
580
581 static ref<Expr> create(const UpdateList &updates, ref<Expr> i);
582
583 Width getWidth() const { assert(updates.root); return updates.root->getRange(); }
584 Kind getKind() const { return Read; }
585
586 unsigned getNumKids() const { return numKids; }
587 ref<Expr> getKid(unsigned i) const { return !i ? index : 0; }
588
589 int compareContents(const Expr &b) const;
590
591 virtual ref<Expr> rebuild(ref<Expr> kids[]) const {
592 return create(updates, kids[0]);
593 }
594
595 virtual unsigned computeHash();
596
597private:
598 ReadExpr(const UpdateList &_updates, const ref<Expr> &_index) :
599 updates(_updates), index(_index) { assert(updates.root); }
600
601public:
602 static bool classof(const Expr *E) {
603 return E->getKind() == Expr::Read;
604 }
605 static bool classof(const ReadExpr *) { return true; }
606};
607
608
611public:
612 static const Kind kind = Select;
613 static const unsigned numKids = 3;
614
615public:
617
618public:
619 static ref<Expr> alloc(const ref<Expr> &c, const ref<Expr> &t,
620 const ref<Expr> &f) {
621 ref<Expr> r(new SelectExpr(c, t, f));
622 r->computeHash();
623 return r;
624 }
625
627
628 Width getWidth() const { return trueExpr->getWidth(); }
629 Kind getKind() const { return Select; }
630
631 unsigned getNumKids() const { return numKids; }
632 ref<Expr> getKid(unsigned i) const {
633 switch(i) {
634 case 0: return cond;
635 case 1: return trueExpr;
636 case 2: return falseExpr;
637 default: return 0;
638 }
639 }
640
641 static bool isValidKidWidth(unsigned kid, Width w) {
642 if (kid == 0)
643 return w == Bool;
644 else
645 return true;
646 }
647
648 virtual ref<Expr> rebuild(ref<Expr> kids[]) const {
649 return create(kids[0], kids[1], kids[2]);
650 }
651
652private:
653 SelectExpr(const ref<Expr> &c, const ref<Expr> &t, const ref<Expr> &f)
654 : cond(c), trueExpr(t), falseExpr(f) {}
655
656public:
657 static bool classof(const Expr *E) {
658 return E->getKind() == Expr::Select;
659 }
660 static bool classof(const SelectExpr *) { return true; }
661
662protected:
663 virtual int compareContents(const Expr &b) const {
664 // No attributes to compare.
665 return 0;
666 }
667};
668
669
674public:
675 static const Kind kind = Concat;
676 static const unsigned numKids = 2;
677
678private:
681
682public:
683 static ref<Expr> alloc(const ref<Expr> &l, const ref<Expr> &r) {
684 ref<Expr> c(new ConcatExpr(l, r));
685 c->computeHash();
686 return c;
687 }
688
689 static ref<Expr> create(const ref<Expr> &l, const ref<Expr> &r);
690
691 Width getWidth() const { return width; }
692 Kind getKind() const { return kind; }
693 ref<Expr> getLeft() const { return left; }
694 ref<Expr> getRight() const { return right; }
695
696 unsigned getNumKids() const { return numKids; }
697 ref<Expr> getKid(unsigned i) const {
698 if (i == 0) return left;
699 else if (i == 1) return right;
700 else return NULL;
701 }
702
704 static ref<Expr> createN(unsigned nKids, const ref<Expr> kids[]);
705 static ref<Expr> create4(const ref<Expr> &kid1, const ref<Expr> &kid2,
706 const ref<Expr> &kid3, const ref<Expr> &kid4);
707 static ref<Expr> create8(const ref<Expr> &kid1, const ref<Expr> &kid2,
708 const ref<Expr> &kid3, const ref<Expr> &kid4,
709 const ref<Expr> &kid5, const ref<Expr> &kid6,
710 const ref<Expr> &kid7, const ref<Expr> &kid8);
711
712 virtual ref<Expr> rebuild(ref<Expr> kids[]) const { return create(kids[0], kids[1]); }
713
714private:
715 ConcatExpr(const ref<Expr> &l, const ref<Expr> &r) : left(l), right(r) {
716 width = l->getWidth() + r->getWidth();
717 }
718
719public:
720 static bool classof(const Expr *E) {
721 return E->getKind() == Expr::Concat;
722 }
723 static bool classof(const ConcatExpr *) { return true; }
724
725protected:
726 virtual int compareContents(const Expr &b) const {
727 const ConcatExpr &eb = static_cast<const ConcatExpr &>(b);
728 if (width != eb.width)
729 return width < eb.width ? -1 : 1;
730 return 0;
731 }
732};
733
734
740public:
741 static const Kind kind = Extract;
742 static const unsigned numKids = 1;
743
744public:
746 unsigned offset;
748
749public:
750 static ref<Expr> alloc(const ref<Expr> &e, unsigned o, Width w) {
751 ref<Expr> r(new ExtractExpr(e, o, w));
752 r->computeHash();
753 return r;
754 }
755
757 static ref<Expr> create(ref<Expr> e, unsigned bitOff, Width w);
758
759 Width getWidth() const { return width; }
760 Kind getKind() const { return Extract; }
761
762 unsigned getNumKids() const { return numKids; }
763 ref<Expr> getKid(unsigned i) const { return expr; }
764
765 int compareContents(const Expr &b) const {
766 const ExtractExpr &eb = static_cast<const ExtractExpr&>(b);
767 if (offset != eb.offset) return offset < eb.offset ? -1 : 1;
768 if (width != eb.width) return width < eb.width ? -1 : 1;
769 return 0;
770 }
771
772 virtual ref<Expr> rebuild(ref<Expr> kids[]) const {
773 return create(kids[0], offset, width);
774 }
775
776 virtual unsigned computeHash();
777
778private:
779 ExtractExpr(const ref<Expr> &e, unsigned b, Width w)
780 : expr(e),offset(b),width(w) {}
781
782public:
783 static bool classof(const Expr *E) {
784 return E->getKind() == Expr::Extract;
785 }
786 static bool classof(const ExtractExpr *) { return true; }
787};
788
789
793class NotExpr : public NonConstantExpr {
794public:
795 static const Kind kind = Not;
796 static const unsigned numKids = 1;
797
799
800public:
801 static ref<Expr> alloc(const ref<Expr> &e) {
802 ref<Expr> r(new NotExpr(e));
803 r->computeHash();
804 return r;
805 }
806
807 static ref<Expr> create(const ref<Expr> &e);
808
809 Width getWidth() const { return expr->getWidth(); }
810 Kind getKind() const { return Not; }
811
812 unsigned getNumKids() const { return numKids; }
813 ref<Expr> getKid(unsigned i) const { return expr; }
814
815 virtual ref<Expr> rebuild(ref<Expr> kids[]) const {
816 return create(kids[0]);
817 }
818
819 virtual unsigned computeHash();
820
821public:
822 static bool classof(const Expr *E) {
823 return E->getKind() == Expr::Not;
824 }
825 static bool classof(const NotExpr *) { return true; }
826
827private:
828 NotExpr(const ref<Expr> &e) : expr(e) {}
829
830protected:
831 virtual int compareContents(const Expr &b) const {
832 // No attributes to compare.
833 return 0;
834 }
835};
836
837
838
839// Casting
840
841class CastExpr : public NonConstantExpr {
842public:
845
846public:
847 CastExpr(const ref<Expr> &e, Width w) : src(e), width(w) {}
848
849 Width getWidth() const { return width; }
850
851 unsigned getNumKids() const { return 1; }
852 ref<Expr> getKid(unsigned i) const { return (i==0) ? src : 0; }
853
854 static bool needsResultType() { return true; }
855
856 int compareContents(const Expr &b) const {
857 const CastExpr &eb = static_cast<const CastExpr&>(b);
858 if (width != eb.width) return width < eb.width ? -1 : 1;
859 return 0;
860 }
861
862 virtual unsigned computeHash();
863
864 static bool classof(const Expr *E) {
865 Expr::Kind k = E->getKind();
866 return Expr::CastKindFirst <= k && k <= Expr::CastKindLast;
867 }
868 static bool classof(const CastExpr *) { return true; }
869};
870
871#define CAST_EXPR_CLASS(_class_kind) \
872class _class_kind ## Expr : public CastExpr { \
873public: \
874 static const Kind kind = _class_kind; \
875 static const unsigned numKids = 1; \
876public: \
877 _class_kind ## Expr(ref<Expr> e, Width w) : CastExpr(e,w) {} \
878 static ref<Expr> alloc(const ref<Expr> &e, Width w) { \
879 ref<Expr> r(new _class_kind ## Expr(e, w)); \
880 r->computeHash(); \
881 return r; \
882 } \
883 static ref<Expr> create(const ref<Expr> &e, Width w); \
884 Kind getKind() const { return _class_kind; } \
885 virtual ref<Expr> rebuild(ref<Expr> kids[]) const { \
886 return create(kids[0], width); \
887 } \
888 \
889 static bool classof(const Expr *E) { \
890 return E->getKind() == Expr::_class_kind; \
891 } \
892 static bool classof(const _class_kind ## Expr *) { \
893 return true; \
894 } \
895}; \
896
897CAST_EXPR_CLASS(SExt)
898CAST_EXPR_CLASS(ZExt)
899
900// Arithmetic/Bit Exprs
901
902#define ARITHMETIC_EXPR_CLASS(_class_kind) \
903 class _class_kind##Expr : public BinaryExpr { \
904 public: \
905 static const Kind kind = _class_kind; \
906 static const unsigned numKids = 2; \
907 \
908 public: \
909 _class_kind##Expr(const ref<Expr> &l, const ref<Expr> &r) \
910 : BinaryExpr(l, r) {} \
911 static ref<Expr> alloc(const ref<Expr> &l, const ref<Expr> &r) { \
912 ref<Expr> res(new _class_kind##Expr(l, r)); \
913 res->computeHash(); \
914 return res; \
915 } \
916 static ref<Expr> create(const ref<Expr> &l, const ref<Expr> &r); \
917 Width getWidth() const { return left->getWidth(); } \
918 Kind getKind() const { return _class_kind; } \
919 virtual ref<Expr> rebuild(ref<Expr> kids[]) const { \
920 return create(kids[0], kids[1]); \
921 } \
922 \
923 static bool classof(const Expr *E) { \
924 return E->getKind() == Expr::_class_kind; \
925 } \
926 static bool classof(const _class_kind##Expr *) { return true; } \
927 \
928 protected: \
929 virtual int compareContents(const Expr &b) const { \
930 /* No attributes to compare.*/ \
931 return 0; \
932 } \
933 };
934
948
949// Comparison Exprs
950
951#define COMPARISON_EXPR_CLASS(_class_kind) \
952 class _class_kind##Expr : public CmpExpr { \
953 public: \
954 static const Kind kind = _class_kind; \
955 static const unsigned numKids = 2; \
956 \
957 public: \
958 _class_kind##Expr(const ref<Expr> &l, const ref<Expr> &r) \
959 : CmpExpr(l, r) {} \
960 static ref<Expr> alloc(const ref<Expr> &l, const ref<Expr> &r) { \
961 ref<Expr> res(new _class_kind##Expr(l, r)); \
962 res->computeHash(); \
963 return res; \
964 } \
965 static ref<Expr> create(const ref<Expr> &l, const ref<Expr> &r); \
966 Kind getKind() const { return _class_kind; } \
967 virtual ref<Expr> rebuild(ref<Expr> kids[]) const { \
968 return create(kids[0], kids[1]); \
969 } \
970 \
971 static bool classof(const Expr *E) { \
972 return E->getKind() == Expr::_class_kind; \
973 } \
974 static bool classof(const _class_kind##Expr *) { return true; } \
975 \
976 protected: \
977 virtual int compareContents(const Expr &b) const { \
978 /* No attributes to compare. */ \
979 return 0; \
980 } \
981 };
982
993
994// Terminal Exprs
995
996class ConstantExpr : public Expr {
997public:
998 static const Kind kind = Constant;
999 static const unsigned numKids = 0;
1000
1001private:
1002 llvm::APInt value;
1003
1004 ConstantExpr(const llvm::APInt &v) : value(v) {}
1005
1006public:
1008
1009 Width getWidth() const { return value.getBitWidth(); }
1010 Kind getKind() const { return Constant; }
1011
1012 unsigned getNumKids() const { return 0; }
1013 ref<Expr> getKid(unsigned i) const { return 0; }
1014
1019 const llvm::APInt &getAPValue() const { return value; }
1020
1030 uint64_t getZExtValue(unsigned bits = 64) const {
1031 assert(getWidth() <= bits && "Value may be out of range!");
1032 return value.getZExtValue();
1033 }
1034
1037 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
1038 return value.getLimitedValue(Limit);
1039 }
1040
1044 void toString(std::string &Res, unsigned radix = 10) const;
1045
1046 int compareContents(const Expr &b) const {
1047 const ConstantExpr &cb = static_cast<const ConstantExpr &>(b);
1048 if (getWidth() != cb.getWidth())
1049 return getWidth() < cb.getWidth() ? -1 : 1;
1050 if (value == cb.value)
1051 return 0;
1052 return value.ult(cb.value) ? -1 : 1;
1053 }
1054
1055 virtual ref<Expr> rebuild(ref<Expr> kids[]) const {
1056 assert(0 && "rebuild() on ConstantExpr");
1057 return const_cast<ConstantExpr *>(this);
1058 }
1059
1060 virtual unsigned computeHash();
1061
1062 static ref<Expr> fromMemory(void *address, Width w);
1063 void toMemory(void *address);
1064
1065 static ref<ConstantExpr> alloc(const llvm::APInt &v) {
1067 r->computeHash();
1068 return r;
1069 }
1070
1071 static ref<ConstantExpr> alloc(const llvm::APFloat &f) {
1072 return alloc(f.bitcastToAPInt());
1073 }
1074
1075 static ref<ConstantExpr> alloc(uint64_t v, Width w) {
1076 return alloc(llvm::APInt(w, v));
1077 }
1078
1079 static ref<ConstantExpr> create(uint64_t v, Width w) {
1080#ifndef NDEBUG
1081 if (w <= 64)
1082 assert(v == bits64::truncateToNBits(v, w) && "invalid constant");
1083#endif
1084 return alloc(v, w);
1085 }
1086
1087 static bool classof(const Expr *E) { return E->getKind() == Expr::Constant; }
1088 static bool classof(const ConstantExpr *) { return true; }
1089
1090 /* Utility Functions */
1091
1093 bool isZero() const { return getAPValue().isMinValue(); }
1094
1096 bool isOne() const { return getLimitedValue() == 1; }
1097
1099 bool isTrue() const {
1100 return (getWidth() == Expr::Bool && value.getBoolValue() == true);
1101 }
1102
1104 bool isFalse() const {
1105 return (getWidth() == Expr::Bool && value.getBoolValue() == false);
1106 }
1107
1109 bool isAllOnes() const { return getAPValue().isAllOnesValue(); }
1110
1111 /* Constant Operations */
1112
1113 ref<ConstantExpr> Concat(const ref<ConstantExpr> &RHS);
1114 ref<ConstantExpr> Extract(unsigned offset, Width W);
1115 ref<ConstantExpr> ZExt(Width W);
1116 ref<ConstantExpr> SExt(Width W);
1117 ref<ConstantExpr> Add(const ref<ConstantExpr> &RHS);
1118 ref<ConstantExpr> Sub(const ref<ConstantExpr> &RHS);
1119 ref<ConstantExpr> Mul(const ref<ConstantExpr> &RHS);
1120 ref<ConstantExpr> UDiv(const ref<ConstantExpr> &RHS);
1121 ref<ConstantExpr> SDiv(const ref<ConstantExpr> &RHS);
1122 ref<ConstantExpr> URem(const ref<ConstantExpr> &RHS);
1123 ref<ConstantExpr> SRem(const ref<ConstantExpr> &RHS);
1124 ref<ConstantExpr> And(const ref<ConstantExpr> &RHS);
1125 ref<ConstantExpr> Or(const ref<ConstantExpr> &RHS);
1126 ref<ConstantExpr> Xor(const ref<ConstantExpr> &RHS);
1127 ref<ConstantExpr> Shl(const ref<ConstantExpr> &RHS);
1128 ref<ConstantExpr> LShr(const ref<ConstantExpr> &RHS);
1129 ref<ConstantExpr> AShr(const ref<ConstantExpr> &RHS);
1130
1131 // Comparisons return a constant expression of width 1.
1132
1133 ref<ConstantExpr> Eq(const ref<ConstantExpr> &RHS);
1134 ref<ConstantExpr> Ne(const ref<ConstantExpr> &RHS);
1135 ref<ConstantExpr> Ult(const ref<ConstantExpr> &RHS);
1136 ref<ConstantExpr> Ule(const ref<ConstantExpr> &RHS);
1137 ref<ConstantExpr> Ugt(const ref<ConstantExpr> &RHS);
1138 ref<ConstantExpr> Uge(const ref<ConstantExpr> &RHS);
1139 ref<ConstantExpr> Slt(const ref<ConstantExpr> &RHS);
1140 ref<ConstantExpr> Sle(const ref<ConstantExpr> &RHS);
1141 ref<ConstantExpr> Sgt(const ref<ConstantExpr> &RHS);
1142 ref<ConstantExpr> Sge(const ref<ConstantExpr> &RHS);
1143
1144 ref<ConstantExpr> Neg();
1145 ref<ConstantExpr> Not();
1146};
1147
1148// Implementations
1149
1150inline bool Expr::isZero() const {
1151 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
1152 return CE->isZero();
1153 return false;
1154}
1155
1156inline bool Expr::isTrue() const {
1157 assert(getWidth() == Expr::Bool && "Invalid isTrue() call!");
1158 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
1159 return CE->isTrue();
1160 return false;
1161}
1162
1163inline bool Expr::isFalse() const {
1164 assert(getWidth() == Expr::Bool && "Invalid isFalse() call!");
1165 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
1166 return CE->isFalse();
1167 return false;
1168}
1169
1170} // End klee namespace
1171
1172#endif /* KLEE_EXPR_H */
#define ARITHMETIC_EXPR_CLASS(_class_kind)
Definition: Expr.h:902
#define CAST_EXPR_CLASS(_class_kind)
Definition: Expr.h:871
#define COMPARISON_EXPR_CLASS(_class_kind)
Definition: Expr.h:951
Implements smart-pointer ref<> used by KLEE.
Provides an interface for creating and destroying Array objects.
Definition: ArrayCache.h:31
Array(const Array &array)
unsigned hashValue
Definition: Expr.h:501
Expr::Width getRange() const
Definition: Expr.h:530
bool isConstantArray() const
Definition: Expr.h:525
Array & operator=(const Array &array)
const unsigned size
Definition: Expr.h:489
bool isSymbolicArray() const
Definition: Expr.h:524
unsigned hash() const
Definition: Expr.h:534
const Expr::Width domain
Definition: Expr.h:493
unsigned getSize() const
Definition: Expr.h:528
const std::vector< ref< ConstantExpr > > constantValues
Definition: Expr.h:498
const std::string getName() const
Definition: Expr.h:527
unsigned computeHash()
ComputeHash must take into account the name, the size, the domain, and the range.
Definition: Expr.cpp:528
const std::string name
Definition: Expr.h:486
Expr::Width getDomain() const
Definition: Expr.h:529
const Expr::Width range
Definition: Expr.h:493
ref< Expr > left
Definition: Expr.h:370
BinaryExpr(const ref< Expr > &l, const ref< Expr > &r)
Definition: Expr.h:383
static bool classof(const BinaryExpr *)
Definition: Expr.h:390
ref< Expr > getKid(unsigned i) const
Definition: Expr.h:374
ref< Expr > right
Definition: Expr.h:370
static bool classof(const Expr *E)
Definition: Expr.h:386
unsigned getNumKids() const
Definition: Expr.h:373
int compareContents(const Expr &b) const
Definition: Expr.h:856
Width getWidth() const
Definition: Expr.h:849
static bool classof(const Expr *E)
Definition: Expr.h:864
static bool classof(const CastExpr *)
Definition: Expr.h:868
Width width
Definition: Expr.h:844
ref< Expr > src
Definition: Expr.h:843
static bool needsResultType()
Definition: Expr.h:854
virtual unsigned computeHash()
Definition: Expr.cpp:205
unsigned getNumKids() const
Definition: Expr.h:851
ref< Expr > getKid(unsigned i) const
Definition: Expr.h:852
CastExpr(const ref< Expr > &e, Width w)
Definition: Expr.h:847
static bool classof(const Expr *E)
Definition: Expr.h:402
Width getWidth() const
Definition: Expr.h:400
static bool classof(const CmpExpr *)
Definition: Expr.h:406
CmpExpr(ref< Expr > l, ref< Expr > r)
Definition: Expr.h:397
ref< Expr > getRight() const
Definition: Expr.h:694
static ref< Expr > createN(unsigned nKids, const ref< Expr > kids[])
Shortcuts to create larger concats. The chain returned is unbalanced to the right.
Definition: Expr.cpp:647
Width width
Definition: Expr.h:679
static ref< Expr > alloc(const ref< Expr > &l, const ref< Expr > &r)
Definition: Expr.h:683
ref< Expr > left
Definition: Expr.h:680
ConcatExpr(const ref< Expr > &l, const ref< Expr > &r)
Definition: Expr.h:715
virtual int compareContents(const Expr &b) const
Definition: Expr.h:726
static ref< Expr > create(const ref< Expr > &l, const ref< Expr > &r)
Definition: Expr.cpp:623
ref< Expr > getKid(unsigned i) const
Definition: Expr.h:697
static const unsigned numKids
Definition: Expr.h:676
static ref< Expr > create8(const ref< Expr > &kid1, const ref< Expr > &kid2, const ref< Expr > &kid3, const ref< Expr > &kid4, const ref< Expr > &kid5, const ref< Expr > &kid6, const ref< Expr > &kid7, const ref< Expr > &kid8)
Shortcut to concat 8 kids. The chain returned is unbalanced to the right.
Definition: Expr.cpp:665
Width getWidth() const
Definition: Expr.h:691
ref< Expr > right
Definition: Expr.h:680
static bool classof(const ConcatExpr *)
Definition: Expr.h:723
ref< Expr > getLeft() const
Definition: Expr.h:693
Kind getKind() const
Definition: Expr.h:692
static ref< Expr > create4(const ref< Expr > &kid1, const ref< Expr > &kid2, const ref< Expr > &kid3, const ref< Expr > &kid4)
Shortcut to concat 4 kids. The chain returned is unbalanced to the right.
Definition: Expr.cpp:659
virtual ref< Expr > rebuild(ref< Expr > kids[]) const
Definition: Expr.h:712
unsigned getNumKids() const
Definition: Expr.h:696
static const Kind kind
Definition: Expr.h:675
static bool classof(const Expr *E)
Definition: Expr.h:720
bool isTrue() const
isTrue - Is this the true expression.
Definition: Expr.h:1099
static ref< ConstantExpr > alloc(uint64_t v, Width w)
Definition: Expr.h:1075
static ref< ConstantExpr > alloc(const llvm::APFloat &f)
Definition: Expr.h:1071
virtual ref< Expr > rebuild(ref< Expr > kids[]) const
Definition: Expr.h:1055
bool isZero() const
isZero - Is this a constant zero.
Definition: Expr.h:1093
bool isOne() const
isOne - Is this a constant one.
Definition: Expr.h:1096
Width getWidth() const
Definition: Expr.h:1009
bool isFalse() const
isFalse - Is this the false expression.
Definition: Expr.h:1104
llvm::APInt value
Definition: Expr.h:1002
int compareContents(const Expr &b) const
Definition: Expr.h:1046
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
Definition: Expr.h:1037
ConstantExpr(const llvm::APInt &v)
Definition: Expr.h:1004
static bool classof(const ConstantExpr *)
Definition: Expr.h:1088
ref< Expr > getKid(unsigned i) const
Definition: Expr.h:1013
static ref< ConstantExpr > create(uint64_t v, Width w)
Definition: Expr.h:1079
static ref< ConstantExpr > alloc(const llvm::APInt &v)
Definition: Expr.h:1065
const llvm::APInt & getAPValue() const
Definition: Expr.h:1019
unsigned getNumKids() const
Definition: Expr.h:1012
uint64_t getZExtValue(unsigned bits=64) const
Definition: Expr.h:1030
static bool classof(const Expr *E)
Definition: Expr.h:1087
Kind getKind() const
Definition: Expr.h:1010
bool isAllOnes() const
isAllOnes - Is this constant all ones.
Definition: Expr.h:1109
Class representing symbolic expressions.
Definition: Expr.h:91
static const unsigned MAGIC_HASH_CONSTANT
Definition: Expr.h:94
static bool needsResultType()
Definition: Expr.h:284
static const Width Int64
Definition: Expr.h:104
virtual ~Expr()
Definition: Expr.h:208
static bool classof(const Expr *)
Definition: Expr.h:286
static const Width Int32
Definition: Expr.h:103
virtual unsigned computeHash()
Definition: Expr.cpp:182
static const Width InvalidWidth
Definition: Expr.h:99
virtual Kind getKind() const =0
virtual void print(llvm::raw_ostream &os) const
Definition: Expr.cpp:326
int compare(const Expr &b) const
Definition: Expr.cpp:95
static ref< Expr > createZExtToPointerWidth(ref< Expr > e)
Definition: Context.cpp:42
static ref< Expr > createIsZero(ref< Expr > e)
Definition: Expr.cpp:322
virtual int compareContents(const Expr &b) const =0
virtual unsigned hash() const
Returns the pre-computed hash of the current expression.
Definition: Expr.h:222
static ref< Expr > createSExtToPointerWidth(ref< Expr > e)
Definition: Context.cpp:38
virtual Width getWidth() const =0
Expr()
Definition: Expr.h:207
static ref< ConstantExpr > createPointer(uint64_t v)
Definition: Context.cpp:46
virtual unsigned getNumKids() const =0
static void printWidth(llvm::raw_ostream &os, Expr::Width w)
Definition: Expr.cpp:306
@ Xor
Definition: Expr.h:149
@ Not
Definition: Expr.h:133
@ Ule
Definition: Expr.h:158
@ LShr
Definition: Expr.h:151
@ Sge
Not used in canonical form.
Definition: Expr.h:164
@ Add
Definition: Expr.h:138
@ Select
Definition: Expr.h:124
@ Ne
Not used in canonical form.
Definition: Expr.h:156
@ LastKind
Definition: Expr.h:166
@ And
Definition: Expr.h:147
@ ZExt
Definition: Expr.h:129
@ URem
Definition: Expr.h:143
@ SDiv
Definition: Expr.h:142
@ NotOptimized
Definition: Expr.h:120
@ Mul
Definition: Expr.h:140
@ CmpKindFirst
Definition: Expr.h:172
@ CastKindFirst
Definition: Expr.h:168
@ AShr
Definition: Expr.h:152
@ CmpKindLast
Definition: Expr.h:173
@ Extract
Definition: Expr.h:126
@ Read
Definition: Expr.h:123
@ Constant
Definition: Expr.h:113
@ Ugt
Not used in canonical form.
Definition: Expr.h:159
@ Sub
Definition: Expr.h:139
@ BinaryKindFirst
Definition: Expr.h:170
@ Ult
Definition: Expr.h:157
@ Shl
Definition: Expr.h:150
@ SRem
Definition: Expr.h:144
@ CastKindLast
Definition: Expr.h:169
@ Concat
Definition: Expr.h:125
@ Or
Definition: Expr.h:148
@ Sle
Definition: Expr.h:162
@ BinaryKindLast
Definition: Expr.h:171
@ Sgt
Not used in canonical form.
Definition: Expr.h:163
@ Slt
Definition: Expr.h:161
@ Uge
Not used in canonical form.
Definition: Expr.h:160
@ SExt
Definition: Expr.h:130
@ InvalidKind
Definition: Expr.h:109
@ UDiv
Definition: Expr.h:141
@ Eq
Definition: Expr.h:155
unsigned Width
The type of an expression is simply its width, in bits.
Definition: Expr.h:97
bool isZero() const
isZero - Is this a constant zero.
Definition: Expr.h:1150
static ref< Expr > createTempRead(const Array *array, Expr::Width w)
Definition: Expr.cpp:49
static unsigned getMinBytesForWidth(Width w)
returns the smallest number of bytes in which the given width fits
Definition: Expr.h:262
static bool isValidKidWidth(unsigned kid, Width w)
Definition: Expr.h:283
unsigned hashValue
Definition: Expr.h:180
bool isFalse() const
isFalse - Is this the false expression.
Definition: Expr.h:1163
static const Width Int16
Definition: Expr.h:102
llvm::DenseSet< std::pair< const Expr *, const Expr * > > ExprEquivSet
Definition: Expr.h:289
virtual ref< Expr > rebuild(ref< Expr > kids[]) const =0
bool isTrue() const
isTrue - Is this the true expression.
Definition: Expr.h:1156
static const Width Bool
Definition: Expr.h:100
static ref< Expr > createFromKind(Kind k, std::vector< CreateArg > args)
Definition: Expr.cpp:230
static const Width Fl80
Definition: Expr.h:105
virtual ref< Expr > getKid(unsigned i) const =0
static ref< Expr > createImplies(ref< Expr > hyp, ref< Expr > conc)
Definition: Expr.cpp:318
static void printKind(llvm::raw_ostream &os, Kind k)
Definition: Expr.cpp:135
static unsigned count
Definition: Expr.h:93
void dump() const
dump - Print the expression to stderr.
Definition: Expr.cpp:330
class ReferenceCounter _refCount
Required by klee::ref-managed objects.
Definition: Expr.h:177
static const Width Int8
Definition: Expr.h:101
static const Kind kind
Definition: Expr.h:741
unsigned offset
Definition: Expr.h:746
unsigned getNumKids() const
Definition: Expr.h:762
ref< Expr > getKid(unsigned i) const
Definition: Expr.h:763
static ref< Expr > alloc(const ref< Expr > &e, unsigned o, Width w)
Definition: Expr.h:750
virtual ref< Expr > rebuild(ref< Expr > kids[]) const
Definition: Expr.h:772
Width getWidth() const
Definition: Expr.h:759
static bool classof(const ExtractExpr *)
Definition: Expr.h:786
static ref< Expr > create(ref< Expr > e, unsigned bitOff, Width w)
Creates an ExtractExpr with the given bit offset and width.
Definition: Expr.cpp:675
ref< Expr > expr
Definition: Expr.h:745
virtual unsigned computeHash()
Definition: Expr.cpp:211
Width width
Definition: Expr.h:747
ExtractExpr(const ref< Expr > &e, unsigned b, Width w)
Definition: Expr.h:779
static const unsigned numKids
Definition: Expr.h:742
int compareContents(const Expr &b) const
Definition: Expr.h:765
static bool classof(const Expr *E)
Definition: Expr.h:783
Kind getKind() const
Definition: Expr.h:760
static bool classof(const Expr *E)
Definition: Expr.h:362
static bool classof(const NonConstantExpr *)
Definition: Expr.h:365
ref< Expr > expr
Definition: Expr.h:798
virtual int compareContents(const Expr &b) const
Definition: Expr.h:831
static ref< Expr > create(const ref< Expr > &e)
Definition: Expr.cpp:705
static bool classof(const Expr *E)
Definition: Expr.h:822
NotExpr(const ref< Expr > &e)
Definition: Expr.h:828
Width getWidth() const
Definition: Expr.h:809
static const Kind kind
Definition: Expr.h:795
Kind getKind() const
Definition: Expr.h:810
static const unsigned numKids
Definition: Expr.h:796
virtual ref< Expr > rebuild(ref< Expr > kids[]) const
Definition: Expr.h:815
static ref< Expr > alloc(const ref< Expr > &e)
Definition: Expr.h:801
unsigned getNumKids() const
Definition: Expr.h:812
static bool classof(const NotExpr *)
Definition: Expr.h:825
ref< Expr > getKid(unsigned i) const
Definition: Expr.h:813
virtual unsigned computeHash()
Definition: Expr.cpp:225
static ref< Expr > create(ref< Expr > src)
Definition: Expr.cpp:501
virtual ref< Expr > rebuild(ref< Expr > kids[]) const
Definition: Expr.h:431
static const Kind kind
Definition: Expr.h:413
static ref< Expr > alloc(const ref< Expr > &src)
Definition: Expr.h:417
NotOptimizedExpr(const ref< Expr > &_src)
Definition: Expr.h:434
static bool classof(const Expr *E)
Definition: Expr.h:443
static bool classof(const NotOptimizedExpr *)
Definition: Expr.h:446
static const unsigned numKids
Definition: Expr.h:414
Kind getKind() const
Definition: Expr.h:426
Width getWidth() const
Definition: Expr.h:425
ref< Expr > getKid(unsigned i) const
Definition: Expr.h:429
virtual int compareContents(const Expr &b) const
Definition: Expr.h:437
ref< Expr > src
Definition: Expr.h:415
unsigned getNumKids() const
Definition: Expr.h:428
Class representing a one byte read from an array.
Definition: Expr.h:565
Kind getKind() const
Definition: Expr.h:584
static const unsigned numKids
Definition: Expr.h:568
virtual unsigned computeHash()
Definition: Expr.cpp:218
unsigned getNumKids() const
Definition: Expr.h:586
virtual ref< Expr > rebuild(ref< Expr > kids[]) const
Definition: Expr.h:591
static const Kind kind
Definition: Expr.h:567
ReadExpr(const UpdateList &_updates, const ref< Expr > &_index)
Definition: Expr.h:598
ref< Expr > index
Definition: Expr.h:572
Width getWidth() const
Definition: Expr.h:583
int compareContents(const Expr &b) const
Definition: Expr.cpp:588
static bool classof(const Expr *E)
Definition: Expr.h:602
ref< Expr > getKid(unsigned i) const
Definition: Expr.h:587
static ref< Expr > alloc(const UpdateList &updates, const ref< Expr > &index)
Definition: Expr.h:575
UpdateList updates
Definition: Expr.h:571
static ref< Expr > create(const UpdateList &updates, ref< Expr > i)
Definition: Expr.cpp:538
static bool classof(const ReadExpr *)
Definition: Expr.h:605
Reference counter to be used as part of a ref-managed struct or class.
Definition: Ref.h:46
Class representing an if-then-else expression.
Definition: Expr.h:610
static bool classof(const Expr *E)
Definition: Expr.h:657
ref< Expr > cond
Definition: Expr.h:616
static bool classof(const SelectExpr *)
Definition: Expr.h:660
static bool isValidKidWidth(unsigned kid, Width w)
Definition: Expr.h:641
virtual int compareContents(const Expr &b) const
Definition: Expr.h:663
ref< Expr > getKid(unsigned i) const
Definition: Expr.h:632
virtual ref< Expr > rebuild(ref< Expr > kids[]) const
Definition: Expr.h:648
static const Kind kind
Definition: Expr.h:612
ref< Expr > trueExpr
Definition: Expr.h:616
static ref< Expr > create(ref< Expr > c, ref< Expr > t, ref< Expr > f)
Definition: Expr.cpp:592
Width getWidth() const
Definition: Expr.h:628
static ref< Expr > alloc(const ref< Expr > &c, const ref< Expr > &t, const ref< Expr > &f)
Definition: Expr.h:619
unsigned getNumKids() const
Definition: Expr.h:631
SelectExpr(const ref< Expr > &c, const ref< Expr > &t, const ref< Expr > &f)
Definition: Expr.h:653
static const unsigned numKids
Definition: Expr.h:613
ref< Expr > falseExpr
Definition: Expr.h:616
Kind getKind() const
Definition: Expr.h:629
Class representing a complete list of updates into an array.
Definition: Expr.h:539
UpdateList & operator=(const UpdateList &b)=default
void extend(const ref< Expr > &index, const ref< Expr > &value)
Definition: Updates.cpp:51
UpdateList(const Array *_root, const ref< UpdateNode > &_head)
Definition: Updates.cpp:48
ref< UpdateNode > head
pointer to the most recent update node
Definition: Expr.h:546
~UpdateList()=default
unsigned getSize() const
size of this update list
Definition: Expr.h:556
int compare(const UpdateList &b) const
Definition: Updates.cpp:61
UpdateList(const UpdateList &b)=default
unsigned hash() const
Definition: Updates.cpp:87
const Array * root
Definition: Expr.h:543
Class representing a byte update of an array.
Definition: Expr.h:451
class ReferenceCounter _refCount
Required by klee::ref-managed objects.
Definition: Expr.h:462
unsigned hashValue
Definition: Expr.h:455
ref< Expr > index
Definition: Expr.h:459
unsigned size
size of this update sequence, including this update
Definition: Expr.h:466
unsigned hash() const
Definition: Expr.h:475
unsigned computeHash()
Definition: Updates.cpp:39
int compare(const UpdateNode &b) const
Definition: Updates.cpp:33
~UpdateNode()=default
const ref< UpdateNode > next
Definition: Expr.h:458
UpdateNode()=delete
ref< Expr > value
Definition: Expr.h:459
unsigned getSize() const
Definition: Expr.h:472
uint64_t truncateToNBits(uint64_t x, unsigned N)
Definition: Bits.h:78
Definition: main.cpp:291
llvm::raw_ostream & operator<<(llvm::raw_ostream &os, const MemoryMap &mm)
bool operator!=(const Expr &lhs, const Expr &rhs)
Definition: Expr.h:314
bool operator<(const Expr &lhs, const Expr &rhs)
Definition: Expr.h:310
bool operator>(const Expr &lhs, const Expr &rhs)
Definition: Expr.h:318
bool operator>=(const Expr &lhs, const Expr &rhs)
Definition: Expr.h:326
llvm::cl::OptionCategory ExprCat
bool operator<=(const Expr &lhs, const Expr &rhs)
Definition: Expr.h:322
bool operator==(const Expr &lhs, const Expr &rhs)
Definition: Expr.h:306
ref< Expr > expr
Definition: Expr.h:294
CreateArg(Width w=Bool)
Definition: Expr.h:297
CreateArg(ref< Expr > e)
Definition: Expr.h:298