llvm.org GIT mirror llvm / 03e091f
Reapply r155682, making constant folding more consistent, with a fix to work properly with how the code handles all-undef PHI nodes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@155721 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 7 years ago
5 changed file(s) with 90 addition(s) and 38 deletion(s). Raw diff Collapse all Expand all
779779 // all operands are constants.
780780 if (isa(Incoming))
781781 continue;
782 // If the incoming value is not a constant, or is a different constant to
782 // If the incoming value is not a constant, then give up.
783 Constant *C = dyn_cast(Incoming);
784 if (!C)
785 return 0;
786 // Fold the PHI's operands.
787 if (ConstantExpr *NewC = dyn_cast(C))
788 C = ConstantFoldConstantExpression(NewC, TD, TLI);
789 // If the incoming value is a different constant to
783790 // the one we saw previously, then give up.
784 Constant *C = dyn_cast(Incoming);
785 if (!C || (CommonValue && C != CommonValue))
791 if (CommonValue && C != CommonValue)
786792 return 0;
787793 CommonValue = C;
788794 }
795
789796
790797 // If we reach here, all incoming values are the same constant or undef.
791798 return CommonValue ? CommonValue : UndefValue::get(PN->getType());
794801 // Scan the operand list, checking to see if they are all constants, if so,
795802 // hand off to ConstantFoldInstOperands.
796803 SmallVector Ops;
797 for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
798 if (Constant *Op = dyn_cast(*i))
799 Ops.push_back(Op);
800 else
804 for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) {
805 Constant *Op = dyn_cast(*i);
806 if (!Op)
801807 return 0; // All operands not constant!
808
809 // Fold the Instruction's operands.
810 if (ConstantExpr *NewCE = dyn_cast(Op))
811 Op = ConstantFoldConstantExpression(NewCE, TD, TLI);
812
813 Ops.push_back(Op);
814 }
802815
803816 if (const CmpInst *CI = dyn_cast(I))
804817 return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
5454
5555 Type *DstEltTy = DstTy->getElementType();
5656
57 // Check to verify that all elements of the input are simple.
5857 SmallVector Result;
58 Type *Ty = IntegerType::get(CV->getContext(), 32);
5959 for (unsigned i = 0; i != NumElts; ++i) {
60 Constant *C = CV->getAggregateElement(i);
61 if (C == 0) return 0;
60 Constant *C =
61 ConstantExpr::getExtractElement(CV, ConstantInt::get(Ty, i));
6262 C = ConstantExpr::getBitCast(C, DstEltTy);
63 if (isa(C)) return 0;
6463 Result.push_back(C);
6564 }
6665
552551 SmallVector res;
553552 VectorType *DestVecTy = cast(DestTy);
554553 Type *DstEltTy = DestVecTy->getElementType();
555 for (unsigned i = 0, e = V->getType()->getVectorNumElements(); i != e; ++i)
556 res.push_back(ConstantExpr::getCast(opc,
557 V->getAggregateElement(i), DstEltTy));
554 Type *Ty = IntegerType::get(V->getContext(), 32);
555 for (unsigned i = 0, e = V->getType()->getVectorNumElements(); i != e; ++i) {
556 Constant *C =
557 ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
558 res.push_back(ConstantExpr::getCast(opc, C, DstEltTy));
559 }
558560 return ConstantVector::get(res);
559561 }
560562
695697 // If the condition is a vector constant, fold the result elementwise.
696698 if (ConstantVector *CondV = dyn_cast(Cond)) {
697699 SmallVector Result;
700 Type *Ty = IntegerType::get(CondV->getContext(), 32);
698701 for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){
699702 ConstantInt *Cond = dyn_cast(CondV->getOperand(i));
700703 if (Cond == 0) break;
701704
702 Constant *Res = (Cond->getZExtValue() ? V1 : V2)->getAggregateElement(i);
703 if (Res == 0) break;
705 Constant *V = Cond->isNullValue() ? V2 : V1;
706 Constant *Res = ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
704707 Result.push_back(Res);
705708 }
706709
759762 const APInt &IdxVal = CIdx->getValue();
760763
761764 SmallVector Result;
765 Type *Ty = IntegerType::get(Val->getContext(), 32);
762766 for (unsigned i = 0, e = Val->getType()->getVectorNumElements(); i != e; ++i){
763767 if (i == IdxVal) {
764768 Result.push_back(Elt);
765769 continue;
766770 }
767771
768 if (Constant *C = Val->getAggregateElement(i))
769 Result.push_back(C);
770 else
771 return 0;
772 Constant *C =
773 ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i));
774 Result.push_back(C);
772775 }
773776
774777 return ConstantVector::get(Result);
800803 Constant *InElt;
801804 if (unsigned(Elt) >= SrcNumElts*2)
802805 InElt = UndefValue::get(EltTy);
803 else if (unsigned(Elt) >= SrcNumElts)
804 InElt = V2->getAggregateElement(Elt - SrcNumElts);
805 else
806 InElt = V1->getAggregateElement(Elt);
807 if (InElt == 0) return 0;
806 else if (unsigned(Elt) >= SrcNumElts) {
807 Type *Ty = IntegerType::get(V2->getContext(), 32);
808 InElt =
809 ConstantExpr::getExtractElement(V2,
810 ConstantInt::get(Ty, Elt - SrcNumElts));
811 } else {
812 Type *Ty = IntegerType::get(V1->getContext(), 32);
813 InElt = ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, Elt));
814 }
808815 Result.push_back(InElt);
809816 }
810817
11291136 } else if (VectorType *VTy = dyn_cast(C1->getType())) {
11301137 // Perform elementwise folding.
11311138 SmallVector Result;
1139 Type *Ty = IntegerType::get(VTy->getContext(), 32);
11321140 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1133 Constant *LHS = C1->getAggregateElement(i);
1134 Constant *RHS = C2->getAggregateElement(i);
1135 if (LHS == 0 || RHS == 0) break;
1141 Constant *LHS =
1142 ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, i));
1143 Constant *RHS =
1144 ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, i));
11361145
11371146 Result.push_back(ConstantExpr::get(Opcode, LHS, RHS));
11381147 }
11391148
1140 if (Result.size() == VTy->getNumElements())
1141 return ConstantVector::get(Result);
1149 return ConstantVector::get(Result);
11421150 }
11431151
11441152 if (ConstantExpr *CE1 = dyn_cast(C1)) {
16961704 // If we can constant fold the comparison of each element, constant fold
16971705 // the whole vector comparison.
16981706 SmallVector ResElts;
1707 Type *Ty = IntegerType::get(C1->getContext(), 32);
16991708 // Compare the elements, producing an i1 result or constant expr.
17001709 for (unsigned i = 0, e = C1->getType()->getVectorNumElements(); i != e;++i){
1701 Constant *C1E = C1->getAggregateElement(i);
1702 Constant *C2E = C2->getAggregateElement(i);
1703 if (C1E == 0 || C2E == 0) break;
1710 Constant *C1E =
1711 ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, i));
1712 Constant *C2E =
1713 ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, i));
17041714
17051715 ResElts.push_back(ConstantExpr::getCompare(pred, C1E, C2E));
17061716 }
17071717
1708 if (ResElts.size() == C1->getType()->getVectorNumElements())
1709 return ConstantVector::get(ResElts);
1718 return ConstantVector::get(ResElts);
17101719 }
17111720
17121721 if (C1->getType()->isFloatingPointTy()) {
262262 ; OPT: ret i64 ptrtoint (double* getelementptr ({ i1, double }* null, i64 0, i32 1) to i64)
263263 ; OPT: }
264264 ; OPT: define i64 @fc() nounwind {
265 ; OPT: ret i64 mul nuw (i64 ptrtoint (double* getelementptr (double* null, i32 1) to i64), i64 2)
265 ; OPT: ret i64 mul (i64 ptrtoint (double* getelementptr (double* null, i32 1) to i64), i64 2)
266266 ; OPT: }
267267 ; OPT: define i64 @fd() nounwind {
268 ; OPT: ret i64 mul nuw (i64 ptrtoint (double* getelementptr (double* null, i32 1) to i64), i64 11)
268 ; OPT: ret i64 mul (i64 ptrtoint (double* getelementptr (double* null, i32 1) to i64), i64 11)
269269 ; OPT: }
270270 ; OPT: define i64 @fe() nounwind {
271271 ; OPT: ret i64 ptrtoint (double* getelementptr ({ double, float, double, double }* null, i64 0, i32 2) to i64)
432432 ; PLAIN: ret i32* %t
433433 ; PLAIN: }
434434 ; OPT: define i32* @fZ() nounwind {
435 ; OPT: ret i32* getelementptr inbounds (i32* getelementptr inbounds ([3 x { i32, i32 }]* @ext, i64 0, i64 1, i32 0), i64 1)
435 ; OPT: ret i32* getelementptr (i32* getelementptr inbounds ([3 x { i32, i32 }]* @ext, i64 0, i64 1, i32 0), i64 1)
436436 ; OPT: }
437437 ; TO: define i32* @fZ() nounwind {
438438 ; TO: ret i32* getelementptr inbounds ([3 x { i32, i32 }]* @ext, i64 0, i64 1, i32 1)
619619 ; CHECK-NOT: phi i32
620620 ; CHECK: ret i1 %z
621621 }
622
623 ; CHECK: @test27(
624 ; CHECK: ret i32 undef
625 define i32 @test27(i1 %b) {
626 entry:
627 br label %done
628 done:
629 %y = phi i32 [ undef, %entry ]
630 ret i32 %y
631 }
0 ; RUN: opt -sccp -S < %s | FileCheck %s
1
2 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32-S128"
3
4 ; CHECK: store volatile <2 x i64> zeroinitializer, <2 x i64>* %p
5 ; rdar://11324230
6
7 define void @foo(<2 x i64>* %p) nounwind {
8 entry:
9 br label %while.body.i
10
11 while.body.i: ; preds = %while.body.i, %entry
12 %vWorkExponent.i.033 = phi <4 x i32> [ %sub.i.i, %while.body.i ], [ , %entry ]
13 %sub.i.i = add <4 x i32> %vWorkExponent.i.033,
14 %0 = bitcast <4 x i32> %sub.i.i to <2 x i64>
15 %and.i119.i = and <2 x i64> %0, zeroinitializer
16 store volatile <2 x i64> %and.i119.i, <2 x i64>* %p
17 br label %while.body.i
18 }
19