llvm.org GIT mirror llvm / b686af0
Recommit the fix for rdar://9289512 with a couple tweaks to fix bugs exposed by the gcc dejagnu testsuite: 1. The load may actually be used by a dead instruction, which would cause an assert. 2. The load may not be used by the current chain of instructions, and we could move it past a side-effecting instruction. Change how we process uses to define the problem away. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@130018 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 9 years ago
4 changed file(s) with 113 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
279279
280280 void PrepareEHLandingPad();
281281 void SelectAllBasicBlocks(const Function &Fn);
282 bool TryToFoldFastISelLoad(const LoadInst *LI, FastISel *FastIS);
282 bool TryToFoldFastISelLoad(const LoadInst *LI, const Instruction *FoldInst,
283 FastISel *FastIS);
283284 void FinishBasicBlock();
284285
285286 void SelectBasicBlock(BasicBlock::const_iterator Begin,
745745
746746
747747
748
748 /// TryToFoldFastISelLoad - We're checking to see if we can fold the specified
749 /// load into the specified FoldInst. Note that we could have a sequence where
750 /// multiple LLVM IR instructions are folded into the same machineinstr. For
751 /// example we could have:
752 /// A: x = load i32 *P
753 /// B: y = icmp A, 42
754 /// C: br y, ...
755 ///
756 /// In this scenario, LI is "A", and FoldInst is "C". We know about "B" (and
757 /// any other folded instructions) because it is between A and C.
758 ///
759 /// If we succeed in folding the load into the operation, return true.
760 ///
749761 bool SelectionDAGISel::TryToFoldFastISelLoad(const LoadInst *LI,
762 const Instruction *FoldInst,
750763 FastISel *FastIS) {
764 // We know that the load has a single use, but don't know what it is. If it
765 // isn't one of the folded instructions, then we can't succeed here. Handle
766 // this by scanning the single-use users of the load until we get to FoldInst.
767 unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs.
768
769 const Instruction *TheUser = LI->use_back();
770 while (TheUser != FoldInst && // Scan up until we find FoldInst.
771 // Stay in the right block.
772 TheUser->getParent() == FoldInst->getParent() &&
773 --MaxUsers) { // Don't scan too far.
774 // If there are multiple or no uses of this instruction, then bail out.
775 if (!TheUser->hasOneUse())
776 return false;
777
778 TheUser = TheUser->use_back();
779 }
780
751781 // Don't try to fold volatile loads. Target has to deal with alignment
752782 // constraints.
753783 if (LI->isVolatile()) return false;
754784
755 // Figure out which vreg this is going into.
785 // Figure out which vreg this is going into. If there is no assigned vreg yet
786 // then there actually was no reference to it. Perhaps the load is referenced
787 // by a dead instruction.
756788 unsigned LoadReg = FastIS->getRegForValue(LI);
757 assert(LoadReg && "Load isn't already assigned a vreg? ");
789 if (LoadReg == 0)
790 return false;
758791
759792 // Check to see what the uses of this vreg are. If it has no uses, or more
760793 // than one use (at the machine instr level) then we can't fold it.
832865 /// Return false if it needs to be emitted.
833866 static bool isFoldedOrDeadInstruction(const Instruction *I,
834867 FunctionLoweringInfo *FuncInfo) {
835 return !I->mayWriteToMemory() &&
836 !isa(I) &&
837 !isa(I) &&
838 !FuncInfo->isExportedInst(I);
868 return !I->mayWriteToMemory() && // Side-effecting instructions aren't folded.
869 !isa(I) && // Terminators aren't folded.
870 !isa(I) && // Debug instructions aren't folded.
871 !FuncInfo->isExportedInst(I); // Exported instrs must be computed.
839872 }
840873
841874 void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) {
927960
928961 // Try to select the instruction with FastISel.
929962 if (FastIS->SelectInstruction(Inst)) {
930 // If fast isel succeeded, check to see if there is a single-use
931 // non-volatile load right before the selected instruction, and see if
932 // the load is used by the instruction. If so, try to fold it.
933 const Instruction *BeforeInst = 0;
934 if (Inst != Begin)
935 BeforeInst = llvm::prior(llvm::prior(BI));
936 if (BeforeInst && isa(BeforeInst) &&
937 BeforeInst->hasOneUse() && *BeforeInst->use_begin() == Inst &&
938 TryToFoldFastISelLoad(cast(BeforeInst), FastIS))
939 --BI; // If we succeeded, don't re-select the load.
963 // If fast isel succeeded, skip over all the folded instructions, and
964 // then see if there is a load right before the selected instructions.
965 // Try to fold the load if so.
966 const Instruction *BeforeInst = Inst;
967 while (BeforeInst != Begin) {
968 BeforeInst = llvm::prior(BasicBlock::const_iterator(BeforeInst));
969 if (!isFoldedOrDeadInstruction(BeforeInst, FuncInfo))
970 break;
971 }
972 if (BeforeInst != Inst && isa(BeforeInst) &&
973 BeforeInst->hasOneUse() &&
974 TryToFoldFastISelLoad(cast(BeforeInst), Inst, FastIS))
975 // If we succeeded, don't re-select the load.
976 BI = llvm::next(BasicBlock::const_iterator(BeforeInst));
940977 continue;
941978 }
942979
1111
1212 ; CHECK: test1:
1313 ; CHECK: andl $8,
14
15
16 ; rdar://9289512 - The load should fold into the compare.
17 define void @test2(i64 %x) nounwind ssp {
18 entry:
19 %x.addr = alloca i64, align 8
20 store i64 %x, i64* %x.addr, align 8
21 %tmp = load i64* %x.addr, align 8
22 %cmp = icmp sgt i64 %tmp, 42
23 br i1 %cmp, label %if.then, label %if.end
24
25 if.then: ; preds = %entry
26 br label %if.end
27
28 if.end: ; preds = %if.then, %entry
29 ret void
30 ; CHECK: test2:
31 ; CHECK: movq %rdi, -8(%rsp)
32 ; CHECK: cmpq $42, -8(%rsp)
33 }
34
35
1436
1537
1638 @G = external global i32
177199 call void (...)* @test16callee(double 1.000000e+00)
178200 ret void
179201 }
202
203
204 declare void @foo() unnamed_addr ssp align 2
205
206 ; Verify that we don't fold the load into the compare here. That would move it
207 ; w.r.t. the call.
208 define i32 @test17(i32 *%P) ssp nounwind {
209 entry:
210 %tmp = load i32* %P
211 %cmp = icmp ne i32 %tmp, 5
212 call void @foo()
213 br i1 %cmp, label %if.then, label %if.else
214
215 if.then: ; preds = %entry
216 ret i32 1
217
218 if.else: ; preds = %entry
219 ret i32 2
220 ; CHECK: test17:
221 ; CHECK: movl (%rdi), %eax
222 ; CHECK: callq _foo
223 ; CHECK: cmpl $5, %eax
224 ; CHECK-NEXT: je
225 }
226
9191 store i1 %t, i1* %q
9292 ret void
9393 }
94
95
96 @crash_test1x = external global <2 x i32>, align 8
97
98 define void @crash_test1() nounwind ssp {
99 %tmp = load <2 x i32>* @crash_test1x, align 8
100 %neg = xor <2 x i32> %tmp,
101 ret void
102 }
103