Skip to content

Commit b0e24bd

Browse files
dtcxzywLukacma
authored andcommitted
[CodeGenPrepare] Don't simplify incomplete expression tree in AddrModeCombine (llvm#164628)
Since new select/phi instructions may construct loops, the expression tree to be simplified may still be incomplete (i.e., it may contain select with dummy values or phi without incoming values). This patch removes the call to simplifyInstruction for now, as it doesn't break existing tests. Original PR: https://reviews.llvm.org/D36073 Fix the crash reported in llvm#163453 (comment).
1 parent c6b27ad commit b0e24bd

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4030,16 +4030,13 @@ bool PhiNodeSetIterator::operator!=(const PhiNodeSetIterator &RHS) const {
40304030
/// if it is simplified.
40314031
class SimplificationTracker {
40324032
DenseMap<Value *, Value *> Storage;
4033-
const SimplifyQuery &SQ;
40344033
// Tracks newly created Phi nodes. The elements are iterated by insertion
40354034
// order.
40364035
PhiNodeSet AllPhiNodes;
40374036
// Tracks newly created Select nodes.
40384037
SmallPtrSet<SelectInst *, 32> AllSelectNodes;
40394038

40404039
public:
4041-
SimplificationTracker(const SimplifyQuery &sq) : SQ(sq) {}
4042-
40434040
Value *Get(Value *V) {
40444041
do {
40454042
auto SV = Storage.find(V);
@@ -4049,30 +4046,6 @@ class SimplificationTracker {
40494046
} while (true);
40504047
}
40514048

4052-
Value *Simplify(Value *Val) {
4053-
SmallVector<Value *, 32> WorkList;
4054-
SmallPtrSet<Value *, 32> Visited;
4055-
WorkList.push_back(Val);
4056-
while (!WorkList.empty()) {
4057-
auto *P = WorkList.pop_back_val();
4058-
if (!Visited.insert(P).second)
4059-
continue;
4060-
if (auto *PI = dyn_cast<Instruction>(P))
4061-
if (Value *V = simplifyInstruction(cast<Instruction>(PI), SQ)) {
4062-
for (auto *U : PI->users())
4063-
WorkList.push_back(cast<Value>(U));
4064-
Put(PI, V);
4065-
PI->replaceAllUsesWith(V);
4066-
if (auto *PHI = dyn_cast<PHINode>(PI))
4067-
AllPhiNodes.erase(PHI);
4068-
if (auto *Select = dyn_cast<SelectInst>(PI))
4069-
AllSelectNodes.erase(Select);
4070-
PI->eraseFromParent();
4071-
}
4072-
}
4073-
return Get(Val);
4074-
}
4075-
40764049
void Put(Value *From, Value *To) { Storage.insert({From, To}); }
40774050

40784051
void ReplacePhi(PHINode *From, PHINode *To) {
@@ -4133,8 +4106,7 @@ class AddressingModeCombiner {
41334106
/// Common Type for all different fields in addressing modes.
41344107
Type *CommonType = nullptr;
41354108

4136-
/// SimplifyQuery for simplifyInstruction utility.
4137-
const SimplifyQuery &SQ;
4109+
const DataLayout &DL;
41384110

41394111
/// Original Address.
41404112
Value *Original;
@@ -4143,8 +4115,8 @@ class AddressingModeCombiner {
41434115
Value *CommonValue = nullptr;
41444116

41454117
public:
4146-
AddressingModeCombiner(const SimplifyQuery &_SQ, Value *OriginalValue)
4147-
: SQ(_SQ), Original(OriginalValue) {}
4118+
AddressingModeCombiner(const DataLayout &DL, Value *OriginalValue)
4119+
: DL(DL), Original(OriginalValue) {}
41484120

41494121
~AddressingModeCombiner() { eraseCommonValueIfDead(); }
41504122

@@ -4256,7 +4228,7 @@ class AddressingModeCombiner {
42564228
// Keep track of keys where the value is null. We will need to replace it
42574229
// with constant null when we know the common type.
42584230
SmallVector<Value *, 2> NullValue;
4259-
Type *IntPtrTy = SQ.DL.getIntPtrType(AddrModes[0].OriginalValue->getType());
4231+
Type *IntPtrTy = DL.getIntPtrType(AddrModes[0].OriginalValue->getType());
42604232
for (auto &AM : AddrModes) {
42614233
Value *DV = AM.GetFieldAsValue(DifferentField, IntPtrTy);
42624234
if (DV) {
@@ -4306,7 +4278,7 @@ class AddressingModeCombiner {
43064278
// simplification is possible only if original phi/selects were not
43074279
// simplified yet.
43084280
// Using this mapping we can find the current value in AddrToBase.
4309-
SimplificationTracker ST(SQ);
4281+
SimplificationTracker ST;
43104282

43114283
// First step, DFS to create PHI nodes for all intermediate blocks.
43124284
// Also fill traverse order for the second step.
@@ -4465,7 +4437,6 @@ class AddressingModeCombiner {
44654437
PHI->addIncoming(ST.Get(Map[PV]), B);
44664438
}
44674439
}
4468-
Map[Current] = ST.Simplify(V);
44694440
}
44704441
}
44714442

@@ -5856,8 +5827,7 @@ bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
58565827
// the graph are compatible.
58575828
bool PhiOrSelectSeen = false;
58585829
SmallVector<Instruction *, 16> AddrModeInsts;
5859-
const SimplifyQuery SQ(*DL, TLInfo);
5860-
AddressingModeCombiner AddrModes(SQ, Addr);
5830+
AddressingModeCombiner AddrModes(*DL, Addr);
58615831
TypePromotionTransaction TPT(RemovedInsts);
58625832
TypePromotionTransaction::ConstRestorationPt LastKnownGood =
58635833
TPT.getRestorationPoint();

llvm/test/Transforms/CodeGenPrepare/X86/sink-addrmode-base.ll

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,3 +951,41 @@ fallthrough:
951951
%v = add i32 %v1, %v2
952952
ret i32 %v
953953
}
954+
955+
; Make sure we don't simplify an incomplete expression tree.
956+
define i8 @pr163453(ptr %p, i1 %cond) {
957+
; CHECK-LABEL: define i8 @pr163453(
958+
; CHECK-SAME: ptr [[P:%.*]], i1 [[COND:%.*]]) {
959+
; CHECK-NEXT: [[ENTRY:.*:]]
960+
; CHECK-NEXT: [[P_ADDR_0:%.*]] = getelementptr i8, ptr [[P]], i64 1
961+
; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[P]], align 1
962+
; CHECK-NEXT: [[INCDEC_PTR11:%.*]] = getelementptr i8, ptr [[P]], i64 2
963+
; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[COND]], ptr [[P_ADDR_0]], ptr [[INCDEC_PTR11]]
964+
; CHECK-NEXT: [[LOAD:%.*]] = load i8, ptr [[SPEC_SELECT]], align 1
965+
; CHECK-NEXT: ret i8 [[LOAD]]
966+
;
967+
entry:
968+
br label %for.cond
969+
970+
for.cond:
971+
%p.pn = phi ptr [ %p, %entry ], [ %p.addr.0, %for.inc ]
972+
%p.addr.0 = getelementptr i8, ptr %p.pn, i64 1
973+
br i1 false, label %exit, label %for.body
974+
975+
for.body:
976+
%1 = load i8, ptr %p.pn, align 1
977+
br i1 false, label %for.inc, label %if.else
978+
979+
if.else:
980+
%incdec.ptr11 = getelementptr i8, ptr %p.pn, i64 2
981+
%spec.select = select i1 %cond, ptr %p.addr.0, ptr %incdec.ptr11
982+
br label %exit
983+
984+
for.inc:
985+
br label %for.cond
986+
987+
exit:
988+
%p.addr.3 = phi ptr [ %spec.select, %if.else ], [ %p.addr.0, %for.cond ]
989+
%load = load i8, ptr %p.addr.3, align 1
990+
ret i8 %load
991+
}

0 commit comments

Comments
 (0)