Skip to content

Commit

Permalink
For functions with ARM target specific calling convention, when simpl…
Browse files Browse the repository at this point in the history
…ify-libcall

optimize a call to a llvm intrinsic to something that invovles a call to a C
library call, make sure it sets the right calling convention on the call.

e.g.
extern double pow(double, double);
double t(double x) {
  return pow(10, x);
}

Compiles to something like this for AAPCS-VFP:
define arm_aapcs_vfpcc double @t(double %x) #0 {
entry:
  %0 = call double @llvm.pow.f64(double 1.000000e+01, double %x)
  ret double %0
}

declare double @llvm.pow.f64(double, double) #1

Simplify libcall (part of instcombine) will turn the above into:
define arm_aapcs_vfpcc double @t(double %x) #0 {
entry:
  %__exp10 = call double @__exp10(double %x) #1
  ret double %__exp10
}

declare double @__exp10(double)

The pre-instcombine code works because calls to LLVM builtins are special.
Instruction selection will chose the right calling convention for the call.
However, the code after instcombine is wrong. The call to __exp10 will use
the C calling convention.

I can think of 3 options to fix this.

1. Make "C" calling convention just work since the target should know what CC
   is being used.

   This doesn't work because each function can use different CC with the "pcs"
   attribute.

2. Have Clang add the right CC keyword on the calls to LLVM builtin.

   This will work but it doesn't match the LLVM IR specification which states
   these are "Standard C Library Intrinsics".

3. Fix simplify libcall so the resulting calls to the C routines will have the
   proper CC keyword. e.g.
   %__exp10 = call arm_aapcs_vfpcc double @__exp10(double %x) #1

   This works and is the solution I implemented here.

Both solutions brson#2 and brson#3 would work. After carefully considering the pros and
cons, I decided to implement brson#3 for the following reasons.

1. It doesn't change the "spec" of the intrinsics.
2. It's a self-contained fix.

There are a couple of potential downsides.
1. There could be other places in the optimizer that is broken in the same way
   that's not addressed by this.
2. There could be other calling conventions that need to be propagated by
   simplify-libcall that's not handled.

But for now, this is the fix that I'm most comfortable with.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203488 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Evan Cheng committed Mar 10, 2014
1 parent 789dde4 commit d89b0f2
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 48 deletions.
4 changes: 4 additions & 0 deletions include/llvm/IR/CallingConv.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ namespace CallingConv {
X86_CDeclMethod = 80

};

/// isARMTargetCC - Return true if the specific calling convention is one of
/// ARM target specific calling convention.
bool isARMTargetCC(ID id);
} // End CallingConv namespace

} // End llvm namespace
Expand Down
8 changes: 8 additions & 0 deletions lib/IR/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,3 +779,11 @@ void Function::setPrefixData(Constant *PrefixData) {
}
setValueSubclassData(SCData);
}


/// isARMTargetCC - Return true if the specific calling convention is one of
/// ARM target specific calling convention.
/// There isn't a CallingConv.cpp so we are adding this utility routine here.
bool CallingConv::isARMTargetCC(ID id) {
return id == ARM_APCS || id == ARM_AAPCS || id == ARM_AAPCS_VFP;
}
Loading

0 comments on commit d89b0f2

Please sign in to comment.