David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | public class ConstantFolding { |
| 18 | |
| 19 | /** |
| 20 | * Tiny three-register program exercising int constant folding |
| 21 | * on negation. |
| 22 | */ |
| 23 | |
| 24 | // CHECK-START: int ConstantFolding.IntNegation() constant_folding (before) |
| 25 | // CHECK: [[Const42:i[0-9]+]] IntConstant 42 |
| 26 | // CHECK: [[Neg:i[0-9]+]] Neg [ [[Const42]] ] |
| 27 | // CHECK: Return [ [[Neg]] ] |
| 28 | |
| 29 | // CHECK-START: int ConstantFolding.IntNegation() constant_folding (after) |
| 30 | // CHECK: [[ConstN42:i[0-9]+]] IntConstant -42 |
| 31 | // CHECK: Return [ [[ConstN42]] ] |
| 32 | |
| 33 | public static int IntNegation() { |
| 34 | int x, y; |
| 35 | x = 42; |
| 36 | y = -x; |
| 37 | return y; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Tiny three-register program exercising int constant folding |
| 42 | * on addition. |
| 43 | */ |
| 44 | |
| 45 | // CHECK-START: int ConstantFolding.IntAddition1() constant_folding (before) |
| 46 | // CHECK: [[Const1:i[0-9]+]] IntConstant 1 |
| 47 | // CHECK: [[Const2:i[0-9]+]] IntConstant 2 |
| 48 | // CHECK: [[Add:i[0-9]+]] Add [ [[Const1]] [[Const2]] ] |
| 49 | // CHECK: Return [ [[Add]] ] |
| 50 | |
| 51 | // CHECK-START: int ConstantFolding.IntAddition1() constant_folding (after) |
| 52 | // CHECK: [[Const3:i[0-9]+]] IntConstant 3 |
| 53 | // CHECK: Return [ [[Const3]] ] |
| 54 | |
| 55 | public static int IntAddition1() { |
| 56 | int a, b, c; |
| 57 | a = 1; |
| 58 | b = 2; |
| 59 | c = a + b; |
| 60 | return c; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Small three-register program exercising int constant folding |
| 65 | * on addition. |
| 66 | */ |
| 67 | |
| 68 | // CHECK-START: int ConstantFolding.IntAddition2() constant_folding (before) |
| 69 | // CHECK: [[Const1:i[0-9]+]] IntConstant 1 |
| 70 | // CHECK: [[Const2:i[0-9]+]] IntConstant 2 |
| 71 | // CHECK: [[Const5:i[0-9]+]] IntConstant 5 |
| 72 | // CHECK: [[Const6:i[0-9]+]] IntConstant 6 |
| 73 | // CHECK: [[Add1:i[0-9]+]] Add [ [[Const1]] [[Const2]] ] |
| 74 | // CHECK: [[Add2:i[0-9]+]] Add [ [[Const5]] [[Const6]] ] |
| 75 | // CHECK: [[Add3:i[0-9]+]] Add [ [[Add1]] [[Add2]] ] |
| 76 | // CHECK: Return [ [[Add3]] ] |
| 77 | |
| 78 | // CHECK-START: int ConstantFolding.IntAddition2() constant_folding (after) |
| 79 | // CHECK: [[Const14:i[0-9]+]] IntConstant 14 |
| 80 | // CHECK: Return [ [[Const14]] ] |
| 81 | |
| 82 | public static int IntAddition2() { |
| 83 | int a, b, c; |
| 84 | a = 1; |
| 85 | b = 2; |
| 86 | a += b; |
| 87 | b = 5; |
| 88 | c = 6; |
| 89 | b += c; |
| 90 | c = a + b; |
| 91 | return c; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Tiny three-register program exercising int constant folding |
| 96 | * on subtraction. |
| 97 | */ |
| 98 | |
| 99 | // CHECK-START: int ConstantFolding.IntSubtraction() constant_folding (before) |
| 100 | // CHECK: [[Const5:i[0-9]+]] IntConstant 5 |
| 101 | // CHECK: [[Const2:i[0-9]+]] IntConstant 2 |
| 102 | // CHECK: [[Sub:i[0-9]+]] Sub [ [[Const5]] [[Const2]] ] |
| 103 | // CHECK: Return [ [[Sub]] ] |
| 104 | |
| 105 | // CHECK-START: int ConstantFolding.IntSubtraction() constant_folding (after) |
| 106 | // CHECK: [[Const3:i[0-9]+]] IntConstant 3 |
| 107 | // CHECK: Return [ [[Const3]] ] |
| 108 | |
| 109 | public static int IntSubtraction() { |
| 110 | int a, b, c; |
| 111 | a = 5; |
| 112 | b = 2; |
| 113 | c = a - b; |
| 114 | return c; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Tiny three-register program exercising long constant folding |
| 119 | * on addition. |
| 120 | */ |
| 121 | |
| 122 | // CHECK-START: long ConstantFolding.LongAddition() constant_folding (before) |
| 123 | // CHECK: [[Const1:j[0-9]+]] LongConstant 1 |
| 124 | // CHECK: [[Const2:j[0-9]+]] LongConstant 2 |
| 125 | // CHECK: [[Add:j[0-9]+]] Add [ [[Const1]] [[Const2]] ] |
| 126 | // CHECK: Return [ [[Add]] ] |
| 127 | |
| 128 | // CHECK-START: long ConstantFolding.LongAddition() constant_folding (after) |
| 129 | // CHECK: [[Const3:j[0-9]+]] LongConstant 3 |
| 130 | // CHECK: Return [ [[Const3]] ] |
| 131 | |
| 132 | public static long LongAddition() { |
| 133 | long a, b, c; |
| 134 | a = 1L; |
| 135 | b = 2L; |
| 136 | c = a + b; |
| 137 | return c; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Tiny three-register program exercising long constant folding |
| 142 | * on subtraction. |
| 143 | */ |
| 144 | |
| 145 | // CHECK-START: long ConstantFolding.LongSubtraction() constant_folding (before) |
| 146 | // CHECK: [[Const5:j[0-9]+]] LongConstant 5 |
| 147 | // CHECK: [[Const2:j[0-9]+]] LongConstant 2 |
| 148 | // CHECK: [[Sub:j[0-9]+]] Sub [ [[Const5]] [[Const2]] ] |
| 149 | // CHECK: Return [ [[Sub]] ] |
| 150 | |
| 151 | // CHECK-START: long ConstantFolding.LongSubtraction() constant_folding (after) |
| 152 | // CHECK: [[Const3:j[0-9]+]] LongConstant 3 |
| 153 | // CHECK: Return [ [[Const3]] ] |
| 154 | |
| 155 | public static long LongSubtraction() { |
| 156 | long a, b, c; |
| 157 | a = 5L; |
| 158 | b = 2L; |
| 159 | c = a - b; |
| 160 | return c; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Three-register program with a constant (static) condition. |
| 165 | */ |
| 166 | |
| 167 | // CHECK-START: int ConstantFolding.StaticCondition() constant_folding (before) |
| 168 | // CHECK: [[Const5:i[0-9]+]] IntConstant 5 |
| 169 | // CHECK: [[Const2:i[0-9]+]] IntConstant 2 |
| 170 | // CHECK: [[Cond:z[0-9]+]] GreaterThanOrEqual [ [[Const5]] [[Const2]] ] |
| 171 | // CHECK: If [ [[Cond]] ] |
| 172 | |
| 173 | // CHECK-START: int ConstantFolding.StaticCondition() constant_folding (after) |
| 174 | // CHECK: [[Const1:i[0-9]+]] IntConstant 1 |
| 175 | // CHECK: If [ [[Const1]] ] |
| 176 | |
| 177 | public static int StaticCondition() { |
| 178 | int a, b, c; |
| 179 | a = 5; |
| 180 | b = 2; |
| 181 | if (a < b) |
| 182 | c = a + b; |
| 183 | else |
| 184 | c = a - b; |
| 185 | return c; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Four-variable program with jumps leading to the creation of many |
| 190 | * blocks. |
| 191 | * |
| 192 | * The intent of this test is to ensure that all constant expressions |
| 193 | * are actually evaluated at compile-time, thanks to the reverse |
| 194 | * (forward) post-order traversal of the the dominator tree. |
| 195 | */ |
| 196 | |
| 197 | // CHECK-START: int ConstantFolding.JumpsAndConditionals(boolean) constant_folding (before) |
| 198 | // CHECK: [[Const5:i[0-9]+]] IntConstant 5 |
| 199 | // CHECK: [[Const2:i[0-9]+]] IntConstant 2 |
| 200 | // CHECK: [[Add:i[0-9]+]] Add [ [[Const5]] [[Const2]] ] |
| 201 | // CHECK: [[Phi:i[0-9]+]] Phi [ [[Add]] [[Sub:i[0-9]+]] ] |
| 202 | // CHECK: Return [ [[Phi]] ] |
| 203 | // CHECK: [[Sub]] Sub [ [[Const5]] [[Const2]] ] |
| 204 | |
| 205 | // CHECK-START: int ConstantFolding.JumpsAndConditionals(boolean) constant_folding (after) |
| 206 | // CHECK: [[Const7:i[0-9]+]] IntConstant 7 |
| 207 | // CHECK: [[Phi:i[0-9]+]] Phi [ [[Const7]] [[Const3:i[0-9]+]] ] |
| 208 | // CHECK: Return [ [[Phi]] ] |
| 209 | // CHECK: [[Const3]] IntConstant 3 |
| 210 | |
| 211 | public static int JumpsAndConditionals(boolean cond) { |
| 212 | int a, b, c; |
| 213 | a = 5; |
| 214 | b = 2; |
| 215 | if (cond) |
| 216 | c = a + b; |
| 217 | else |
| 218 | c = a - b; |
| 219 | return c; |
| 220 | } |
| 221 | } |