现代C编译器的内联函数被称为一次?

就像我的头文件是:
class A
{
    void Complicated();
}

和我的源文件

void A::Complicated()
{
    ...really long function...
}

我可以将源文件拆分成

void DoInitialStuff(pass necessary vars by ref or value)
{
    ...
}
void HandleCaseA(pass necessary vars by ref or value)
{
    ...
}
void HandleCaseB(pass necessary vars by ref or value)
{
    ...
}
void FinishUp(pass necessary vars by ref or value)
{
    ...
}
void A::Complicated()
{
    ...
    DoInitialStuff(...);
    switch ...
        HandleCaseA(...)
        HandleCaseB(...)
    ...
    FinishUp(...)
}

完全可以阅读,不用担心影响性能

解决方法

您应该将函数标记为静态,以便编译器知道它们是该翻译单元的本地.

没有静态编译器不能假设(禁止LTO / WPA)该函数调用一次,因此不太可能内联它.

演示使用LLVM Try Out页.

也就是说,首先可读性的代码,微优化(和这样的调整是微优化)只能在性能测量之后才会出现.

例:

#include <cstdio>

static void foo(int i) {
  int m = i % 3;
  printf("%d %d",i,m);
}

int main(int argc,char* argv[]) {
  for (int i = 0; i != argc; ++i) {
    foo(i);
  }
}

产生静态:

; ModuleID = '/tmp/webcompile/_27689_0.bc'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-unkNown-linux-gnu"

@.str = private constant [6 x i8] c"%d %d\00"     ; <[6 x i8]*> [#uses=1]

define i32 @main(i32 %argc,i8** nocapture %argv) nounwind {
entry:
  %cmp4 = icmp eq i32 %argc,0                    ; <i1> [#uses=1]
  br i1 %cmp4,label %for.end,label %for.body

for.body:                                         ; preds = %for.body,%entry
  %0 = phi i32 [ %inc,%for.body ],[ 0,%entry ] ; <i32> [#uses=3]
  %rem.i = srem i32 %0,3                         ; <i32> [#uses=1]
  %call.i = tail call i32 (i8*,...)* @printf(i8* getelementptr inbounds ([6 x i8]* @.str,i64 0,i64 0),i32 %0,i32 %rem.i) nounwind ; <i32> [#uses=0]
  %inc = add nsw i32 %0,1                        ; <i32> [#uses=2]
  %exitcond = icmp eq i32 %inc,%argc             ; <i1> [#uses=1]
  br i1 %exitcond,label %for.body

for.end:                                          ; preds = %for.body,%entry
  ret i32 0
}

declare i32 @printf(i8* nocapture,...) nounwind

没有静态:

; ModuleID = '/tmp/webcompile/_27859_0.bc'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-unkNown-linux-gnu"

@.str = private constant [6 x i8] c"%d %d\00"     ; <[6 x i8]*> [#uses=1]

define void @foo(int)(i32 %i) nounwind {
entry:
  %rem = srem i32 %i,3                           ; <i32> [#uses=1]
  %call = tail call i32 (i8*,i32 %i,i32 %rem) ; <i32> [#uses=0]
  ret void
}

declare i32 @printf(i8* nocapture,...) nounwind

define i32 @main(i32 %argc,%entry
  ret i32 0
}

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...