Skip to content

Commit 18ac6e6

Browse files
authored
Merge pull request #26755 from JuliaLang/kf/liftselect
Also lift SelectInst addrspaces
2 parents bc219ed + 14e9c42 commit 18ac6e6

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

src/llvm-propagate-addrspaces.cpp

+31-16
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,21 @@ Value *PropagateJuliaAddrspaces::LiftPointer(Value *V, Type *LocTy, Instruction
132132
Stack.push_back(Phi);
133133
LocalVisited.insert(Phi);
134134
break;
135+
} else if (auto *Select = dyn_cast<SelectInst>(CurrentV)) {
136+
if (LiftingMap.count(Select)) {
137+
break;
138+
} else if (Visited.count(Select)) {
139+
return nullptr;
140+
}
141+
// Push one of the branches onto the worklist, continue with the other one
142+
// directly
143+
Worklist.push_back(Select->getOperand(2));
144+
Stack.push_back(Select);
145+
LocalVisited.insert(Select);
146+
CurrentV = Select->getOperand(1);
147+
} else if (isa<ConstantPointerNull>(CurrentV)) {
148+
// It's always legal to lift null pointers into any address space
149+
break;
135150
} else {
136151
// Ok, we've reached a leaf - check if it is eligible for lifting
137152
if (!CurrentV->getType()->isPointerTy() ||
@@ -154,25 +169,19 @@ Value *PropagateJuliaAddrspaces::LiftPointer(Value *V, Type *LocTy, Instruction
154169
for (Value *V : Stack) {
155170
if (LiftingMap.count(V))
156171
continue;
157-
if (auto *GEP = dyn_cast<GetElementPtrInst>(V)) {
158-
auto *NewGEP = cast<GetElementPtrInst>(GEP->clone());
159-
ToInsert.push_back(std::make_pair(NewGEP, GEP));
160-
Type *NewRetTy = cast<PointerType>(GEP->getType())->getElementType()->getPointerTo(0);
161-
NewGEP->mutateType(NewRetTy);
162-
LiftingMap[GEP] = NewGEP;
163-
ToRevisit.push_back(NewGEP);
164-
} else if (auto *Phi = dyn_cast<PHINode>(V)) {
165-
auto *NewPhi = cast<PHINode>(Phi->clone());
166-
ToInsert.push_back(std::make_pair(NewPhi, Phi));
167-
Type *NewRetTy = cast<PointerType>(Phi->getType())->getElementType()->getPointerTo(0);
168-
NewPhi->mutateType(NewRetTy);
169-
LiftingMap[Phi] = NewPhi;
170-
ToRevisit.push_back(NewPhi);
172+
if (isa<GetElementPtrInst>(V) || isa<PHINode>(V) || isa<SelectInst>(V)) {
173+
Instruction *InstV = cast<Instruction>(V);
174+
Instruction *NewV = InstV->clone();
175+
ToInsert.push_back(std::make_pair(NewV, InstV));
176+
Type *NewRetTy = cast<PointerType>(InstV->getType())->getElementType()->getPointerTo(0);
177+
NewV->mutateType(NewRetTy);
178+
LiftingMap[InstV] = NewV;
179+
ToRevisit.push_back(NewV);
171180
}
172181
}
173182

174-
auto CollapseCastsAndLift = [&](Value *CurrentV, Instruction *InsertPt) {
175-
Type *TargetType = cast<PointerType>(CurrentV->getType())->getElementType()->getPointerTo(0);
183+
auto CollapseCastsAndLift = [&](Value *CurrentV, Instruction *InsertPt) -> Value * {
184+
PointerType *TargetType = cast<PointerType>(CurrentV->getType())->getElementType()->getPointerTo(0);
176185
while (!LiftingMap.count(CurrentV)) {
177186
if (isa<BitCastInst>(CurrentV))
178187
CurrentV = cast<BitCastInst>(CurrentV)->getOperand(0);
@@ -181,6 +190,9 @@ Value *PropagateJuliaAddrspaces::LiftPointer(Value *V, Type *LocTy, Instruction
181190
else
182191
break;
183192
}
193+
if (isa<ConstantPointerNull>(CurrentV)) {
194+
return ConstantPointerNull::get(TargetType);
195+
}
184196
if (LiftingMap.count(CurrentV))
185197
CurrentV = LiftingMap[CurrentV];
186198
if (CurrentV->getType() != TargetType) {
@@ -202,6 +214,9 @@ Value *PropagateJuliaAddrspaces::LiftPointer(Value *V, Type *LocTy, Instruction
202214
NewPhi->setIncomingValue(i, CollapseCastsAndLift(NewPhi->getIncomingValue(i),
203215
NewPhi->getIncomingBlock(i)->getTerminator()));
204216
}
217+
} else if (SelectInst *NewSelect = dyn_cast<SelectInst>(V)) {
218+
NewSelect->setOperand(1, CollapseCastsAndLift(NewSelect->getOperand(1), NewSelect));
219+
NewSelect->setOperand(2, CollapseCastsAndLift(NewSelect->getOperand(2), NewSelect));
205220
} else {
206221
assert(false && "Shouldn't have reached here");
207222
}

test/llvmpasses/propagate-addrspace.ll

+22
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,25 @@ B:
3636
%load = load i64, i64 addrspace(11)* %phi
3737
ret i64 %load
3838
}
39+
40+
41+
define i64 @select(i1 %cond) {
42+
; CHECK-LABEL: @select
43+
; CHECK-NOT: addrspace(11)
44+
top:
45+
%stack1 = alloca i64
46+
%stack2 = alloca i64
47+
%stack1_casted = addrspacecast i64 *%stack1 to i64 addrspace(11)*
48+
%stack2_casted = addrspacecast i64 *%stack2 to i64 addrspace(11)*
49+
%select = select i1 %cond, i64 addrspace(11)* %stack1_casted, i64 addrspace(11)* %stack2_casted
50+
%load = load i64, i64 addrspace(11)* %select
51+
ret i64 %load
52+
}
53+
54+
define i64 @nullptr() {
55+
; CHECK-LABEL: @nullptr
56+
; CHECK-NOT: addrspace(11)
57+
%casted = addrspacecast i64 *null to i64 addrspace(11)*
58+
%load = load i64, i64 addrspace(11)* %casted
59+
ret i64 %load
60+
}

0 commit comments

Comments
 (0)