@@ -132,6 +132,21 @@ Value *PropagateJuliaAddrspaces::LiftPointer(Value *V, Type *LocTy, Instruction
132
132
Stack.push_back (Phi);
133
133
LocalVisited.insert (Phi);
134
134
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 ;
135
150
} else {
136
151
// Ok, we've reached a leaf - check if it is eligible for lifting
137
152
if (!CurrentV->getType ()->isPointerTy () ||
@@ -154,25 +169,19 @@ Value *PropagateJuliaAddrspaces::LiftPointer(Value *V, Type *LocTy, Instruction
154
169
for (Value *V : Stack) {
155
170
if (LiftingMap.count (V))
156
171
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);
171
180
}
172
181
}
173
182
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 );
176
185
while (!LiftingMap.count (CurrentV)) {
177
186
if (isa<BitCastInst>(CurrentV))
178
187
CurrentV = cast<BitCastInst>(CurrentV)->getOperand (0 );
@@ -181,6 +190,9 @@ Value *PropagateJuliaAddrspaces::LiftPointer(Value *V, Type *LocTy, Instruction
181
190
else
182
191
break ;
183
192
}
193
+ if (isa<ConstantPointerNull>(CurrentV)) {
194
+ return ConstantPointerNull::get (TargetType);
195
+ }
184
196
if (LiftingMap.count (CurrentV))
185
197
CurrentV = LiftingMap[CurrentV];
186
198
if (CurrentV->getType () != TargetType) {
@@ -202,6 +214,9 @@ Value *PropagateJuliaAddrspaces::LiftPointer(Value *V, Type *LocTy, Instruction
202
214
NewPhi->setIncomingValue (i, CollapseCastsAndLift (NewPhi->getIncomingValue (i),
203
215
NewPhi->getIncomingBlock (i)->getTerminator ()));
204
216
}
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));
205
220
} else {
206
221
assert (false && " Shouldn't have reached here" );
207
222
}
0 commit comments