LLVM的-globalopt传递不会优化全局变量

时间:2016-03-12 08:35:45

标签: llvm llvm-ir

我有以下LLVM IR程序:

@test1 = global i32 3, align 4
@test2 = common global i32 0, align 4

; Function Attrs: nounwind uwtable
define i32 @main() #0 {
  store i32 4, i32* @test2, align 4
  %1 = load i32* @test1, align 4
  ret i32 %1
}

当我使用opt(版本3.3)与-globalopt传递一起运行时,bitcode保持不变。但是,我希望将@test1标记为constant并删除@test2,因为该程序仅存储到@test2

我是否必须先运行一些分析过程的选择,或者为什么传球没有按照我的预期执行?

1 个答案:

答案 0 :(得分:4)

来自linkage section of the langref(强调我的):

  

#include <stdio.h> struct date { int year; int month; int day; }; int later (struct date *one, struct date *two); void prn_result (struct date a, struct date b, char op); /* main is a function of type 'int' and returns a value */ int main (void) { struct date dates[2] = {{0}}; /* initialize variables */ int i = 0, res = 0; for (i = 0; i < 2; i++) { /* user input of dates */ printf ("Enter a year: "); scanf ("%d", &dates[i].year); printf ("Enter a month :"); scanf ("%d", &dates[i].month); printf ("Enter a day: "); scanf ("%d", &dates[i].day); putchar ('\n'); } res = later (&dates[0], &dates[1]); /* comparison */ if (res == 0) prn_result (dates[0], dates[1], '='); else if (res > 0) prn_result (dates[0], dates[1], '>'); else prn_result (dates[0], dates[1], '<'); return 0; } int later (struct date *one, struct date *two) { /* the ternary operator is your friend */ if (one->year != two->year) /* years differ */ return one->year > two->year ? 1 : -1; if (one->month != two->month) /* months differ */ return one->month > two->month ? 1 : -1; if (one->day != two->day) /* days differ */ return one->day > two->day ? 1 : -1; return 0; /* dates are equivalent */ } void prn_result (struct date a, struct date b, char op) { printf ("%d/%d/%d %c %d/%d/%d\n", a.month, a.day, a.year, op, b.month, b.day, b.year); }
  “$ ./bin/cmpyear Enter a year: 1993 Enter a month :3 Enter a day: 13 Enter a year: 1993 Enter a month :3 Enter a day: 12 3/13/1993 > 3/12/1993 $ ./bin/cmpyear Enter a year: 1993 Enter a month :3 Enter a day: 12 Enter a year: 1993 Enter a month :3 Enter a day: 12 3/12/1993 = 3/12/1993 $ ./bin/cmpyear Enter a year: 1993 Enter a month :3 Enter a day: 12 Enter a year: 1993 Enter a month :3 Enter a day: 13 3/12/1993 < 3/13/1993 ”链接与“common”链接最相似,但它们用于C中的暂定定义,例如全局范围内的“common”。具有“weak”链接的符号与int X;符号的合并方式相同,如果未引用,则可能不会被删除common符号可能没有明确的部分,必须为零初始值设定项,并且可能不会标记为“weak”。函数和别名可能没有共同的联系   ...
  common
  如果没有使用上述标识符,则全局外部可见,这意味着它参与链接并可用于解析外部符号引用。

因此constant在外部可见,这意味着它不能保证是常量(可以从外部更改),而external是一种明确表示它可能不会的链接类型即使没有引用也要删除。