@@ -142,7 +142,7 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
142
142
switch (II->getOpcode ()) {
143
143
case Instruction::Store: {
144
144
auto *storeI = cast<StoreInst>(II);
145
- if (storeI->getValueOperand () == use->get ()) {
145
+ if (storeI->getValueOperand () == use->get () && v2vMap. count (storeI-> getPointerOperand ()) == 0 ) {
146
146
SmallVector<Instruction *> origins;
147
147
auto hasOrigins = Provenance::tryFindPointerOrigin (storeI->getPointerOperand (), origins);
148
148
@@ -157,7 +157,8 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
157
157
cast<ArrayType>(array->getAllocatedType ())->getNumElements ());
158
158
159
159
IRB.SetInsertPoint (array);
160
- auto *newArray = IRB.CreateAlloca (ty, nullptr , array->getName (), array->getAddressSpace ());
160
+ auto *newArray = IRB.CreateAlloca (ty, nullptr , VALUE_NAME (" RQObjectArrayAlloca_" ) + array->getName (),
161
+ array->getAddressSpace ());
161
162
v2vMap[array] = newArray;
162
163
163
164
llvm::for_each (array->uses (), [&worklist](Use &U) { worklist.push_back (&U); });
@@ -180,12 +181,14 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
180
181
SmallVector<Value *> indices (cast<GetElementPtrInst>(II)->indices ());
181
182
182
183
IRB.SetInsertPoint (II);
183
- v2vMap[II] = IRB.CreateInBoundsGEP (array->getAllocatedType (), array, indices, II->getName ());
184
+ v2vMap[II] = IRB.CreateInBoundsGEP (array->getAllocatedType (), array, indices,
185
+ VALUE_NAME (" RQObjectGEP_" ) + II->getName ());
184
186
llvm::for_each (II->uses (), [&worklist](Use &U) { worklist.push_back (&U); });
185
187
} break ;
186
188
case Instruction::Load:
187
189
IRB.SetInsertPoint (II);
188
- v2vMap[II] = IRB.CreateLoad (m_RQObjectType->getPointerTo (), v2vMap[II->getOperand (0 )], II->getName ());
190
+ v2vMap[II] = IRB.CreateLoad (m_RQObjectType->getPointerTo (), v2vMap[II->getOperand (0 )],
191
+ VALUE_NAME (" RQObjectLoad_" ) + II->getName ());
189
192
llvm::for_each (II->uses (), [&worklist](Use &U) { worklist.push_back (&U); });
190
193
break ;
191
194
case Instruction::Select:
@@ -194,8 +197,8 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
194
197
continue ;
195
198
196
199
IRB.SetInsertPoint (II);
197
- v2vMap[II] =
198
- IRB. CreateSelect (II-> getOperand ( 0 ), v2vMap[II-> getOperand ( 1 )], v2vMap[II-> getOperand ( 2 )], II->getName ());
200
+ v2vMap[II] = IRB. CreateSelect (II-> getOperand ( 0 ), v2vMap[II-> getOperand ( 1 )], v2vMap[II-> getOperand ( 2 )],
201
+ VALUE_NAME ( " RQObjectSelect_ " ) + II->getName ());
199
202
llvm::for_each (II->uses (), [&worklist](Use &U) { worklist.push_back (&U); });
200
203
break ;
201
204
default :
@@ -208,6 +211,37 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
208
211
209
212
RemapFunction (F, v2vMap, RF_IgnoreMissingLocals | RF_ReuseAndMutateDistinctMDs);
210
213
214
+ DenseSet<Instruction *> canBeDeleted;
215
+
216
+ // try to remove as many of unused instructions as possible
217
+ for (auto [from, _] : v2vMap) {
218
+
219
+ if (auto *I = dyn_cast<Instruction>(const_cast <Value *>(from))) {
220
+
221
+ if (I->getType ()->isVoidTy ())
222
+ continue ;
223
+
224
+ canBeDeleted.insert (I);
225
+ }
226
+ }
227
+
228
+ while (!canBeDeleted.empty ()) {
229
+
230
+ bool changed = false ;
231
+ for (auto *V : canBeDeleted) {
232
+
233
+ if (V->use_empty ()) {
234
+
235
+ canBeDeleted.erase (V);
236
+ cast<Instruction>(V)->eraseFromParent ();
237
+ changed = true ;
238
+ break ;
239
+ }
240
+ }
241
+ if (!changed) // no progress has been done
242
+ break ;
243
+ }
244
+
211
245
return true ;
212
246
}
213
247
0 commit comments