Skip to content

Commit 265fa12

Browse files
committed
For PR9500.
--- Merging r128041 into '.': U test/CodeGen/X86/fast-isel-gep.ll U lib/Target/X86/X86FastISel.cpp llvm-svn: 128042
1 parent 59fe9d1 commit 265fa12

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

llvm/lib/Target/X86/X86FastISel.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -399,33 +399,39 @@ bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
399399
Disp += SL->getElementOffset(Idx);
400400
} else {
401401
uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
402-
SmallVector<const Value *, 4> Worklist;
403-
Worklist.push_back(Op);
404-
do {
405-
Op = Worklist.pop_back_val();
402+
for (;;) {
406403
if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
407404
// Constant-offset addressing.
408405
Disp += CI->getSExtValue() * S;
409-
} else if (isa<AddOperator>(Op) &&
410-
isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
411-
// An add with a constant operand. Fold the constant.
406+
break;
407+
}
408+
if (isa<AddOperator>(Op) &&
409+
(!isa<Instruction>(Op) ||
410+
FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
411+
== FuncInfo.MBB) &&
412+
isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
413+
// An add (in the same block) with a constant operand. Fold the
414+
// constant.
412415
ConstantInt *CI =
413416
cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
414417
Disp += CI->getSExtValue() * S;
415-
// Add the other operand back to the work list.
416-
Worklist.push_back(cast<AddOperator>(Op)->getOperand(0));
417-
} else if (IndexReg == 0 &&
418-
(!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
419-
(S == 1 || S == 2 || S == 4 || S == 8)) {
418+
// Iterate on the other operand.
419+
Op = cast<AddOperator>(Op)->getOperand(0);
420+
continue;
421+
}
422+
if (IndexReg == 0 &&
423+
(!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
424+
(S == 1 || S == 2 || S == 4 || S == 8)) {
420425
// Scaled-index addressing.
421426
Scale = S;
422427
IndexReg = getRegForGEPIndex(Op).first;
423428
if (IndexReg == 0)
424429
return false;
425-
} else
426-
// Unsupported.
427-
goto unsupported_gep;
428-
} while (!Worklist.empty());
430+
break;
431+
}
432+
// Unsupported.
433+
goto unsupported_gep;
434+
}
429435
}
430436
}
431437
// Check for displacement overflow.

llvm/test/CodeGen/X86/fast-isel-gep.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,23 @@ define i64 @test5(i8* %A, i32 %I, i64 %B) nounwind {
8787
; X64-NEXT: ret
8888
}
8989

90+
; PR9500, rdar://9156159 - Don't do non-local address mode folding,
91+
; because it may require values which wouldn't otherwise be live out
92+
; of their blocks.
93+
define void @test6() {
94+
if.end: ; preds = %if.then, %invoke.cont
95+
%tmp15 = load i64* undef
96+
%dec = add i64 %tmp15, 13
97+
store i64 %dec, i64* undef
98+
%call17 = invoke i8* @_ZNK18G__FastAllocString4dataEv()
99+
to label %invoke.cont16 unwind label %lpad
90100

101+
invoke.cont16: ; preds = %if.then14
102+
%arrayidx18 = getelementptr inbounds i8* %call17, i64 %dec
103+
store i8 0, i8* %arrayidx18
104+
unreachable
105+
106+
lpad: ; preds = %if.end19, %if.then14, %if.end, %entry
107+
unreachable
108+
}
109+
declare i8* @_ZNK18G__FastAllocString4dataEv() nounwind

0 commit comments

Comments
 (0)