@@ -1202,70 +1202,71 @@ void InferAddressSpacesImpl::performPointerReplacement(
1202
1202
return ;
1203
1203
}
1204
1204
1205
- if (isa<Instruction>(CurUser)) {
1206
- if (ICmpInst *Cmp = dyn_cast<ICmpInst>(CurUser)) {
1207
- // If we can infer that both pointers are in the same addrspace,
1208
- // transform e.g.
1209
- // %cmp = icmp eq float* %p, %q
1210
- // into
1211
- // %cmp = icmp eq float addrspace(3)* %new_p, %new_q
1212
-
1213
- unsigned NewAS = NewV->getType ()->getPointerAddressSpace ();
1214
- int SrcIdx = U.getOperandNo ();
1215
- int OtherIdx = (SrcIdx == 0 ) ? 1 : 0 ;
1216
- Value *OtherSrc = Cmp->getOperand (OtherIdx);
1217
-
1218
- if (Value *OtherNewV = ValueWithNewAddrSpace.lookup (OtherSrc)) {
1219
- if (OtherNewV->getType ()->getPointerAddressSpace () == NewAS) {
1220
- Cmp->setOperand (OtherIdx, OtherNewV);
1221
- Cmp->setOperand (SrcIdx, NewV);
1222
- return ;
1223
- }
1224
- }
1205
+ if (!isa<Instruction>(CurUser))
1206
+ return ;
1225
1207
1226
- // Even if the type mismatches, we can cast the constant.
1227
- if (auto *KOtherSrc = dyn_cast<Constant>(OtherSrc)) {
1228
- if (isSafeToCastConstAddrSpace (KOtherSrc, NewAS)) {
1229
- Cmp->setOperand (SrcIdx, NewV);
1230
- Cmp->setOperand (OtherIdx, ConstantExpr::getAddrSpaceCast (
1231
- KOtherSrc, NewV->getType ()));
1232
- return ;
1233
- }
1208
+ if (ICmpInst *Cmp = dyn_cast<ICmpInst>(CurUser)) {
1209
+ // If we can infer that both pointers are in the same addrspace,
1210
+ // transform e.g.
1211
+ // %cmp = icmp eq float* %p, %q
1212
+ // into
1213
+ // %cmp = icmp eq float addrspace(3)* %new_p, %new_q
1214
+
1215
+ unsigned NewAS = NewV->getType ()->getPointerAddressSpace ();
1216
+ int SrcIdx = U.getOperandNo ();
1217
+ int OtherIdx = (SrcIdx == 0 ) ? 1 : 0 ;
1218
+ Value *OtherSrc = Cmp->getOperand (OtherIdx);
1219
+
1220
+ if (Value *OtherNewV = ValueWithNewAddrSpace.lookup (OtherSrc)) {
1221
+ if (OtherNewV->getType ()->getPointerAddressSpace () == NewAS) {
1222
+ Cmp->setOperand (OtherIdx, OtherNewV);
1223
+ Cmp->setOperand (SrcIdx, NewV);
1224
+ return ;
1234
1225
}
1235
1226
}
1236
1227
1237
- if (AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(CurUser)) {
1238
- unsigned NewAS = NewV->getType ()->getPointerAddressSpace ();
1239
- if (ASC->getDestAddressSpace () == NewAS) {
1240
- ASC->replaceAllUsesWith (NewV);
1241
- DeadInstructions.push_back (ASC);
1228
+ // Even if the type mismatches, we can cast the constant.
1229
+ if (auto *KOtherSrc = dyn_cast<Constant>(OtherSrc)) {
1230
+ if (isSafeToCastConstAddrSpace (KOtherSrc, NewAS)) {
1231
+ Cmp->setOperand (SrcIdx, NewV);
1232
+ Cmp->setOperand (OtherIdx, ConstantExpr::getAddrSpaceCast (
1233
+ KOtherSrc, NewV->getType ()));
1242
1234
return ;
1243
1235
}
1244
1236
}
1237
+ }
1245
1238
1246
- // Otherwise, replaces the use with flat(NewV).
1247
- if (Instruction *VInst = dyn_cast<Instruction>(V)) {
1248
- // Don't create a copy of the original addrspacecast.
1249
- if (U == V && isa<AddrSpaceCastInst>(V))
1250
- return ;
1251
-
1252
- // Insert the addrspacecast after NewV.
1253
- BasicBlock::iterator InsertPos;
1254
- if (Instruction *NewVInst = dyn_cast<Instruction>(NewV))
1255
- InsertPos = std::next (NewVInst->getIterator ());
1256
- else
1257
- InsertPos = std::next (VInst->getIterator ());
1258
-
1259
- while (isa<PHINode>(InsertPos))
1260
- ++InsertPos;
1261
- // This instruction may contain multiple uses of V, update them all.
1262
- CurUser->replaceUsesOfWith (
1263
- V, new AddrSpaceCastInst (NewV, V->getType (), " " , InsertPos));
1264
- } else {
1265
- CurUser->replaceUsesOfWith (V, ConstantExpr::getAddrSpaceCast (
1266
- cast<Constant>(NewV), V->getType ()));
1239
+ if (AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(CurUser)) {
1240
+ unsigned NewAS = NewV->getType ()->getPointerAddressSpace ();
1241
+ if (ASC->getDestAddressSpace () == NewAS) {
1242
+ ASC->replaceAllUsesWith (NewV);
1243
+ DeadInstructions.push_back (ASC);
1244
+ return ;
1267
1245
}
1268
1246
}
1247
+
1248
+ // Otherwise, replaces the use with flat(NewV).
1249
+ if (Instruction *VInst = dyn_cast<Instruction>(V)) {
1250
+ // Don't create a copy of the original addrspacecast.
1251
+ if (U == V && isa<AddrSpaceCastInst>(V))
1252
+ return ;
1253
+
1254
+ // Insert the addrspacecast after NewV.
1255
+ BasicBlock::iterator InsertPos;
1256
+ if (Instruction *NewVInst = dyn_cast<Instruction>(NewV))
1257
+ InsertPos = std::next (NewVInst->getIterator ());
1258
+ else
1259
+ InsertPos = std::next (VInst->getIterator ());
1260
+
1261
+ while (isa<PHINode>(InsertPos))
1262
+ ++InsertPos;
1263
+ // This instruction may contain multiple uses of V, update them all.
1264
+ CurUser->replaceUsesOfWith (
1265
+ V, new AddrSpaceCastInst (NewV, V->getType (), " " , InsertPos));
1266
+ } else {
1267
+ CurUser->replaceUsesOfWith (V, ConstantExpr::getAddrSpaceCast (
1268
+ cast<Constant>(NewV), V->getType ()));
1269
+ }
1269
1270
}
1270
1271
1271
1272
bool InferAddressSpacesImpl::rewriteWithNewAddressSpaces (
0 commit comments