@@ -128,6 +128,13 @@ class SimplifyStructRegSignatures : public ModulePass {
128
128
SetVector<InvokeInst *> InvokesToPatch;
129
129
DenseMap<Function *, Function *> FunctionMap;
130
130
131
+ struct FunctionAddressing {
132
+ Value *Temp;
133
+ Function *Old;
134
+ FunctionAddressing (Value *Temp, Function *Old) : Temp(Temp), Old(Old) {}
135
+ };
136
+ std::vector<FunctionAddressing> FunctionAddressings;
137
+
131
138
bool
132
139
simplifyFunction (LLVMContext &Ctx, Function *OldFunc);
133
140
@@ -355,7 +362,7 @@ TCall *SimplifyStructRegSignatures::fixCallTargetAndArguments(
355
362
Value *OldArg = OldArgUse;
356
363
Type *OldArgType = OldArg->getType ();
357
364
unsigned NewArgPos = OldArgUse.getOperandNo () + argOffset;
358
- Type *NewArgType = NewType->getFunctionParamType (NewArgPos);
365
+ Type *NewArgType = NewArgPos < VarargMark ? NewType->getFunctionParamType (NewArgPos) : nullptr ;
359
366
360
367
if (OldArgType != NewArgType && OldArgType->isAggregateType ()) {
361
368
if (NewArgPos >= VarargMark) {
@@ -369,6 +376,13 @@ TCall *SimplifyStructRegSignatures::fixCallTargetAndArguments(
369
376
Builder.CreateStore (OldArg, Alloca);
370
377
ByRefPlaces.insert (NewArgPos);
371
378
NewArgs.push_back (Alloca);
379
+ } else if (NewArgType && OldArgType != NewArgType && isa<Function>(OldArg)) {
380
+ // If a function pointer has a changed type due to struct reg changes, it will still have
381
+ // the wrong type here, since we may have not changed that method yet. We'll fix it up
382
+ // later, and meanwhile place an undef of the right type in that slot.
383
+ Value *Temp = UndefValue::get (NewArgType);
384
+ FunctionAddressings.emplace_back (Temp, cast<Function>(OldArg));
385
+ NewArgs.push_back (Temp);
372
386
} else {
373
387
NewArgs.push_back (OldArg);
374
388
}
@@ -514,10 +528,12 @@ bool SimplifyStructRegSignatures::runOnModule(Module &M) {
514
528
}
515
529
516
530
// Update taking of a function's address
517
- for (auto &Old : FunctionsToDelete) {
531
+ for (auto &Addressing : FunctionAddressings) {
532
+ Value *Temp = Addressing.Temp ;
533
+ Function *Old = Addressing.Old ;
518
534
Function *New = FunctionMap[Old];
519
535
assert (New);
520
- Old ->replaceAllUsesWith (New);
536
+ Temp ->replaceAllUsesWith (New);
521
537
}
522
538
523
539
// Delete leftover functions - the ones with old signatures.
0 commit comments