@@ -215,18 +215,6 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInst(GetElementPtrInst &GEP) {
215
215
if (GEPChainInfoMap.contains (cast<GEPOperator>(&GEP)))
216
216
return false ;
217
217
218
- // Construct GEPInfo for this GEP
219
- GEPInfo Info;
220
-
221
- // Obtain the variable and constant byte offsets computed by this GEP
222
- const DataLayout &DL = GEP.getDataLayout ();
223
- unsigned BitWidth = DL.getIndexTypeSizeInBits (GEP.getType ());
224
- Info.ConstantOffset = {BitWidth, 0 };
225
- bool Success = GEP.collectOffset (DL, BitWidth, Info.VariableOffsets ,
226
- Info.ConstantOffset );
227
- (void )Success;
228
- assert (Success && " Failed to collect offsets for GEP" );
229
-
230
218
Value *PtrOperand = GEP.getPointerOperand ();
231
219
232
220
// Replace a GEP ConstantExpr pointer operand with a GEP instruction so that
@@ -243,7 +231,7 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInst(GetElementPtrInst &GEP) {
243
231
Builder.CreateGEP (GEP.getSourceElementType (), OldGEPI, Indices,
244
232
GEP.getName (), GEP.getNoWrapFlags ());
245
233
assert (isa<GetElementPtrInst>(NewGEP) &&
246
- " Expected newly-created GEP to not be a ConstantExpr " );
234
+ " Expected newly-created GEP to be an instruction " );
247
235
GetElementPtrInst *NewGEPI = cast<GetElementPtrInst>(NewGEP);
248
236
249
237
GEP.replaceAllUsesWith (NewGEPI);
@@ -253,6 +241,18 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInst(GetElementPtrInst &GEP) {
253
241
return true ;
254
242
}
255
243
244
+ // Construct GEPInfo for this GEP
245
+ GEPInfo Info;
246
+
247
+ // Obtain the variable and constant byte offsets computed by this GEP
248
+ const DataLayout &DL = GEP.getDataLayout ();
249
+ unsigned BitWidth = DL.getIndexTypeSizeInBits (GEP.getType ());
250
+ Info.ConstantOffset = {BitWidth, 0 };
251
+ bool Success = GEP.collectOffset (DL, BitWidth, Info.VariableOffsets ,
252
+ Info.ConstantOffset );
253
+ (void )Success;
254
+ assert (Success && " Failed to collect offsets for GEP" );
255
+
256
256
// If there is a parent GEP, inherit the root array type and pointer, and
257
257
// merge the byte offsets. Otherwise, this GEP is itself the root of a GEP
258
258
// chain and we need to deterine the root array type
@@ -270,15 +270,13 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInst(GetElementPtrInst &GEP) {
270
270
271
271
// We should try to determine the type of the root from the pointer rather
272
272
// than the GEP's source element type because this could be a scalar GEP
273
- // into a multidimensional array-typed pointer from an Alloca or Global
274
- // Variable.
273
+ // into an array-typed pointer from an Alloca or Global Variable.
275
274
Type *RootTy = GEP.getSourceElementType ();
276
275
if (auto *GlobalVar = dyn_cast<GlobalVariable>(PtrOperand)) {
277
- if (!GlobalMap.contains (GlobalVar))
278
- return false ;
279
- GlobalVariable *NewGlobal = GlobalMap[GlobalVar];
280
- Info.RootPointerOperand = NewGlobal;
281
- RootTy = NewGlobal->getValueType ();
276
+ if (GlobalMap.contains (GlobalVar))
277
+ GlobalVar = GlobalMap[GlobalVar];
278
+ Info.RootPointerOperand = GlobalVar;
279
+ RootTy = GlobalVar->getValueType ();
282
280
} else if (auto *Alloca = dyn_cast<AllocaInst>(PtrOperand)) {
283
281
RootTy = Alloca->getAllocatedType ();
284
282
}
@@ -341,7 +339,8 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInst(GetElementPtrInst &GEP) {
341
339
}
342
340
343
341
// This GEP is potentially dead at the end of the pass since it may not have
344
- // any users anymore after GEP chains have been collapsed.
342
+ // any users anymore after GEP chains have been collapsed. We retain store
343
+ // GEPInfo for GEPs down the chain to use to compute their indices.
345
344
GEPChainInfoMap.insert ({cast<GEPOperator>(&GEP), std::move (Info)});
346
345
PotentiallyDeadInstrs.emplace_back (&GEP);
347
346
return false ;
0 commit comments