@@ -142,7 +142,7 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
142142 switch (II->getOpcode ()) {
143143 case Instruction::Store: {
144144 auto *storeI = cast<StoreInst>(II);
145- if (storeI->getValueOperand () == use->get ()) {
145+ if (storeI->getValueOperand () == use->get () && v2vMap. count (storeI-> getPointerOperand ()) == 0 ) {
146146 SmallVector<Instruction *> origins;
147147 auto hasOrigins = Provenance::tryFindPointerOrigin (storeI->getPointerOperand (), origins);
148148
@@ -157,7 +157,8 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
157157 cast<ArrayType>(array->getAllocatedType ())->getNumElements ());
158158
159159 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 ());
161162 v2vMap[array] = newArray;
162163
163164 llvm::for_each (array->uses (), [&worklist](Use &U) { worklist.push_back (&U); });
@@ -180,12 +181,14 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
180181 SmallVector<Value *> indices (cast<GetElementPtrInst>(II)->indices ());
181182
182183 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 ());
184186 llvm::for_each (II->uses (), [&worklist](Use &U) { worklist.push_back (&U); });
185187 } break ;
186188 case Instruction::Load:
187189 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 ());
189192 llvm::for_each (II->uses (), [&worklist](Use &U) { worklist.push_back (&U); });
190193 break ;
191194 case Instruction::Select:
@@ -194,8 +197,8 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
194197 continue ;
195198
196199 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 ());
199202 llvm::for_each (II->uses (), [&worklist](Use &U) { worklist.push_back (&U); });
200203 break ;
201204 default :
@@ -208,6 +211,37 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
208211
209212 RemapFunction (F, v2vMap, RF_IgnoreMissingLocals | RF_ReuseAndMutateDistinctMDs);
210213
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+
211245 return true ;
212246}
213247
0 commit comments