llvm.org GIT mirror
Add some transforms of the kind X-Y>X -> 0>Y which are valid when there is no overflow. These subsume some existing equality transforms, so zap those. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@125843 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 8 years ago
3 changed file(s) with 143 addition(s) and 20 deletion(s).
 2266 2266 return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()), 2267 2267 C == Op0 ? D : C); 2268 2268 2269 // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.⏎ 2269 // icmp (X+Y), (X+Z) -> icmp Y, Z for equalities or if there is no overflow.⏎ 2270 2270 if (A && C && (A == C || A == D || B == C || B == D) && 2271 2271 NoOp0WrapProblem && NoOp1WrapProblem && 2272 2272 // Try not to increase register pressure. 2285 2285 if (BO1 && BO1->getOpcode() == Instruction::Sub) 2286 2286 C = BO1->getOperand(0), D = BO1->getOperand(1); 2287 2287 2288 // icmp (Y-X), (Z-X) -> icmp Y,Z for equalities or if there is no overflow.⏎ 2288 // icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow.⏎ 2289 if (A == Op1 && NoOp0WrapProblem) 2290 return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B); 2291 2292 // icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow. 2293 if (C == Op0 && NoOp1WrapProblem) 2294 return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType())); 2295 2296 // icmp (Y-X), (Z-X) -> icmp Y, Z for equalities or if there is no overflow. 2289 2297 if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem && 2290 2298 // Try not to increase register pressure. 2291 2299 BO0->hasOneUse() && BO1->hasOneUse()) 2292 2300 return new ICmpInst(Pred, A, C); 2301 2302 // icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow. 2303 if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem && 2304 // Try not to increase register pressure. 2305 BO0->hasOneUse() && BO1->hasOneUse()) 2306 return new ICmpInst(Pred, D, B); 2293 2307 2294 2308 if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() && 2295 2309 BO0->hasOneUse() && BO1->hasOneUse() && 2374 2388 2375 2389 if (I.isEquality()) { 2376 2390 Value *A, *B, *C, *D; 2377 2378 // -x == -y --> x == y 2379 if (match(Op0, m_Neg(m_Value(A))) && 2380 match(Op1, m_Neg(m_Value(B)))) 2381 return new ICmpInst(I.getPredicate(), A, B); 2382 ⏎ 2391 ⏎ 2383 2392 if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { 2384 2393 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 2385 2394 Value *OtherVal = A == Op1 ? B : A; 2413 2422 return new ICmpInst(I.getPredicate(), OtherVal, 2414 2423 Constant::getNullValue(A->getType())); 2415 2424 } 2416 2417 // (A-B) == A -> B == 0 2418 if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B)))) 2419 return new ICmpInst(I.getPredicate(), B, 2420 Constant::getNullValue(B->getType())); 2421 2422 // A == (A-B) -> B == 0 2423 if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B)))) 2424 return new ICmpInst(I.getPredicate(), B, 2425 Constant::getNullValue(B->getType())); 2426 2425 2427 2426 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 2428 2427 if (Op0->hasOneUse() && Op1->hasOneUse() &&
 234 234 } 235 235 236 236 ; CHECK: @test25 237 ; X + Z > Y + Z -> X > Y if there is no overflow. 237 238 ; CHECK: %c = icmp sgt i32 %x, %y 238 239 ; CHECK: ret i1 %c 239 240 define i1 @test25(i32 %x, i32 %y, i32 %z) { 244 245 } 245 246 246 247 ; CHECK: @test26 248 ; X + Z > Y + Z -> X > Y if there is no overflow. 249 ; CHECK: %c = icmp ugt i32 %x, %y 250 ; CHECK: ret i1 %c 251 define i1 @test26(i32 %x, i32 %y, i32 %z) { 252 %lhs = add nuw i32 %x, %z 253 %rhs = add nuw i32 %y, %z 254 %c = icmp ugt i32 %lhs, %rhs 255 ret i1 %c 256 } 257 258 ; CHECK: @test27 259 ; X - Z > Y - Z -> X > Y if there is no overflow. 247 260 ; CHECK: %c = icmp sgt i32 %x, %y 248 261 ; CHECK: ret i1 %c 249 define i1 @test26(i32 %x, i32 %y, i32 %z) {⏎ 262 define i1 @test27(i32 %x, i32 %y, i32 %z) {⏎ 250 263 %lhs = sub nsw i32 %x, %z 251 264 %rhs = sub nsw i32 %y, %z 252 265 %c = icmp sgt i32 %lhs, %rhs 253 266 ret i1 %c 254 267 } 268 269 ; CHECK: @test28 270 ; X - Z > Y - Z -> X > Y if there is no overflow. 271 ; CHECK: %c = icmp ugt i32 %x, %y 272 ; CHECK: ret i1 %c 273 define i1 @test28(i32 %x, i32 %y, i32 %z) { 274 %lhs = sub nuw i32 %x, %z 275 %rhs = sub nuw i32 %y, %z 276 %c = icmp ugt i32 %lhs, %rhs 277 ret i1 %c 278 } 279 280 ; CHECK: @test29 281 ; X + Y > X -> Y > 0 if there is no overflow. 282 ; CHECK: %c = icmp sgt i32 %y, 0 283 ; CHECK: ret i1 %c 284 define i1 @test29(i32 %x, i32 %y) { 285 %lhs = add nsw i32 %x, %y 286 %c = icmp sgt i32 %lhs, %x 287 ret i1 %c 288 } 289 290 ; CHECK: @test30 291 ; X + Y > X -> Y > 0 if there is no overflow. 292 ; CHECK: %c = icmp ne i32 %y, 0 293 ; CHECK: ret i1 %c 294 define i1 @test30(i32 %x, i32 %y) { 295 %lhs = add nuw i32 %x, %y 296 %c = icmp ugt i32 %lhs, %x 297 ret i1 %c 298 } 299 300 ; CHECK: @test31 301 ; X > X + Y -> 0 > Y if there is no overflow. 302 ; CHECK: %c = icmp slt i32 %y, 0 303 ; CHECK: ret i1 %c 304 define i1 @test31(i32 %x, i32 %y) { 305 %rhs = add nsw i32 %x, %y 306 %c = icmp sgt i32 %x, %rhs 307 ret i1 %c 308 } 309 310 ; CHECK: @test32 311 ; X > X + Y -> 0 > Y if there is no overflow. 312 ; CHECK: ret i1 false 313 define i1 @test32(i32 %x, i32 %y) { 314 %rhs = add nuw i32 %x, %y 315 %c = icmp ugt i32 %x, %rhs 316 ret i1 %c 317 } 318 319 ; CHECK: @test33 320 ; X - Y > X -> 0 > Y if there is no overflow. 321 ; CHECK: %c = icmp slt i32 %y, 0 322 ; CHECK: ret i1 %c 323 define i1 @test33(i32 %x, i32 %y) { 324 %lhs = sub nsw i32 %x, %y 325 %c = icmp sgt i32 %lhs, %x 326 ret i1 %c 327 } 328 329 ; CHECK: @test34 330 ; X - Y > X -> 0 > Y if there is no overflow. 331 ; CHECK: ret i1 false 332 define i1 @test34(i32 %x, i32 %y) { 333 %lhs = sub nuw i32 %x, %y 334 %c = icmp ugt i32 %lhs, %x 335 ret i1 %c 336 } 337 338 ; CHECK: @test35 339 ; X > X - Y -> Y > 0 if there is no overflow. 340 ; CHECK: %c = icmp sgt i32 %y, 0 341 ; CHECK: ret i1 %c 342 define i1 @test35(i32 %x, i32 %y) { 343 %rhs = sub nsw i32 %x, %y 344 %c = icmp sgt i32 %x, %rhs 345 ret i1 %c 346 } 347 348 ; CHECK: @test36 349 ; X > X - Y -> Y > 0 if there is no overflow. 350 ; CHECK: %c = icmp ne i32 %y, 0 351 ; CHECK: ret i1 %c 352 define i1 @test36(i32 %x, i32 %y) { 353 %rhs = sub nuw i32 %x, %y 354 %c = icmp ugt i32 %x, %rhs 355 ret i1 %c 356 } 357 358 ; CHECK: @test37 359 ; X - Y > X - Z -> Z > Y if there is no overflow. 360 ; CHECK: %c = icmp sgt i32 %z, %y 361 ; CHECK: ret i1 %c 362 define i1 @test37(i32 %x, i32 %y, i32 %z) { 363 %lhs = sub nsw i32 %x, %y 364 %rhs = sub nsw i32 %x, %z 365 %c = icmp sgt i32 %lhs, %rhs 366 ret i1 %c 367 } 368 369 ; CHECK: @test38 370 ; X - Y > X - Z -> Z > Y if there is no overflow. 371 ; CHECK: %c = icmp ugt i32 %z, %y 372 ; CHECK: ret i1 %c 373 define i1 @test38(i32 %x, i32 %y, i32 %z) { 374 %lhs = sub nuw i32 %x, %y 375 %rhs = sub nuw i32 %x, %z 376 %c = icmp ugt i32 %lhs, %rhs 377 ret i1 %c 378 }
 208 208 %tmp5 = icmp eq i32 %tmp2, %tmp4 209 209 ret i1 %tmp5 210 210 ; CHECK: @test22 211 ; CHECK: %tmp5 = icmp eq i32 %a, %b⏎ 211 ; CHECK: %tmp5 = icmp eq i32 %b, %a⏎ 212 212 ; CHECK: ret i1 %tmp5 213 213 } 214 214