【问题标题】:How to implement copy propagation? [Design]如何实现复制传播? [设计]
【发布时间】:2022-01-11 01:01:22
【问题描述】:

在编译器理论中,copy propagation 是用它们的值替换直接赋值目标的出现的过程。直接赋值是 x = y 形式的指令,它只是将 y 的值赋给 x。

y = x
z = 3 + y

复制传播会产生:

z = 3 + x

在 wiki 中,复制传播是通过使用 use-definition chains 实现的。

下面的代码(我的最佳尝试)通过创建 use-def 链并在使用变量的任何位置替换变量的值来实现复制传播(即使该变量在程序的该点不存在)。在第二次运行中,它评估所有常量(常量折叠)。

char *str_replace(char *orig, char *rep, char *with) 归属于https://stackoverflow.com/a/779960/10603510

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>

char *str_replace(char *orig, char *rep, char *with) {
    char *result; // the return string
    char *ins;    // the next insert point
    char *tmp;    // varies
    int len_rep;  // length of rep (the string to remove)
    int len_with; // length of with (the string to replace rep with)
    int len_front; // distance between rep and end of last rep
    int count;    // number of replacements

    // sanity checks and initialization
    if (!orig || !rep)
        return NULL;
    len_rep = strlen(rep);
    if (len_rep == 0)
        return NULL; // empty rep causes infinite loop during count
    if (!with)
        with = "";
    len_with = strlen(with);

    // count the number of replacements needed
    ins = orig;
    for (count = 0; (tmp = strstr(ins, rep)); ++count) {
        ins = tmp + len_rep;
    }

    tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);

    if (!result)
        return NULL;

    // first time through the loop, all the variable are set correctly
    // from here on,
    //    tmp points to the end of the result string
    //    ins points to the next occurrence of rep in orig
    //    orig points to the remainder of orig after "end of rep"
    while (count--) {
        ins = strstr(orig, rep);
        len_front = ins - orig;
        tmp = strncpy(tmp, orig, len_front) + len_front;
        tmp = strcpy(tmp, with) + len_with;
        orig += len_front + len_rep; // move to next "end of rep"
    }
    strcpy(tmp, orig);
    return result;
}

bool is_operator(char c) {
    return c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')';
}

int calculate(int a, int b, char op)
{
    switch (op)
    {
    case '+': return a + b;
    case '-': return a - b;
    case '*': return a * b;
    case '/': return a / b;
    case '^': return pow(a, b);
    }
    return -1;
}

bool is_operand(char c)
{
    return strchr("0123456789", c);
}

int priority(char c)
{
    switch (c)
    {
    case '+':
    case '-':
        return 0;
    case '*':
    case '/':
        return 1;
    case '^':
        return 2;
    }
    return -1;
}

int evaluate(char *expression)
{
    int op1;
    int op2;
    int top = 0;
    int ops = 0;
    int operand_stack[50];
    char operators[50];
    char *p = expression;
    
    for (; *p; p++)
    {
        if (*p == ' ')
        {
            continue;
        }
        else if (isalpha(*p))
        {
            return -1;
        }
        else if (is_operand(*p))
        {
            operand_stack[++top] = strtol((char*)p, (char**)&p, 10);
            p--;
        }
        else if (is_operator(*p)) 
        {
            while (ops) {
                if (priority(*p) < priority(operators[ops])) {
                    op2 = operand_stack[top--];
                    op1 = operand_stack[top--];
                    operand_stack[++top] = calculate(op1, op2, operators[ops--]);
                }
                else {
                    break;
                }
            }
            operators[++ops] = *p;

        }
    }
    
    while (ops) {
        op2 = operand_stack[top--];
        op1 = operand_stack[top--];
        operand_stack[++top] = calculate(op1, op2, operators[ops--]);
    }
    return operand_stack[top];
}

char expressions[50][50];
int n;

void constant_folding() {
    for (int i = 0; i < n; i++) {
        char *p = strchr(expressions[i], (int)'=');
        if (p) {
            char integer[20];
            int a = evaluate(p+1);
        
            if (a != -1) {
                sprintf(integer, "%d", a);
                strcpy(expressions[i], str_replace(expressions[i], p + 1, integer));
            }
        }
    }
}

// line starts from 0
typedef struct use_def {
    int line;
    int use_count;
    char var[20];
    char replacement[20];
    int lines_used[20];
} use_def;
use_def use_defs[5];
int use_def_count = 0;

