| 1 |
//===- LLVMSaddWithOverflow.h - implementation of llvm.sadd.with.overflow -===// |
| 2 |
// |
| 3 |
// The LLVM/SPIRV Translator |
| 4 |
// |
| 5 |
// This file is distributed under the University of Illinois Open Source |
| 6 |
// License. See LICENSE.TXT for details. |
| 7 |
// |
| 8 |
// Copyright (c) 2020 Intel Corporation. All rights reserved. |
| 9 |
// |
| 10 |
// Permission is hereby granted, free of charge, to any person obtaining a |
| 11 |
// copy of this software and associated documentation files (the "Software"), |
| 12 |
// to deal with the Software without restriction, including without limitation |
| 13 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 14 |
// and/or sell copies of the Software, and to permit persons to whom the |
| 15 |
// Software is furnished to do so, subject to the following conditions: |
| 16 |
// |
| 17 |
// Redistributions of source code must retain the above copyright notice, |
| 18 |
// this list of conditions and the following disclaimers. |
| 19 |
// Redistributions in binary form must reproduce the above copyright notice, |
| 20 |
// this list of conditions and the following disclaimers in the documentation |
| 21 |
// and/or other materials provided with the distribution. |
| 22 |
// Neither the names of Intel Corporation, nor the names of its |
| 23 |
// contributors may be used to endorse or promote products derived from this |
| 24 |
// Software without specific prior written permission. |
| 25 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 28 |
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 29 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 30 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH |
| 31 |
// THE SOFTWARE. |
| 32 |
// |
| 33 |
//===----------------------------------------------------------------------===// |
| 34 |
// |
| 35 |
// This file implements lowering of llvm.sadd.with.overflow.* into basic LLVM |
| 36 |
// operations. |
| 37 |
// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
|
| 40 |
// The IR below is slightly manually modified IR which was produced by Clang |
| 41 |
// from the C++ code below. The modifications include: |
| 42 |
// - adapting the return value, i.e. replacing `store` instructions for the c |
| 43 |
// and o arguments with `insertvalue` instructions. |
| 44 |
// - changing type of the `phi` instruction in the last basic block from `i8` |
| 45 |
// to `i1`. That also requires change of the argument of the `phi` instruction |
| 46 |
// and allowed to remove an unnecessary `sext` instruction. |
| 47 |
// |
| 48 |
// #include <stdlib.h> |
| 49 |
// #include <stdint.h> |
| 50 |
// |
| 51 |
// const unsigned short i16_abs_pos_max = 0x7FFF; // 32767; |
| 52 |
// const unsigned short i16_abs_neg_max = 0x8000; // 32768; |
| 53 |
// |
| 54 |
// void llvm_sadd_with_overflow_i16(int16_t a, int16_t b, int16_t& c, bool& o) { |
| 55 |
// bool overflow = false; |
| 56 |
// bool both_pos = (a>=0 && b>=0); |
| 57 |
// bool both_neg = (a<0 && b<0); |
| 58 |
// if (both_pos || both_neg) { |
| 59 |
// // 32-bit integers are always supported in SPIR-V |
| 60 |
// uint32_t x = (uint32_t)abs(a) + (uint32_t)abs(b); |
| 61 |
// if (both_pos && x > i16_abs_pos_max || |
| 62 |
// both_neg && x > i16_abs_neg_max) { |
| 63 |
// overflow = true; |
| 64 |
// } |
| 65 |
// } |
| 66 |
// c = a + b; |
| 67 |
// o = overflow; |
| 68 |
// } |
| 69 |
// |
| 70 |
// const uint32_t i32_abs_pos_max = 0x7FFFFFFF; // 2147483647 |
| 71 |
// const uint32_t i32_abs_neg_max = 0x80000000; // 2147483648 |
| 72 |
// const int32_t i32_min = 0x80000000; // -2147483648 |
| 73 |
// |
| 74 |
// void llvm_sadd_with_overflow_i32(int32_t a, int32_t b, int32_t& c, bool& o) { |
| 75 |
// bool overflow = false; |
| 76 |
// bool both_pos = (a>=0 && b>=0); |
| 77 |
// bool both_neg = (a<0 && b<0); |
| 78 |
// // if a or b is the most negative number we can't get its absolute value, |
| 79 |
// // because it is out of range. |
| 80 |
// if (both_neg && (a == i32_min || b == i32_min)) |
| 81 |
// overflow = true; |
| 82 |
// else if (both_pos || both_neg) { |
| 83 |
// uint32_t x = (uint32_t)abs(a) + (uint32_t)abs(b); |
| 84 |
// if (both_pos && x > i32_abs_pos_max || |
| 85 |
// both_neg && x > 2147483648U) { |
| 86 |
// overflow = true; |
| 87 |
// } |
| 88 |
// } |
| 89 |
// c = a + b; |
| 90 |
// o = overflow; |
| 91 |
// } |
| 92 |
// |
| 93 |
// const uint64_t i64_abs_pos_max = 0x7fffffffffffffff; // 9223372036854775807 |
| 94 |
// const uint64_t i64_abs_neg_max = 0x8000000000000000; // 9223372036854775808 |
| 95 |
// const int64_t i64_min = 0x8000000000000000; // -9223372036854775808 |
| 96 |
// |
| 97 |
// void llvm_sadd_with_overflow_i64(int64_t a, int64_t b, int64_t& c, bool& o) { |
| 98 |
// bool overflow = false; |
| 99 |
// bool both_pos = (a>=0 && b>=0); |
| 100 |
// bool both_neg = (a<0 && b<0); |
| 101 |
// // if a or b is the most negative number we can't get its absolute value, |
| 102 |
// // because it is out of range. |
| 103 |
// if (both_neg && (a == i64_min || b == i64_min)) |
| 104 |
// overflow = true; |
| 105 |
// else if (both_pos || both_neg) { |
| 106 |
// uint64_t x = (uint64_t)abs(a) + (uint64_t)abs(b); |
| 107 |
// if (both_pos && x > i64_abs_pos_max || |
| 108 |
// both_neg && x > i64_abs_neg_max) { |
| 109 |
// overflow = true; |
| 110 |
// } |
| 111 |
// } |
| 112 |
// c = a + b; |
| 113 |
// o = overflow; |
| 114 |
// } |
| 115 |
// |
| 116 |
// const unsigned int abs_pos_max = 2147483647; |
| 117 |
// const unsigned int abs_neg_max = 2147483648; |
| 118 |
// |
| 119 |
// void llvm_sadd_with_overflow_i32(int a, int b, int& c, bool& o) { |
| 120 |
// bool overflow = false; |
| 121 |
// bool both_pos = (a>=0 && b>=0); |
| 122 |
// bool both_neg = (a<0 && b<0); |
| 123 |
// if (both_pos || both_neg) { |
| 124 |
// unsigned int x = (unsigned int)abs(a) + (unsigned int)abs(b); |
| 125 |
// if (both_pos && x > abs_pos_max || |
| 126 |
// both_neg && x > abs_neg_max) { |
| 127 |
// overflow = true; |
| 128 |
// } |
| 129 |
// } |
| 130 |
// c = a + b; |
| 131 |
// o = overflow; |
| 132 |
// } |
| 133 |
// Clang options: -emit-llvm -O2 -g0 -fno-discard-value-names |
| 134 |
|
| 135 |
static const char LLVMSaddWithOverflow[]{R"( |
| 136 |
define spir_func { i16, i1 } @llvm_sadd_with_overflow_i16(i16 %a, i16 %b) { |
| 137 |
entry: |
| 138 |
%conv = sext i16 %a to i32 |
| 139 |
%conv1 = sext i16 %b to i32 |
| 140 |
%0 = or i16 %b, %a |
| 141 |
%1 = icmp sgt i16 %0, -1 |
| 142 |
%2 = and i16 %b, %a |
| 143 |
%3 = icmp slt i16 %2, 0 |
| 144 |
%brmerge = or i1 %1, %3 |
| 145 |
br i1 %brmerge, label %if.then, label %if.end21 |
| 146 |
|
| 147 |
if.then: ; preds = %entry |
| 148 |
%4 = icmp slt i32 %conv, 0 |
| 149 |
%neg = sub nsw i32 0, %conv |
| 150 |
%5 = select i1 %4, i32 %neg, i32 %conv |
| 151 |
%6 = icmp slt i32 %conv1, 0 |
| 152 |
%neg39 = sub nsw i32 0, %conv1 |
| 153 |
%7 = select i1 %6, i32 %neg39, i32 %conv1 |
| 154 |
%add = add nuw nsw i32 %7, %5 |
| 155 |
%cmp15 = icmp ugt i32 %add, 32767 |
| 156 |
%or.cond = and i1 %1, %cmp15 |
| 157 |
%cmp19 = icmp ugt i32 %add, 32768 |
| 158 |
%or.cond28 = and i1 %3, %cmp19 |
| 159 |
%or.cond40 = or i1 %or.cond, %or.cond28 |
| 160 |
br label %if.end21 |
| 161 |
|
| 162 |
if.end21: ; preds = %if.then, %entry |
| 163 |
%overflow = phi i1 [ 0, %entry ], [ %or.cond40, %if.then ] |
| 164 |
%add24 = add i16 %b, %a |
| 165 |
%agg = insertvalue {i16, i1} undef, i16 %add24, 0 |
| 166 |
%res = insertvalue {i16, i1} %agg, i1 %overflow, 1 |
| 167 |
ret {i16, i1} %res |
| 168 |
} |
| 169 |
|
| 170 |
define spir_func { i32, i1 } @llvm_sadd_with_overflow_i32(i32 %a, i32 %b) { |
| 171 |
entry: |
| 172 |
%0 = or i32 %b, %a |
| 173 |
%1 = icmp sgt i32 %0, -1 |
| 174 |
%2 = and i32 %b, %a |
| 175 |
%3 = icmp slt i32 %2, 0 |
| 176 |
br i1 %3, label %land.lhs.true, label %if.else |
| 177 |
|
| 178 |
land.lhs.true: ; preds = %entry |
| 179 |
%cmp7 = icmp eq i32 %a, -2147483648 |
| 180 |
%cmp8 = icmp eq i32 %b, -2147483648 |
| 181 |
%or.cond = or i1 %cmp7, %cmp8 |
| 182 |
br i1 %or.cond, label %if.end23, label %if.then12 |
| 183 |
|
| 184 |
if.else: ; preds = %entry |
| 185 |
br i1 %1, label %if.then12, label %if.end23 |
| 186 |
|
| 187 |
if.then12: ; preds = %land.lhs.true, %if.else |
| 188 |
%4 = icmp slt i32 %a, 0 |
| 189 |
%neg = sub nsw i32 0, %a |
| 190 |
%5 = select i1 %4, i32 %neg, i32 %a |
| 191 |
%6 = icmp slt i32 %b, 0 |
| 192 |
%neg42 = sub nsw i32 0, %b |
| 193 |
%7 = select i1 %6, i32 %neg42, i32 %b |
| 194 |
%add = add nuw i32 %7, %5 |
| 195 |
%cmp16 = icmp slt i32 %add, 0 |
| 196 |
%or.cond27 = and i1 %1, %cmp16 |
| 197 |
%cmp20 = icmp ugt i32 %add, -2147483648 |
| 198 |
%or.cond28 = and i1 %3, %cmp20 |
| 199 |
%or.cond43 = or i1 %or.cond27, %or.cond28 |
| 200 |
br label %if.end23 |
| 201 |
|
| 202 |
if.end23: ; preds = %if.then12, %if.else, %land.lhs.true |
| 203 |
%overflow = phi i1 [ 1, %land.lhs.true ], [ 0, %if.else ], [ %or.cond43, %if.then12 ] |
| 204 |
%add24 = add nsw i32 %b, %a |
| 205 |
%agg = insertvalue {i32, i1} undef, i32 %add24, 0 |
| 206 |
%res = insertvalue {i32, i1} %agg, i1 %overflow, 1 |
| 207 |
ret {i32, i1} %res |
| 208 |
} |
| 209 |
|
| 210 |
define spir_func { i64, i1 } @llvm_sadd_with_overflow_i64(i64 %a, i64 %b) { |
| 211 |
entry: |
| 212 |
%0 = or i64 %b, %a |
| 213 |
%1 = icmp sgt i64 %0, -1 |
| 214 |
%2 = and i64 %b, %a |
| 215 |
%3 = icmp slt i64 %2, 0 |
| 216 |
br i1 %3, label %land.lhs.true, label %if.else |
| 217 |
|
| 218 |
land.lhs.true: ; preds = %entry |
| 219 |
%cmp7 = icmp eq i64 %a, -9223372036854775808 |
| 220 |
%cmp8 = icmp eq i64 %b, -9223372036854775808 |
| 221 |
%or.cond = or i1 %cmp7, %cmp8 |
| 222 |
br i1 %or.cond, label %if.end23, label %if.then12 |
| 223 |
|
| 224 |
if.else: ; preds = %entry |
| 225 |
br i1 %1, label %if.then12, label %if.end23 |
| 226 |
|
| 227 |
if.then12: ; preds = %land.lhs.true, %if.else |
| 228 |
%neg.i = sub nsw i64 0, %a |
| 229 |
%abscond.i = icmp slt i64 %a, 0 |
| 230 |
%abs.i = select i1 %abscond.i, i64 %neg.i, i64 %a |
| 231 |
%neg.i43 = sub nsw i64 0, %b |
| 232 |
%abscond.i44 = icmp slt i64 %b, 0 |
| 233 |
%abs.i45 = select i1 %abscond.i44, i64 %neg.i43, i64 %b |
| 234 |
%add = add nuw i64 %abs.i45, %abs.i |
| 235 |
%cmp16 = icmp slt i64 %add, 0 |
| 236 |
%or.cond27 = and i1 %1, %cmp16 |
| 237 |
%cmp20 = icmp ugt i64 %add, -9223372036854775808 |
| 238 |
%or.cond28 = and i1 %3, %cmp20 |
| 239 |
%or.cond42 = or i1 %or.cond27, %or.cond28 |
| 240 |
br label %if.end23 |
| 241 |
|
| 242 |
if.end23: ; preds = %if.then12, %if.else, %land.lhs.true |
| 243 |
%overflow = phi i1 [ 1, %land.lhs.true ], [ 0, %if.else ], [ %or.cond42, %if.then12 ] |
| 244 |
%add24 = add nsw i64 %b, %a |
| 245 |
%agg = insertvalue {i64, i1} undef, i64 %add24, 0 |
| 246 |
%res = insertvalue {i64, i1} %agg, i1 %overflow, 1 |
| 247 |
ret {i64, i1} %res |
| 248 |
} |
| 249 |
)"}; |