LLVM  9.0.0svn
CheckedArithmetic.h
Go to the documentation of this file.
1 //==-- llvm/Support/CheckedArithmetic.h - Safe arithmetical operations *- 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 contains generic functions for operating on integers which
10 // give the indication on whether the operation has overflown.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_CHECKEDARITHMETIC_H
15 #define LLVM_SUPPORT_CHECKEDARITHMETIC_H
16 
17 #include "llvm/ADT/APInt.h"
18 #include "llvm/ADT/Optional.h"
19 
20 #include <type_traits>
21 
22 namespace {
23 
24 /// Utility function to apply a given method of \c APInt \p F to \p LHS and
25 /// \p RHS.
26 /// \return Empty optional if the operation overflows, or result otherwise.
27 template <typename T, typename F>
28 typename std::enable_if<std::is_integral<T>::value && sizeof(T) * 8 <= 64,
30 checkedOp(T LHS, T RHS, F Op, bool Signed = true) {
31  llvm::APInt ALHS(/*BitSize=*/sizeof(T) * 8, LHS, Signed);
32  llvm::APInt ARHS(/*BitSize=*/sizeof(T) * 8, RHS, Signed);
33  bool Overflow;
34  llvm::APInt Out = (ALHS.*Op)(ARHS, Overflow);
35  if (Overflow)
36  return llvm::None;
37  return Signed ? Out.getSExtValue() : Out.getZExtValue();
38 }
39 }
40 
41 namespace llvm {
42 
43 /// Add two signed integers \p LHS and \p RHS.
44 /// \return Optional of sum if no signed overflow occurred,
45 /// \c None otherwise.
46 template <typename T>
47 typename std::enable_if<std::is_signed<T>::value, llvm::Optional<T>>::type
48 checkedAdd(T LHS, T RHS) {
49  return checkedOp(LHS, RHS, &llvm::APInt::sadd_ov);
50 }
51 
52 /// Multiply two signed integers \p LHS and \p RHS.
53 /// \return Optional of product if no signed overflow occurred,
54 /// \c None otherwise.
55 template <typename T>
56 typename std::enable_if<std::is_signed<T>::value, llvm::Optional<T>>::type
57 checkedMul(T LHS, T RHS) {
58  return checkedOp(LHS, RHS, &llvm::APInt::smul_ov);
59 }
60 
61 /// Multiply A and B, and add C to the resulting product.
62 /// \return Optional of result if no signed overflow occurred,
63 /// \c None otherwise.
64 template <typename T>
65 typename std::enable_if<std::is_signed<T>::value, llvm::Optional<T>>::type
66 checkedMulAdd(T A, T B, T C) {
67  if (auto Product = checkedMul(A, B))
68  return checkedAdd(*Product, C);
69  return llvm::None;
70 }
71 
72 /// Add two unsigned integers \p LHS and \p RHS.
73 /// \return Optional of sum if no unsigned overflow occurred,
74 /// \c None otherwise.
75 template <typename T>
76 typename std::enable_if<std::is_unsigned<T>::value, llvm::Optional<T>>::type
77 checkedAddUnsigned(T LHS, T RHS) {
78  return checkedOp(LHS, RHS, &llvm::APInt::uadd_ov, /*Signed=*/false);
79 }
80 
81 /// Multiply two unsigned integers \p LHS and \p RHS.
82 /// \return Optional of product if no unsigned overflow occurred,
83 /// \c None otherwise.
84 template <typename T>
85 typename std::enable_if<std::is_unsigned<T>::value, llvm::Optional<T>>::type
86 checkedMulUnsigned(T LHS, T RHS) {
87  return checkedOp(LHS, RHS, &llvm::APInt::umul_ov, /*Signed=*/false);
88 }
89 
90 /// Multiply unsigned integers A and B, and add C to the resulting product.
91 /// \return Optional of result if no unsigned overflow occurred,
92 /// \c None otherwise.
93 template <typename T>
94 typename std::enable_if<std::is_unsigned<T>::value, llvm::Optional<T>>::type
96  if (auto Product = checkedMulUnsigned(A, B))
97  return checkedAddUnsigned(*Product, C);
98  return llvm::None;
99 }
100 
101 } // End llvm namespace
102 
103 #endif
const NoneType None
Definition: None.h:23
uint64_t CallInst * C
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1562
std::enable_if< std::is_unsigned< T >::value, llvm::Optional< T > >::type checkedMulUnsigned(T LHS, T RHS)
Multiply two unsigned integers LHS and RHS.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::enable_if< std::is_signed< T >::value, llvm::Optional< T > >::type checkedAdd(T LHS, T RHS)
Add two signed integers LHS and RHS.
F(f)
This file implements a class to represent arbitrary precision integral constant values and operations...
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1574
#define T
std::enable_if< std::is_signed< T >::value, llvm::Optional< T > >::type checkedMul(T LHS, T RHS)
Multiply two signed integers LHS and RHS.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1881
std::enable_if< std::is_unsigned< T >::value, llvm::Optional< T > >::type checkedAddUnsigned(T LHS, T RHS)
Add two unsigned integers LHS and RHS.
Class for arbitrary precision integers.
Definition: APInt.h:69
APInt smul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1906
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1916
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1874
std::enable_if< std::is_signed< T >::value, llvm::Optional< T > >::type checkedMulAdd(T A, T B, T C)
Multiply A and B, and add C to the resulting product.
std::enable_if< std::is_unsigned< T >::value, llvm::Optional< T > >::type checkedMulAddUnsigned(T A, T B, T C)
Multiply unsigned integers A and B, and add C to the resulting product.