1*d9f75844SAndroid Build Coastguard Worker /* 2*d9f75844SAndroid Build Coastguard Worker * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved. 3*d9f75844SAndroid Build Coastguard Worker * 4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license 5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source 6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found 7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may 8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree. 9*d9f75844SAndroid Build Coastguard Worker */ 10*d9f75844SAndroid Build Coastguard Worker 11*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_SYSTEM_ASSUME_H_ 12*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_SYSTEM_ASSUME_H_ 13*d9f75844SAndroid Build Coastguard Worker 14*d9f75844SAndroid Build Coastguard Worker // Possibly evaluate `p`, promising the compiler that the result is true; the 15*d9f75844SAndroid Build Coastguard Worker // compiler is allowed (but not required) to use this information when 16*d9f75844SAndroid Build Coastguard Worker // optimizing the code. USE WITH CAUTION! If you promise the compiler things 17*d9f75844SAndroid Build Coastguard Worker // that aren't true, it will build a broken binary for you. 18*d9f75844SAndroid Build Coastguard Worker // 19*d9f75844SAndroid Build Coastguard Worker // As a simple example, the compiler is allowed to transform this 20*d9f75844SAndroid Build Coastguard Worker // 21*d9f75844SAndroid Build Coastguard Worker // RTC_ASSUME(x == 4); 22*d9f75844SAndroid Build Coastguard Worker // return x; 23*d9f75844SAndroid Build Coastguard Worker // 24*d9f75844SAndroid Build Coastguard Worker // into this 25*d9f75844SAndroid Build Coastguard Worker // 26*d9f75844SAndroid Build Coastguard Worker // return 4; 27*d9f75844SAndroid Build Coastguard Worker // 28*d9f75844SAndroid Build Coastguard Worker // It is even allowed to propagate the assumption "backwards in time", if it can 29*d9f75844SAndroid Build Coastguard Worker // prove that it must have held at some earlier time. For example, the compiler 30*d9f75844SAndroid Build Coastguard Worker // is allowed to transform this 31*d9f75844SAndroid Build Coastguard Worker // 32*d9f75844SAndroid Build Coastguard Worker // int Add(int x, int y) { 33*d9f75844SAndroid Build Coastguard Worker // if (x == 17) 34*d9f75844SAndroid Build Coastguard Worker // y += 1; 35*d9f75844SAndroid Build Coastguard Worker // RTC_ASSUME(x != 17); 36*d9f75844SAndroid Build Coastguard Worker // return x + y; 37*d9f75844SAndroid Build Coastguard Worker // } 38*d9f75844SAndroid Build Coastguard Worker // 39*d9f75844SAndroid Build Coastguard Worker // into this 40*d9f75844SAndroid Build Coastguard Worker // 41*d9f75844SAndroid Build Coastguard Worker // int Add(int x, int y) { 42*d9f75844SAndroid Build Coastguard Worker // return x + y; 43*d9f75844SAndroid Build Coastguard Worker // } 44*d9f75844SAndroid Build Coastguard Worker // 45*d9f75844SAndroid Build Coastguard Worker // since if `x` isn't 17 on the third line of the function body, the test of `x 46*d9f75844SAndroid Build Coastguard Worker // == 17` on the first line must fail since nothing can modify the local 47*d9f75844SAndroid Build Coastguard Worker // variable `x` in between. 48*d9f75844SAndroid Build Coastguard Worker // 49*d9f75844SAndroid Build Coastguard Worker // The intended use is to allow the compiler to optimize better. For example, 50*d9f75844SAndroid Build Coastguard Worker // here we allow the compiler to omit an instruction that ensures correct 51*d9f75844SAndroid Build Coastguard Worker // rounding of negative arguments: 52*d9f75844SAndroid Build Coastguard Worker // 53*d9f75844SAndroid Build Coastguard Worker // int DivBy2(int x) { 54*d9f75844SAndroid Build Coastguard Worker // RTC_ASSUME(x >= 0); 55*d9f75844SAndroid Build Coastguard Worker // return x / 2; 56*d9f75844SAndroid Build Coastguard Worker // } 57*d9f75844SAndroid Build Coastguard Worker // 58*d9f75844SAndroid Build Coastguard Worker // and here we allow the compiler to possibly omit a null check: 59*d9f75844SAndroid Build Coastguard Worker // 60*d9f75844SAndroid Build Coastguard Worker // void Delete(int* p) { 61*d9f75844SAndroid Build Coastguard Worker // RTC_ASSUME(p != nullptr); 62*d9f75844SAndroid Build Coastguard Worker // delete p; 63*d9f75844SAndroid Build Coastguard Worker // } 64*d9f75844SAndroid Build Coastguard Worker // 65*d9f75844SAndroid Build Coastguard Worker // clang-format off 66*d9f75844SAndroid Build Coastguard Worker #if defined(__GNUC__) 67*d9f75844SAndroid Build Coastguard Worker #define RTC_ASSUME(p) do { if (!(p)) __builtin_unreachable(); } while (0) 68*d9f75844SAndroid Build Coastguard Worker #else 69*d9f75844SAndroid Build Coastguard Worker #define RTC_ASSUME(p) do {} while (0) 70*d9f75844SAndroid Build Coastguard Worker #endif 71*d9f75844SAndroid Build Coastguard Worker // clang-format on 72*d9f75844SAndroid Build Coastguard Worker 73*d9f75844SAndroid Build Coastguard Worker #endif // RTC_BASE_SYSTEM_ASSUME_H_ 74