1 //===- SMTAPI.h -------------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines a SMT generic Solver API, which will be the base class 10 // for every SMT solver specific class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_SUPPORT_SMTAPI_H 15 #define LLVM_SUPPORT_SMTAPI_H 16 17 #include "llvm/ADT/APFloat.h" 18 #include "llvm/ADT/APSInt.h" 19 #include "llvm/ADT/FoldingSet.h" 20 #include "llvm/Support/raw_ostream.h" 21 #include <memory> 22 23 namespace llvm { 24 25 /// Generic base class for SMT sorts 26 class SMTSort { 27 public: 28 SMTSort() = default; 29 virtual ~SMTSort() = default; 30 31 /// Returns true if the sort is a bitvector, calls isBitvectorSortImpl(). isBitvectorSort()32 virtual bool isBitvectorSort() const { return isBitvectorSortImpl(); } 33 34 /// Returns true if the sort is a floating-point, calls isFloatSortImpl(). isFloatSort()35 virtual bool isFloatSort() const { return isFloatSortImpl(); } 36 37 /// Returns true if the sort is a boolean, calls isBooleanSortImpl(). isBooleanSort()38 virtual bool isBooleanSort() const { return isBooleanSortImpl(); } 39 40 /// Returns the bitvector size, fails if the sort is not a bitvector 41 /// Calls getBitvectorSortSizeImpl(). getBitvectorSortSize()42 virtual unsigned getBitvectorSortSize() const { 43 assert(isBitvectorSort() && "Not a bitvector sort!"); 44 unsigned Size = getBitvectorSortSizeImpl(); 45 assert(Size && "Size is zero!"); 46 return Size; 47 }; 48 49 /// Returns the floating-point size, fails if the sort is not a floating-point 50 /// Calls getFloatSortSizeImpl(). getFloatSortSize()51 virtual unsigned getFloatSortSize() const { 52 assert(isFloatSort() && "Not a floating-point sort!"); 53 unsigned Size = getFloatSortSizeImpl(); 54 assert(Size && "Size is zero!"); 55 return Size; 56 }; 57 58 virtual void Profile(llvm::FoldingSetNodeID &ID) const = 0; 59 60 bool operator<(const SMTSort &Other) const { 61 llvm::FoldingSetNodeID ID1, ID2; 62 Profile(ID1); 63 Other.Profile(ID2); 64 return ID1 < ID2; 65 } 66 67 friend bool operator==(SMTSort const &LHS, SMTSort const &RHS) { 68 return LHS.equal_to(RHS); 69 } 70 71 virtual void print(raw_ostream &OS) const = 0; 72 73 LLVM_DUMP_METHOD void dump() const; 74 75 protected: 76 /// Query the SMT solver and returns true if two sorts are equal (same kind 77 /// and bit width). This does not check if the two sorts are the same objects. 78 virtual bool equal_to(SMTSort const &other) const = 0; 79 80 /// Query the SMT solver and checks if a sort is bitvector. 81 virtual bool isBitvectorSortImpl() const = 0; 82 83 /// Query the SMT solver and checks if a sort is floating-point. 84 virtual bool isFloatSortImpl() const = 0; 85 86 /// Query the SMT solver and checks if a sort is boolean. 87 virtual bool isBooleanSortImpl() const = 0; 88 89 /// Query the SMT solver and returns the sort bit width. 90 virtual unsigned getBitvectorSortSizeImpl() const = 0; 91 92 /// Query the SMT solver and returns the sort bit width. 93 virtual unsigned getFloatSortSizeImpl() const = 0; 94 }; 95 96 /// Shared pointer for SMTSorts, used by SMTSolver API. 97 using SMTSortRef = const SMTSort *; 98 99 /// Generic base class for SMT exprs 100 class SMTExpr { 101 public: 102 SMTExpr() = default; 103 virtual ~SMTExpr() = default; 104 105 bool operator<(const SMTExpr &Other) const { 106 llvm::FoldingSetNodeID ID1, ID2; 107 Profile(ID1); 108 Other.Profile(ID2); 109 return ID1 < ID2; 110 } 111 112 virtual void Profile(llvm::FoldingSetNodeID &ID) const = 0; 113 114 friend bool operator==(SMTExpr const &LHS, SMTExpr const &RHS) { 115 return LHS.equal_to(RHS); 116 } 117 118 virtual void print(raw_ostream &OS) const = 0; 119 120 LLVM_DUMP_METHOD void dump() const; 121 122 protected: 123 /// Query the SMT solver and returns true if two sorts are equal (same kind 124 /// and bit width). This does not check if the two sorts are the same objects. 125 virtual bool equal_to(SMTExpr const &other) const = 0; 126 }; 127 128 class SMTSolverStatistics { 129 public: 130 SMTSolverStatistics() = default; 131 virtual ~SMTSolverStatistics() = default; 132 133 virtual double getDouble(llvm::StringRef) const = 0; 134 virtual unsigned getUnsigned(llvm::StringRef) const = 0; 135 136 virtual void print(raw_ostream &OS) const = 0; 137 138 LLVM_DUMP_METHOD void dump() const; 139 }; 140 141 /// Shared pointer for SMTExprs, used by SMTSolver API. 142 using SMTExprRef = const SMTExpr *; 143 144 /// Generic base class for SMT Solvers 145 /// 146 /// This class is responsible for wrapping all sorts and expression generation, 147 /// through the mk* methods. It also provides methods to create SMT expressions 148 /// straight from clang's AST, through the from* methods. 149 class SMTSolver { 150 public: 151 SMTSolver() = default; 152 virtual ~SMTSolver() = default; 153 154 LLVM_DUMP_METHOD void dump() const; 155 156 // Returns an appropriate floating-point sort for the given bitwidth. getFloatSort(unsigned BitWidth)157 SMTSortRef getFloatSort(unsigned BitWidth) { 158 switch (BitWidth) { 159 case 16: 160 return getFloat16Sort(); 161 case 32: 162 return getFloat32Sort(); 163 case 64: 164 return getFloat64Sort(); 165 case 128: 166 return getFloat128Sort(); 167 default:; 168 } 169 llvm_unreachable("Unsupported floating-point bitwidth!"); 170 } 171 172 // Returns a boolean sort. 173 virtual SMTSortRef getBoolSort() = 0; 174 175 // Returns an appropriate bitvector sort for the given bitwidth. 176 virtual SMTSortRef getBitvectorSort(const unsigned BitWidth) = 0; 177 178 // Returns a floating-point sort of width 16 179 virtual SMTSortRef getFloat16Sort() = 0; 180 181 // Returns a floating-point sort of width 32 182 virtual SMTSortRef getFloat32Sort() = 0; 183 184 // Returns a floating-point sort of width 64 185 virtual SMTSortRef getFloat64Sort() = 0; 186 187 // Returns a floating-point sort of width 128 188 virtual SMTSortRef getFloat128Sort() = 0; 189 190 // Returns an appropriate sort for the given AST. 191 virtual SMTSortRef getSort(const SMTExprRef &AST) = 0; 192 193 /// Given a constraint, adds it to the solver 194 virtual void addConstraint(const SMTExprRef &Exp) const = 0; 195 196 /// Creates a bitvector addition operation 197 virtual SMTExprRef mkBVAdd(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 198 199 /// Creates a bitvector subtraction operation 200 virtual SMTExprRef mkBVSub(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 201 202 /// Creates a bitvector multiplication operation 203 virtual SMTExprRef mkBVMul(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 204 205 /// Creates a bitvector signed modulus operation 206 virtual SMTExprRef mkBVSRem(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 207 208 /// Creates a bitvector unsigned modulus operation 209 virtual SMTExprRef mkBVURem(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 210 211 /// Creates a bitvector signed division operation 212 virtual SMTExprRef mkBVSDiv(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 213 214 /// Creates a bitvector unsigned division operation 215 virtual SMTExprRef mkBVUDiv(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 216 217 /// Creates a bitvector logical shift left operation 218 virtual SMTExprRef mkBVShl(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 219 220 /// Creates a bitvector arithmetic shift right operation 221 virtual SMTExprRef mkBVAshr(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 222 223 /// Creates a bitvector logical shift right operation 224 virtual SMTExprRef mkBVLshr(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 225 226 /// Creates a bitvector negation operation 227 virtual SMTExprRef mkBVNeg(const SMTExprRef &Exp) = 0; 228 229 /// Creates a bitvector not operation 230 virtual SMTExprRef mkBVNot(const SMTExprRef &Exp) = 0; 231 232 /// Creates a bitvector xor operation 233 virtual SMTExprRef mkBVXor(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 234 235 /// Creates a bitvector or operation 236 virtual SMTExprRef mkBVOr(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 237 238 /// Creates a bitvector and operation 239 virtual SMTExprRef mkBVAnd(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 240 241 /// Creates a bitvector unsigned less-than operation 242 virtual SMTExprRef mkBVUlt(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 243 244 /// Creates a bitvector signed less-than operation 245 virtual SMTExprRef mkBVSlt(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 246 247 /// Creates a bitvector unsigned greater-than operation 248 virtual SMTExprRef mkBVUgt(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 249 250 /// Creates a bitvector signed greater-than operation 251 virtual SMTExprRef mkBVSgt(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 252 253 /// Creates a bitvector unsigned less-equal-than operation 254 virtual SMTExprRef mkBVUle(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 255 256 /// Creates a bitvector signed less-equal-than operation 257 virtual SMTExprRef mkBVSle(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 258 259 /// Creates a bitvector unsigned greater-equal-than operation 260 virtual SMTExprRef mkBVUge(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 261 262 /// Creates a bitvector signed greater-equal-than operation 263 virtual SMTExprRef mkBVSge(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 264 265 /// Creates a boolean not operation 266 virtual SMTExprRef mkNot(const SMTExprRef &Exp) = 0; 267 268 /// Creates a boolean equality operation 269 virtual SMTExprRef mkEqual(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 270 271 /// Creates a boolean and operation 272 virtual SMTExprRef mkAnd(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 273 274 /// Creates a boolean or operation 275 virtual SMTExprRef mkOr(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 276 277 /// Creates a boolean ite operation 278 virtual SMTExprRef mkIte(const SMTExprRef &Cond, const SMTExprRef &T, 279 const SMTExprRef &F) = 0; 280 281 /// Creates a bitvector sign extension operation 282 virtual SMTExprRef mkBVSignExt(unsigned i, const SMTExprRef &Exp) = 0; 283 284 /// Creates a bitvector zero extension operation 285 virtual SMTExprRef mkBVZeroExt(unsigned i, const SMTExprRef &Exp) = 0; 286 287 /// Creates a bitvector extract operation 288 virtual SMTExprRef mkBVExtract(unsigned High, unsigned Low, 289 const SMTExprRef &Exp) = 0; 290 291 /// Creates a bitvector concat operation 292 virtual SMTExprRef mkBVConcat(const SMTExprRef &LHS, 293 const SMTExprRef &RHS) = 0; 294 295 /// Creates a predicate that checks for overflow in a bitvector addition 296 /// operation 297 virtual SMTExprRef mkBVAddNoOverflow(const SMTExprRef &LHS, 298 const SMTExprRef &RHS, 299 bool isSigned) = 0; 300 301 /// Creates a predicate that checks for underflow in a signed bitvector 302 /// addition operation 303 virtual SMTExprRef mkBVAddNoUnderflow(const SMTExprRef &LHS, 304 const SMTExprRef &RHS) = 0; 305 306 /// Creates a predicate that checks for overflow in a signed bitvector 307 /// subtraction operation 308 virtual SMTExprRef mkBVSubNoOverflow(const SMTExprRef &LHS, 309 const SMTExprRef &RHS) = 0; 310 311 /// Creates a predicate that checks for underflow in a bitvector subtraction 312 /// operation 313 virtual SMTExprRef mkBVSubNoUnderflow(const SMTExprRef &LHS, 314 const SMTExprRef &RHS, 315 bool isSigned) = 0; 316 317 /// Creates a predicate that checks for overflow in a signed bitvector 318 /// division/modulus operation 319 virtual SMTExprRef mkBVSDivNoOverflow(const SMTExprRef &LHS, 320 const SMTExprRef &RHS) = 0; 321 322 /// Creates a predicate that checks for overflow in a bitvector negation 323 /// operation 324 virtual SMTExprRef mkBVNegNoOverflow(const SMTExprRef &Exp) = 0; 325 326 /// Creates a predicate that checks for overflow in a bitvector multiplication 327 /// operation 328 virtual SMTExprRef mkBVMulNoOverflow(const SMTExprRef &LHS, 329 const SMTExprRef &RHS, 330 bool isSigned) = 0; 331 332 /// Creates a predicate that checks for underflow in a signed bitvector 333 /// multiplication operation 334 virtual SMTExprRef mkBVMulNoUnderflow(const SMTExprRef &LHS, 335 const SMTExprRef &RHS) = 0; 336 337 /// Creates a floating-point negation operation 338 virtual SMTExprRef mkFPNeg(const SMTExprRef &Exp) = 0; 339 340 /// Creates a floating-point isInfinite operation 341 virtual SMTExprRef mkFPIsInfinite(const SMTExprRef &Exp) = 0; 342 343 /// Creates a floating-point isNaN operation 344 virtual SMTExprRef mkFPIsNaN(const SMTExprRef &Exp) = 0; 345 346 /// Creates a floating-point isNormal operation 347 virtual SMTExprRef mkFPIsNormal(const SMTExprRef &Exp) = 0; 348 349 /// Creates a floating-point isZero operation 350 virtual SMTExprRef mkFPIsZero(const SMTExprRef &Exp) = 0; 351 352 /// Creates a floating-point multiplication operation 353 virtual SMTExprRef mkFPMul(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 354 355 /// Creates a floating-point division operation 356 virtual SMTExprRef mkFPDiv(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 357 358 /// Creates a floating-point remainder operation 359 virtual SMTExprRef mkFPRem(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 360 361 /// Creates a floating-point addition operation 362 virtual SMTExprRef mkFPAdd(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 363 364 /// Creates a floating-point subtraction operation 365 virtual SMTExprRef mkFPSub(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 366 367 /// Creates a floating-point less-than operation 368 virtual SMTExprRef mkFPLt(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 369 370 /// Creates a floating-point greater-than operation 371 virtual SMTExprRef mkFPGt(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 372 373 /// Creates a floating-point less-than-or-equal operation 374 virtual SMTExprRef mkFPLe(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 375 376 /// Creates a floating-point greater-than-or-equal operation 377 virtual SMTExprRef mkFPGe(const SMTExprRef &LHS, const SMTExprRef &RHS) = 0; 378 379 /// Creates a floating-point equality operation 380 virtual SMTExprRef mkFPEqual(const SMTExprRef &LHS, 381 const SMTExprRef &RHS) = 0; 382 383 /// Creates a floating-point conversion from floatint-point to floating-point 384 /// operation 385 virtual SMTExprRef mkFPtoFP(const SMTExprRef &From, const SMTSortRef &To) = 0; 386 387 /// Creates a floating-point conversion from signed bitvector to 388 /// floatint-point operation 389 virtual SMTExprRef mkSBVtoFP(const SMTExprRef &From, 390 const SMTSortRef &To) = 0; 391 392 /// Creates a floating-point conversion from unsigned bitvector to 393 /// floatint-point operation 394 virtual SMTExprRef mkUBVtoFP(const SMTExprRef &From, 395 const SMTSortRef &To) = 0; 396 397 /// Creates a floating-point conversion from floatint-point to signed 398 /// bitvector operation 399 virtual SMTExprRef mkFPtoSBV(const SMTExprRef &From, unsigned ToWidth) = 0; 400 401 /// Creates a floating-point conversion from floatint-point to unsigned 402 /// bitvector operation 403 virtual SMTExprRef mkFPtoUBV(const SMTExprRef &From, unsigned ToWidth) = 0; 404 405 /// Creates a new symbol, given a name and a sort 406 virtual SMTExprRef mkSymbol(const char *Name, SMTSortRef Sort) = 0; 407 408 // Returns an appropriate floating-point rounding mode. 409 virtual SMTExprRef getFloatRoundingMode() = 0; 410 411 // If the a model is available, returns the value of a given bitvector symbol 412 virtual llvm::APSInt getBitvector(const SMTExprRef &Exp, unsigned BitWidth, 413 bool isUnsigned) = 0; 414 415 // If the a model is available, returns the value of a given boolean symbol 416 virtual bool getBoolean(const SMTExprRef &Exp) = 0; 417 418 /// Constructs an SMTExprRef from a boolean. 419 virtual SMTExprRef mkBoolean(const bool b) = 0; 420 421 /// Constructs an SMTExprRef from a finite APFloat. 422 virtual SMTExprRef mkFloat(const llvm::APFloat Float) = 0; 423 424 /// Constructs an SMTExprRef from an APSInt and its bit width 425 virtual SMTExprRef mkBitvector(const llvm::APSInt Int, unsigned BitWidth) = 0; 426 427 /// Given an expression, extract the value of this operand in the model. 428 virtual bool getInterpretation(const SMTExprRef &Exp, llvm::APSInt &Int) = 0; 429 430 /// Given an expression extract the value of this operand in the model. 431 virtual bool getInterpretation(const SMTExprRef &Exp, 432 llvm::APFloat &Float) = 0; 433 434 /// Check if the constraints are satisfiable 435 virtual std::optional<bool> check() const = 0; 436 437 /// Push the current solver state 438 virtual void push() = 0; 439 440 /// Pop the previous solver state 441 virtual void pop(unsigned NumStates = 1) = 0; 442 443 /// Reset the solver and remove all constraints. 444 virtual void reset() = 0; 445 446 /// Checks if the solver supports floating-points. 447 virtual bool isFPSupported() = 0; 448 449 virtual void print(raw_ostream &OS) const = 0; 450 451 /// Sets the requested option. 452 virtual void setBoolParam(StringRef Key, bool Value) = 0; 453 virtual void setUnsignedParam(StringRef Key, unsigned Value) = 0; 454 455 virtual std::unique_ptr<SMTSolverStatistics> getStatistics() const = 0; 456 }; 457 458 /// Shared pointer for SMTSolvers. 459 using SMTSolverRef = std::shared_ptr<SMTSolver>; 460 461 /// Convenience method to create and Z3Solver object 462 SMTSolverRef CreateZ3Solver(); 463 464 } // namespace llvm 465 466 #endif 467