llvm.org GIT mirror llvm / 2bb04c9
Re-commit r257064, this time with a fixed assert In setInsertionPoint if the value is not a PHI, Instruction or Argument it should be a Constant, not a ConstantExpr. Original commit message: [InstCombine] Look through PHIs, GEPs, IntToPtrs and PtrToInts to expose more constants when comparing GEPs Summary: When comparing two GEP instructions which have the same base pointer and one of them has a constant index, it is possible to only compare indices, transforming it to a compare with a constant. This removes one use for the GEP instruction with the constant index, can reduce register pressure and can sometimes lead to removing the comparisson entirely. InstCombine was already doing this when comparing two GEPs if the base pointers were the same. However, in the case where we have complex pointer arithmetic (GEPs applied to GEPs, PHIs of GEPs, conversions to or from integers, etc) the value of the original base pointer will be hidden to the optimizer and this transformation will be disabled. This change detects when the two sides of the comparison can be expressed as GEPs with the same base pointer, even if they don't appear as such in the IR. The transformation will convert all the pointer arithmetic to arithmetic done on indices and all the relevant uses of GEPs to GEPs with a common base pointer. The GEP comparison will be converted to a comparison done on indices. Reviewers: majnemer, jmolloy Subscribers: hfinkel, jevinskie, jmolloy, aadg, llvm-commits Differential Revision: http://reviews.llvm.org/D15146 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@257164 91177308-0d34-0410-b5e6-96231b3b80d8 Silviu Baranga 4 years ago
2 changed file(s) with 422 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
1212
1313 #include "InstCombineInternal.h"
1414 #include "llvm/ADT/APSInt.h"
15 #include "llvm/ADT/SetVector.h"
1516 #include "llvm/ADT/Statistic.h"
1617 #include "llvm/Analysis/ConstantFolding.h"
1718 #include "llvm/Analysis/InstructionSimplify.h"
594595 return IC.Builder->CreateAdd(VariableIdx, OffsetVal, "offset");
595596 }
596597
598 /// Returns true if we can rewrite Start as a GEP with pointer Base
599 /// and some integer offset. The nodes that need to be re-written
600 /// for this transformation will be added to Explored.
601 static bool canRewriteGEPAsOffset(Value *Start, Value *Base,
602 const DataLayout &DL,
603 SetVector &Explored) {
604 SmallVector WorkList(1, Start);
605 Explored.insert(Base);
606
607 // The following traversal gives us an order which can be used
608 // when doing the final transformation. Since in the final
609 // transformation we create the PHI replacement instructions first,
610 // we don't have to get them in any particular order.
611 //
612 // However, for other instructions we will have to traverse the
613 // operands of an instruction first, which means that we have to
614 // do a post-order traversal.
615 while (!WorkList.empty()) {
616 SetVector PHIs;
617
618 while (!WorkList.empty()) {
619 if (Explored.size() >= 100)
620 return false;
621
622 Value *V = WorkList.back();
623
624 if (Explored.count(V) != 0) {
625 WorkList.pop_back();
626 continue;
627 }
628
629 if (!isa(V) && !isa(V) &&
630 !isa(V) && !isa(V))
631 // We've found some value that we can't explore which is different from
632 // the base. Therefore we can't do this transformation.
633 return false;
634
635 if (isa(V) || isa(V)) {
636 auto *CI = dyn_cast(V);
637 if (!CI->isNoopCast(DL))
638 return false;
639
640 if (Explored.count(CI->getOperand(0)) == 0)
641 WorkList.push_back(CI->getOperand(0));
642 }
643
644 if (auto *GEP = dyn_cast(V)) {
645 // We're limiting the GEP to having one index. This will preserve
646 // the original pointer type. We could handle more cases in the
647 // future.
648 if (GEP->getNumIndices() != 1 || !GEP->isInBounds())
649 return false;
650
651 if (Explored.count(GEP->getOperand(0)) == 0)
652 WorkList.push_back(GEP->getOperand(0));
653 }
654
655 if (WorkList.back() == V) {
656 WorkList.pop_back();
657 // We've finished visiting this node, mark it as such.
658 Explored.insert(V);
659 }
660
661 if (auto *PN = dyn_cast(V)) {
662 Explored.insert(PN);
663 PHIs.insert(PN);
664 }
665 }
666
667 // Explore the PHI nodes further.
668 for (auto *PN : PHIs)
669 for (Value *Op : PN->incoming_values())
670 if (Explored.count(Op) == 0)
671 WorkList.push_back(Op);
672 }
673
674 // Make sure that we can do this. Since we can't insert GEPs in a basic
675 // block before a PHI node, we can't easily do this transformation if
676 // we have PHI node users of transformed instructions.
677 for (Value *Val : Explored) {
678 for (Value *Use : Val->uses()) {
679
680 auto *PHI = dyn_cast(Use);
681 auto *Inst = dyn_cast(Val);
682
683 if (Inst == Base || Inst == PHI || !Inst || !PHI ||
684 Explored.count(PHI) == 0)
685 continue;
686
687 if (PHI->getParent() == Inst->getParent())
688 return false;
689 }
690 }
691 return true;
692 }
693
694 // Sets the appropriate insert point on Builder where we can add
695 // a replacement Instruction for V (if that is possible).
696 static void setInsertionPoint(IRBuilder<> &Builder, Value *V,
697 bool Before = true) {
698 if (auto *PHI = dyn_cast(V)) {
699 Builder.SetInsertPoint(&*PHI->getParent()->getFirstInsertionPt());
700 return;
701 }
702 if (auto *I = dyn_cast(V)) {
703 if (!Before)
704 I = &*std::next(I->getIterator());
705 Builder.SetInsertPoint(I);
706 return;
707 }
708 if (auto *A = dyn_cast(V)) {
709 // Set the insertion point in the entry block.
710 BasicBlock &Entry = A->getParent()->getEntryBlock();
711 Builder.SetInsertPoint(&*Entry.getFirstInsertionPt());
712 return;
713 }
714 // Otherwise, this is a constant and we don't need to set a new
715 // insertion point.
716 assert(isa(V) && "Setting insertion point for unknown value!");
717 }
718
719 /// Returns a re-written value of Start as an indexed GEP using Base as a
720 /// pointer.
721 static Value *rewriteGEPAsOffset(Value *Start, Value *Base,
722 const DataLayout &DL,
723 SetVector &Explored) {
724 // Perform all the substitutions. This is a bit tricky because we can
725 // have cycles in our use-def chains.
726 // 1. Create the PHI nodes without any incoming values.
727 // 2. Create all the other values.
728 // 3. Add the edges for the PHI nodes.
729 // 4. Emit GEPs to get the original pointers.
730 // 5. Remove the original instructions.
731 Type *IndexType = IntegerType::get(
732 Base->getContext(), DL.getPointerTypeSizeInBits(Start->getType()));
733
734 DenseMap NewInsts;
735 NewInsts[Base] = ConstantInt::getNullValue(IndexType);
736
737 // Create the new PHI nodes, without adding any incoming values.
738 for (Value *Val : Explored) {
739 if (Val == Base)
740 continue;
741 // Create empty phi nodes. This avoids cyclic dependencies when creating
742 // the remaining instructions.
743 if (auto *PHI = dyn_cast(Val))
744 NewInsts[PHI] = PHINode::Create(IndexType, PHI->getNumIncomingValues(),
745 PHI->getName() + ".idx", PHI);
746 }
747 IRBuilder<> Builder(Base->getContext());
748
749 // Create all the other instructions.
750 for (Value *Val : Explored) {
751
752 if (NewInsts.find(Val) != NewInsts.end())
753 continue;
754
755 if (auto *CI = dyn_cast(Val)) {
756 NewInsts[CI] = NewInsts[CI->getOperand(0)];
757 continue;
758 }
759 if (auto *GEP = dyn_cast(Val)) {
760 Value *Index = NewInsts[GEP->getOperand(1)]
761 ? NewInsts[GEP->getOperand(1)]
762 : GEP->getOperand(1);
763 setInsertionPoint(Builder, GEP);
764 // Indices might need to be sign extended. GEPs will magically do
765 // this, but we need to do it ourselves here.
766 if (Index->getType()->getScalarSizeInBits() !=
767 NewInsts[GEP->getOperand(0)]->getType()->getScalarSizeInBits()) {
768 Index = Builder.CreateSExtOrTrunc(
769 Index, NewInsts[GEP->getOperand(0)]->getType(),
770 GEP->getOperand(0)->getName() + ".sext");
771 }
772 NewInsts[GEP] =
773 Builder.CreateAdd(NewInsts[GEP->getOperand(0)], Index,
774 GEP->getOperand(0)->getName() + ".add");
775 continue;
776
777 }
778 if (isa(Val))
779 continue;
780
781 llvm_unreachable("Unexpected instruction type");
782 }
783
784 // Add the incoming values to the PHI nodes.
785 for (Value *Val : Explored) {
786 if (Val == Base)
787 continue;
788 // All the instructions have been created, we can now add edges to the
789 // phi nodes.
790 if (auto *PHI = dyn_cast(Val)) {
791 PHINode *NewPhi = static_cast(NewInsts[PHI]);
792 for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) {
793 Value *NewIncoming = PHI->getIncomingValue(I);
794
795 if (NewInsts.find(NewIncoming) != NewInsts.end())
796 NewIncoming = NewInsts[NewIncoming];
797
798 NewPhi->addIncoming(NewIncoming, PHI->getIncomingBlock(I));
799 }
800 }
801 }
802
803 // If required, create a inttoptr instruction.
804 Value *NewBase = Base;
805 setInsertionPoint(Builder, Base, false);
806 if (!Base->getType()->isPointerTy())
807 NewBase = Builder.CreateBitOrPointerCast(Base, Start->getType(),
808 Start->getName() + "to.ptr");
809
810 for (Value *Val : Explored) {
811 if (Val == Base)
812 continue;
813
814 // Depending on the type, for external users we have to emit
815 // a GEP or a GEP + ptrtoint.
816 if (isa(NewInsts[Val]))
817 setInsertionPoint(Builder, NewInsts[Val], false);
818 else
819 setInsertionPoint(Builder, NewBase, false);
820
821 Value *GEP =
822 Builder.CreateInBoundsGEP(Start->getType()->getPointerElementType(),
823 NewBase, {NewInsts[Val]},
824 Val->getName() + ".ptr");
825
826 if (!Val->getType()->isPointerTy()) {
827 Value *Cast = Builder.CreatePointerCast(GEP, Val->getType(),
828 Val->getName() + ".conv");
829 GEP = Cast;
830 }
831 Val->replaceAllUsesWith(GEP);
832 }
833
834 return NewInsts[Start];
835 }
836
837 /// Looks through GEPs, IntToPtrInsts and PtrToIntInsts in order to express
838 /// the input Value as a GEP constant indexed GEP. Returns a pair containing
839 /// the GEPs Pointer and Index.
840 static std::pair
841 getAsConstantIndexedAddress(Value *V, const DataLayout &DL) {
842 Type *IndexType =
843 IntegerType::get(V->getContext(),
844 DL.getPointerTypeSizeInBits(V->getType()));
845
846 Constant *Index = ConstantInt::getNullValue(IndexType);
847 while (true) {
848 if (GEPOperator *GEP = dyn_cast(V)) {
849 // We accept only inbouds GEPs here to exclude the possibility of
850 // overflow.
851 if (!GEP->isInBounds())
852 break;
853 if (GEP->hasAllConstantIndices() && GEP->getNumIndices() == 1) {
854 V = GEP->getOperand(0);
855 Constant *GEPIndex = static_cast(GEP->getOperand(1));
856 Index = ConstantExpr::getAdd(
857 Index, ConstantExpr::getSExtOrBitCast(GEPIndex, IndexType));
858 continue;
859 }
860 break;
861 }
862 if (auto *CI = dyn_cast(V)) {
863 if (!CI->isNoopCast(DL))
864 break;
865 V = CI->getOperand(0);
866 continue;
867 }
868 if (auto *CI = dyn_cast(V)) {
869 if (!CI->isNoopCast(DL))
870 break;
871 V = CI->getOperand(0);
872 continue;
873 }
874 break;
875 }
876 return {V, Index};
877 }
878
879 // Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
880 // We can look through PHIs, GEPs and casts in order to determine a
881 // common base between GEPLHS and RHS.
882 static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS,
883 ICmpInst::Predicate Cond,
884 const DataLayout &DL) {
885 if (!GEPLHS->hasAllConstantIndices())
886 return nullptr;
887
888 Value *PtrBase, *Index;
889 std::tie(PtrBase, Index) = getAsConstantIndexedAddress(GEPLHS, DL);
890
891 // The set of nodes that will take part in this transformation.
892 SetVector Nodes;
893
894 if (!canRewriteGEPAsOffset(RHS, PtrBase, DL, Nodes))
895 return nullptr;
896
897 // We know we can re-write this as
898 // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)
899 // Since we've only looked through inbouds GEPs we know that we
900 // can't have overflow on either side. We can therefore re-write
901 // this as:
902 // OFFSET1 cmp OFFSET2
903 Value *NewRHS = rewriteGEPAsOffset(RHS, PtrBase, DL, Nodes);
904
905 // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written
906 // GEP having PtrBase as the pointer base, and has returned in NewRHS the
907 // offset. Since Index is the offset of LHS to the base pointer, we will now
908 // compare the offsets instead of comparing the pointers.
909 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Index, NewRHS);
910 }
911
597912 /// FoldGEPICmp - Fold comparisons between a GEP instruction and something
598913 /// else. At this point we know that the GEP is on the LHS of the comparison.
599914 Instruction *InstCombiner::FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
673988 }
674989
675990 // Otherwise, the base pointers are different and the indices are
676 // different, bail out.
677 return nullptr;
991 // different. Try convert this to an indexed compare by looking through
992 // PHIs/casts.
993 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL);
678994 }
679995
680996 // If one of the GEPs has all zero indices, recurse.
7261042 return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
7271043 }
7281044 }
729 return nullptr;
1045
1046 // Try convert this to an indexed compare by looking through PHIs/casts as a
1047 // last resort.
1048 return transformToIndexedCompare(GEPLHS, RHS, Cond, DL);
7301049 }
7311050
7321051 Instruction *InstCombiner::FoldAllocaCmp(ICmpInst &ICI, AllocaInst *Alloca,
0 ; RUN: opt -instcombine -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:32-f32:32:32-f64:32:32-v64:64:64-v128:128:128-a0:0:64"
3
4 define i32 *@test1(i32* %A, i32 %Offset) {
5 entry:
6 %tmp = getelementptr inbounds i32, i32* %A, i32 %Offset
7 br label %bb
8
9 bb:
10 %RHS = phi i32* [ %RHS.next, %bb ], [ %tmp, %entry ]
11 %LHS = getelementptr inbounds i32, i32* %A, i32 100
12 %RHS.next = getelementptr inbounds i32, i32* %RHS, i64 1
13 %cond = icmp ult i32 * %LHS, %RHS
14 br i1 %cond, label %bb2, label %bb
15
16 bb2:
17 ret i32* %RHS
18
19 ; CHECK-LABEL: @test1(
20 ; CHECK: %[[INDEX:[0-9A-Za-z.]+]] = phi i32 [ %[[ADD:[0-9A-Za-z.]+]], %bb ], [ %Offset, %entry ]
21 ; CHECK: %[[ADD]] = add i32 %[[INDEX]], 1
22 ; CHECK: %cond = icmp sgt i32 %[[INDEX]], 100
23 ; CHECK: br i1 %cond, label %bb2, label %bb
24 ; CHECK: %[[PTR:[0-9A-Za-z.]+]] = getelementptr inbounds i32, i32* %A, i32 %[[INDEX]]
25 ; CHECK: ret i32* %[[PTR]]
26 }
27
28 define i32 *@test2(i32 %A, i32 %Offset) {
29 entry:
30 %A.ptr = inttoptr i32 %A to i32*
31 %tmp = getelementptr inbounds i32, i32* %A.ptr, i32 %Offset
32 br label %bb
33
34 bb:
35 %RHS = phi i32* [ %RHS.next, %bb ], [ %tmp, %entry ]
36 %LHS = getelementptr inbounds i32, i32* %A.ptr, i32 100
37 %RHS.next = getelementptr inbounds i32, i32* %RHS, i64 1
38 %cmp0 = ptrtoint i32 *%LHS to i32
39 %cmp1 = ptrtoint i32 *%RHS to i32
40 %cond = icmp ult i32 %cmp0, %cmp1
41 br i1 %cond, label %bb2, label %bb
42
43 bb2:
44 ret i32* %RHS
45
46 ; CHECK-LABEL: @test2(
47 ; CHECK: %[[TOPTR:[0-9A-Za-z.]+]] = inttoptr i32 %[[ADD:[0-9A-Za-z.]+]] to i32*
48 ; CHECK: %[[INDEX:[0-9A-Za-z.]+]] = phi i32 [ %[[ADD:[0-9A-Za-z.]+]], %bb ], [ %Offset, %entry ]
49 ; CHECK: %[[ADD]] = add i32 %[[INDEX]], 1
50 ; CHECK: %cond = icmp sgt i32 %[[INDEX]], 100
51 ; CHECK: br i1 %cond, label %bb2, label %bb
52 ; CHECK: %[[PTR:[0-9A-Za-z.]+]] = getelementptr inbounds i32, i32* %[[TOPTR]], i32 %[[INDEX]]
53 ; CHECK: ret i32* %[[PTR]]
54 }
55
56 ; Perform the transformation only if we know that the GEPs used are inbounds.
57 define i32 *@test3(i32* %A, i32 %Offset) {
58 entry:
59 %tmp = getelementptr i32, i32* %A, i32 %Offset
60 br label %bb
61
62 bb:
63 %RHS = phi i32* [ %RHS.next, %bb ], [ %tmp, %entry ]
64 %LHS = getelementptr i32, i32* %A, i32 100
65 %RHS.next = getelementptr i32, i32* %RHS, i64 1
66 %cond = icmp ult i32 * %LHS, %RHS
67 br i1 %cond, label %bb2, label %bb
68
69 bb2:
70 ret i32* %RHS
71
72 ; CHECK-LABEL: @test3(
73 ; CHECK-NOT: %cond = icmp sgt i32 %{{[0-9A-Za-z.]+}}, 100
74 }
75
76 ; An inttoptr that requires an extension or truncation will be opaque when determining
77 ; the base pointer. In this case we can still perform the transformation by considering
78 ; A.ptr as being the base pointer.
79 define i32 *@test4(i16 %A, i32 %Offset) {
80 entry:
81 %A.ptr = inttoptr i16 %A to i32*
82 %tmp = getelementptr inbounds i32, i32* %A.ptr, i32 %Offset
83 br label %bb
84
85 bb:
86 %RHS = phi i32* [ %RHS.next, %bb ], [ %tmp, %entry ]
87 %LHS = getelementptr inbounds i32, i32* %A.ptr, i32 100
88 %RHS.next = getelementptr inbounds i32, i32* %RHS, i64 1
89 %cmp0 = ptrtoint i32 *%LHS to i32
90 %cmp1 = ptrtoint i32 *%RHS to i32
91 %cond = icmp ult i32 %cmp0, %cmp1
92 br i1 %cond, label %bb2, label %bb
93
94 bb2:
95 ret i32* %RHS
96
97 ; CHECK-LABEL: @test4(
98 ; CHECK: %cond = icmp sgt i32 %{{[0-9A-Za-z.]+}}, 100
99 }