Skip to content

Commit 15552a1

Browse files
committed
Make peek-args more robust against logical oversights
1 parent fb5b8f9 commit 15552a1

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

backends/asm/optimize_ir.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ static bool FuncUsesArgEx(Operand *func, Operand *arg, bool actually)
985985
if ((/*func->kind == IMM_COG_LABEL ||*/ func->kind == IMM_HUB_LABEL) && (actually || funcObj->is_leaf || FuncData(funcObj)->effectivelyLeaf)) {
986986
if (arg->kind != REG_ARG) return true; // subreg or smth
987987
if (arg->val < funcObj->numparams) return true; // Arg used;
988-
if (!actually && arg->val < FuncData(funcObj)->maxInlineArg) return true; // Arg clobbered
988+
if (!actually && arg->val < FuncData(funcObj)->maxClobberArg) return true; // Arg clobbered
989989
if ( ((Function *)func->val)->numparams < 0 ) return true; // varargs
990990
return false; // Arg not used
991991
}
@@ -5567,6 +5567,18 @@ ShouldExpandPureFunction(IR *ir) {
55675567
return true;
55685568
}
55695569

5570+
static int getArgClobberVal(Operand *op) {
5571+
int offset = 0;
5572+
if (!op) return 0;
5573+
if (op->kind == REG_SUBREG) {
5574+
offset = op->val;
5575+
op = (Operand *)op->name;
5576+
}
5577+
if (op->kind != REG_ARG) return 0;
5578+
5579+
return op->val + offset + 1;
5580+
}
5581+
55705582
//
55715583
// expand function calls inline if appropriate
55725584
// returns 1 if anything was expanded
@@ -5581,14 +5593,16 @@ ExpandInlines(IRList *irl)
55815593
ir = irl->head;
55825594
while (ir) {
55835595
ir_next = ir->next;
5596+
updateMax(&FuncData(curfunc)->maxClobberArg, getArgClobberVal(ir->dst));
5597+
updateMax(&FuncData(curfunc)->maxClobberArg, getArgClobberVal(ir->src));
55845598
if (ir->opc == OPC_CALL) {
55855599
f = (Function *)ir->aux;
55865600
if (f && ((FuncData(f)->inliningFlags & (ASM_INLINE_SMALL_FLAG|ASM_INLINE_SINGLE_FLAG)) || ShouldExpandPureFunction(ir))) {
55875601
ReplaceIRWithInline(irl, ir, f);
55885602
FuncData(f)->actual_callsites--;
55895603
FuncData(f)->got_inlined = true;
5590-
updateMax(&FuncData(curfunc)->maxInlineArg,f->numparams);
5591-
updateMax(&FuncData(curfunc)->maxInlineArg,FuncData(f)->maxInlineArg);
5604+
updateMax(&FuncData(curfunc)->maxClobberArg,f->numparams);
5605+
updateMax(&FuncData(curfunc)->maxClobberArg,FuncData(f)->maxClobberArg);
55925606
if (!f->is_leaf && !FuncData(f)->effectivelyLeaf) non_inline_calls++; // Non-leaf inline may contain call
55935607
change = 1;
55945608
} else {

backends/asm/outasm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5266,7 +5266,7 @@ static void CompileStatement(IRList *irl, IRList *cold_irl, AST *ast)
52665266
EmitMove(irl, GetArgReg(1), op, ast);
52675267
EmitMove(irl, GetArgReg(2), NewImmediate(ast->d.ival), ast);
52685268
EmitOp1(irl, OPC_CALL, longjmpfunc);
5269-
updateMax(&FuncData(curfunc)->maxInlineArg,3);
5269+
updateMax(&FuncData(curfunc)->maxClobberArg,3);
52705270
break;
52715271
case AST_LABEL:
52725272
EmitDebugComment(irl, ast);

backends/asm/outasm.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ typedef struct ir_bedata {
135135

136136
/* set after inlining if the function has no external calls left */
137137
bool effectivelyLeaf;
138-
/* Highest arg count of all inlined functions */
139-
int maxInlineArg;
138+
/* Highest arg count clobbered */
139+
/* (beware of off-by-one errors) */
140+
int maxClobberArg;
140141

141142
/* type of calling convention */
142143
CallConvention convention;

0 commit comments

Comments
 (0)