Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 07e9d11

Browse files
committed
be more careful when fixing function pointers in SimplifyStructRegSignatures, we need the types to always be consistent, so llvm assertions are not triggered
1 parent a7975f1 commit 07e9d11

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

lib/Target/JSBackend/NaCl/SimplifyStructRegSignatures.cpp

+19-3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ class SimplifyStructRegSignatures : public ModulePass {
128128
SetVector<InvokeInst *> InvokesToPatch;
129129
DenseMap<Function *, Function *> FunctionMap;
130130

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+
131138
bool
132139
simplifyFunction(LLVMContext &Ctx, Function *OldFunc);
133140

@@ -355,7 +362,7 @@ TCall *SimplifyStructRegSignatures::fixCallTargetAndArguments(
355362
Value *OldArg = OldArgUse;
356363
Type *OldArgType = OldArg->getType();
357364
unsigned NewArgPos = OldArgUse.getOperandNo() + argOffset;
358-
Type *NewArgType = NewType->getFunctionParamType(NewArgPos);
365+
Type *NewArgType = NewArgPos < VarargMark ? NewType->getFunctionParamType(NewArgPos) : nullptr;
359366

360367
if (OldArgType != NewArgType && OldArgType->isAggregateType()) {
361368
if (NewArgPos >= VarargMark) {
@@ -369,6 +376,13 @@ TCall *SimplifyStructRegSignatures::fixCallTargetAndArguments(
369376
Builder.CreateStore(OldArg, Alloca);
370377
ByRefPlaces.insert(NewArgPos);
371378
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);
372386
} else {
373387
NewArgs.push_back(OldArg);
374388
}
@@ -514,10 +528,12 @@ bool SimplifyStructRegSignatures::runOnModule(Module &M) {
514528
}
515529

516530
// 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;
518534
Function *New = FunctionMap[Old];
519535
assert(New);
520-
Old->replaceAllUsesWith(New);
536+
Temp->replaceAllUsesWith(New);
521537
}
522538

523539
// Delete leftover functions - the ones with old signatures.

0 commit comments

Comments
 (0)