llvm.org GIT mirror llvm / 1c14c29
refactor handling of symbolic constant folding, picking up a few new cases( see Integer/a1.ll), but not anything that would happen in practice. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49965 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 11 years ago
2 changed file(s) with 67 addition(s) and 67 deletion(s). Raw diff Collapse all Expand all
518518 }
519519 }
520520
521 if (const ConstantExpr *CE1 = dyn_cast(C1)) {
522 if (isa(C2)) {
523 // There are many possible foldings we could do here. We should probably
524 // at least fold add of a pointer with an integer into the appropriate
525 // getelementptr. This will improve alias analysis a bit.
526 } else {
527 // Just implement a couple of simple identities.
528 switch (Opcode) {
529 case Instruction::Add:
530 if (C2->isNullValue()) return const_cast(C1); // X + 0 == X
531 break;
532 case Instruction::Sub:
533 if (C2->isNullValue()) return const_cast(C1); // X - 0 == X
534 break;
535 case Instruction::Mul:
536 if (C2->isNullValue()) return const_cast(C2); // X * 0 == 0
537 if (const ConstantInt *CI = dyn_cast(C2))
538 if (CI->equalsInt(1))
539 return const_cast(C1); // X * 1 == X
540 break;
541 case Instruction::UDiv:
542 case Instruction::SDiv:
543 if (const ConstantInt *CI = dyn_cast(C2))
544 if (CI->equalsInt(1))
545 return const_cast(C1); // X / 1 == X
546 break;
547 case Instruction::URem:
548 case Instruction::SRem:
549 if (const ConstantInt *CI = dyn_cast(C2))
550 if (CI->equalsInt(1))
551 return Constant::getNullValue(CI->getType()); // X % 1 == 0
552 break;
553 case Instruction::And:
554 if (const ConstantInt *CI = dyn_cast(C2)) {
555 if (CI->isZero()) return const_cast(C2); // X & 0 == 0
556 if (CI->isAllOnesValue())
557 return const_cast(C1); // X & -1 == X
558
559 // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
560 if (CE1->getOpcode() == Instruction::ZExt) {
561 APInt PossiblySetBits
562 = cast(CE1->getOperand(0)->getType())->getMask();
563 PossiblySetBits.zext(C1->getType()->getPrimitiveSizeInBits());
564 if ((PossiblySetBits & CI->getValue()) == PossiblySetBits)
565 return const_cast(C1);
566 }
521 // Handle simplifications of the RHS when a constant int.
522 if (const ConstantInt *CI2 = dyn_cast(C2)) {
523 switch (Opcode) {
524 case Instruction::Add:
525 if (CI2->equalsInt(0)) return const_cast(C1); // X + 0 == X
526 break;
527 case Instruction::Sub:
528 if (CI2->equalsInt(0)) return const_cast(C1); // X - 0 == X
529 break;
530 case Instruction::Mul:
531 if (CI2->equalsInt(0)) return const_cast(C2); // X * 0 == 0
532 if (CI2->equalsInt(1))
533 return const_cast(C1); // X * 1 == X
534 break;
535 case Instruction::UDiv:
536 case Instruction::SDiv:
537 if (CI2->equalsInt(1))
538 return const_cast(C1); // X / 1 == X
539 break;
540 case Instruction::URem:
541 case Instruction::SRem:
542 if (CI2->equalsInt(1))
543 return Constant::getNullValue(CI2->getType()); // X % 1 == 0
544 break;
545 case Instruction::And:
546 if (CI2->isZero()) return const_cast(C2); // X & 0 == 0
547 if (CI2->isAllOnesValue())
548 return const_cast(C1); // X & -1 == X
549
550 // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
551 if (const ConstantExpr *CE1 = dyn_cast(C1)) {
552 if (CE1->getOpcode() == Instruction::ZExt) {
553 unsigned DstWidth = CI2->getType()->getBitWidth();
554 unsigned SrcWidth =
555 CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
556 APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
557 if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
558 return const_cast(C1);
567559 }
568 if (CE1->isCast() && isa(CE1->getOperand(0))) {
560
561 if (CE1->getOpcode() == Instruction::PtrToInt &&
562 isa(CE1->getOperand(0))) {
569563 GlobalValue *CPR = cast(CE1->getOperand(0));
570
564
571565 // Functions are at least 4-byte aligned. If and'ing the address of a
572566 // function with a constant < 4, fold it to zero.
573567 if (const ConstantInt *CI = dyn_cast(C2))
575569 isa(CPR))
576570 return Constant::getNullValue(CI->getType());
577571 }
578 break;
579 case Instruction::Or:
580 if (C2->isNullValue()) return const_cast(C1); // X | 0 == X
581 if (const ConstantInt *CI = dyn_cast(C2))
582 if (CI->isAllOnesValue())
583 return const_cast(C2); // X | -1 == -1
584 break;
585 case Instruction::Xor:
586 if (C2->isNullValue()) return const_cast(C1); // X ^ 0 == X
587 break;
588 case Instruction::AShr:
589 // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
572 }
573 break;
574 case Instruction::Or:
575 if (CI2->equalsInt(0)) return const_cast(C1); // X | 0 == X
576 if (CI2->isAllOnesValue())
577 return const_cast(C2); // X | -1 == -1
578 break;
579 case Instruction::Xor:
580 if (CI2->equalsInt(0)) return const_cast(C1); // X ^ 0 == X
581 break;
582 case Instruction::AShr:
583 // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
584 if (const ConstantExpr *CE1 = dyn_cast(C1))
590585 if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero.
591586 return ConstantExpr::getLShr(const_cast(C1),
592587 const_cast(C2));
593 break;
594 }
595 }
588 break;
589 }
590 }
591
592 if (isa(C1)) {
593 // There are many possible foldings we could do here. We should probably
594 // at least fold add of a pointer with an integer into the appropriate
595 // getelementptr. This will improve alias analysis a bit.
596596 } else if (isa(C2)) {
597597 // If C2 is a constant expr and C1 isn't, flop them around and fold the
598598 // other way if possible.
99 @j = constant i1 undef ; [#uses=0]
1010 @m = constant i1 undef ; [#uses=0]
1111 @n = constant i1 true ; [#uses=0]
12 @o = constant i1 sdiv (i1 true, i1 true) ; [#uses=0]
13 @p = constant i1 sdiv (i1 true, i1 true) ; [#uses=0]
12 @o = constant i1 true ; [#uses=0]
13 @p = constant i1 true ; [#uses=0]
1414 @q = constant i1 true ; [#uses=0]
1515 @r = constant i1 true ; [#uses=0]
16 @s = constant i1 srem (i1 true, i1 true) ; [#uses=0]
16 @s = constant i1 false ; [#uses=0]
1717 @t = constant i1 false ; [#uses=0]
18 @u = constant i1 srem (i1 true, i1 true) ; [#uses=0]
18 @u = constant i1 false ; [#uses=0]