void constant_propogation() {
    for (int i = 0; i < use_def_count; i++) {
        use_def *a = &use_defs[i];
        for (int j = 0; j < a->use_count; j++) {
            strcpy(expressions[a->lines_used[j]], str_replace(expressions[a->lines_used[j]], a->var, a->replacement));
        }
    }
}

int main()
{

    printf("\n\nEnter the number of expressions : ");
    scanf("%d", &n);

    for(int i=0; i<n;i++)
    {
        scanf(" %[^\n]", expressions[i]);
    }

    for (int i = 0; i < n; i++) 
    {
        use_def *a = use_defs + i;
        a->line = i;
        char buff[20];
        strcpy(buff, expressions[i]);
        strcpy(a->var, strtok(buff, "="));
        if (a->var) {
            strcpy(a->replacement, strtok(NULL, ""));
            for (int j = i + 1; j < n ; j++) {
                if (strstr(expressions[j], a->var)) {
                    a->lines_used[a->use_count++] = j;
                }
            }
            use_def_count++;
        }
    }
    
    constant_propogation();
    constant_folding();
    
    printf("\nCode after propagation: \n");
    for(int i=0;i<n;i++) {
        printf("%s\n", expressions[i]);
    }
    return 0;

}

但是我的算法不适用于所有基本测试用例
(请忽略硬编码的值,我无法继续我的错误代码设计)。

我想要关于如何真正实现复制传播的指导,
或许更详细地解释全局公共子表达式消除 (GCSE) 和 CProp 的算法如何工作。

【问题讨论】:

  • 我投票结束这个问题,因为它对 StackOverflow 来说是广泛的。关于编译器的书籍通常应该回答这样的问题(但这不在 StackOverflow 上)。如果您有一些(更多)具体问题,请更新问题。
  • 这里有什么问题?
  • @vish4071: 问我自己...问题是“如何实现复制传播”,编译器优化的主题,它非常广泛,因为有钝复制的缺点。在 gcc 中,您可能想查看各种形式的选项 Global Common Subexpression Elimination (GCSE) 和 CProp。根据上面的实现:你有很多硬编码的值,比如变量的长度(20)和表达式的数量(50),它们没有被检查......这很危险。
  • @GraceMathew,你说你在上面的代码中实现了复制传播,有什么问题吗?如果没有,那么您不是已经回答了自己的问题吗?我认为您可能没有得到任何回应,因为我们无法弄清楚这里的问题是什么,和/或您在答案中寻找什么。
  • @RBarryYoung 我曾尝试实现复制传播,但我的代码并未涵盖所有测试用例,而且我的代码设计很幼稚。我正在寻找一种更健壮的设计,一种编译器所做工作的简单版本,以理解和欣赏编译器所做的事情。

标签: c string algorithm optimization compiler-construction


【解决方案1】:

缓冲区溢出可能性

可能会或可能不会解释 OP 的复制传播问题,但肯定是代码弱点。

代码表现出未定义的行为应该输入超过缓冲区的容量。

scanf(" %[^\n]", ...gets() 差。

    char expressions[50][50];

    // scanf(" %[^\n]", expressions[i]);

    char buf[sizeof expressions[i] * 2];
    if (fgets(buf, sizeof buf, stdin) == NULL) {
      fprintf(stderr, "No input\n");
      exit(-1);
    }
    size_t len = strlen(buf);
    if (len > 0 && buf[len-1] == '\n') {
      buf[--len] = '\0';
    }
    if (len >= sizeof expressions[i]) {
      fprintf(stderr, "Input too long %zu.\n", len);
      exit(-1);
    }
    strcpy(expressions[i], buf);

稍后

    char buff[20];

    // add check
    if (strlen(expressions[i] >= sizeof buff) {
      fprintf(stderr, "Expression <%s> too long.\n", expressions[i]);
      exit(-1);
    }

    strcpy(buff, expressions[i]);

也许还有其他有风险的缓冲区副本。


轻微

建议longstrtol() 一起使用。

// int operand_stack[50];
long operand_stack[50];

注意char operators[50]; ... operators[++ops] 确实索引为 50。

【讨论】:

    猜你喜欢
    • 2020-11-30
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 2011-07-02
    相关资源
    最近更新 更多