Skip to content

Commit cab34b1

Browse files
[SystemZ] Add proper mcount handling
When compiling with `-pg`, the `EntryExitInstrumenterPass` will insert calls to the glibc function `mcount` at the begining of each `MachineFunction`. On SystemZ, these calls require special handling: - The call to `mcount` needs to happen at the beginning of the prologue. - Prior to the call to `mcount`, register `%r14`, the return address of the callee function, must be stored 8 bytes above the stack pointer `%r15`. After the call to `mcount` returns, that register needs to be restored. This commit adds some special handling to the EntryExitInstrumenterPass that keeps the insertion of the mcount function into the module, but skips over insertion of the actual call in order to perform this insertion in the `emitPrologue` function. There, a simple sequence of store/call/load is inserted, which implements the above.
1 parent df26417 commit cab34b1

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/CodeGen/RegisterScavenging.h"
1919
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
2020
#include "llvm/IR/Function.h"
21+
#include "llvm/IR/Module.h"
2122
#include "llvm/Target/TargetMachine.h"
2223

2324
using namespace llvm;
@@ -558,6 +559,32 @@ void SystemZELFFrameLowering::emitPrologue(MachineFunction &MF,
558559
// to determine the end of the prologue.
559560
DebugLoc DL;
560561

562+
// Add mcount instrumentation if necessary.
563+
if (MF.getFunction().getFnAttribute("systemz-backend").getValueAsString() ==
564+
"insert-mcount") {
565+
566+
// Store return address 8 bytes above stack pointer.
567+
BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
568+
.addReg(SystemZ::R14D)
569+
.addReg(SystemZ::R15D)
570+
.addImm(8)
571+
.addReg(0);
572+
573+
// Call mcount (Regmask 0 to ensure this will not be moved by the
574+
// scheduler.).
575+
const uint32_t Mask = 0;
576+
BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::CallBRASL))
577+
.addGlobalAddress(MF.getFunction().getParent()->getFunction("mcount"))
578+
.addRegMask(&Mask);
579+
580+
// Reload return address drom 8 bytes above stack pointer.
581+
BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LG))
582+
.addReg(SystemZ::R14D)
583+
.addReg(SystemZ::R15D)
584+
.addImm(8)
585+
.addReg(0);
586+
}
587+
561588
// The current offset of the stack pointer from the CFA.
562589
int64_t SPOffsetFromCFA = -SystemZMC::ELFCFAOffsetFromInitialSP;
563590

llvm/lib/Transforms/Utils/EntryExitInstrumenter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ static void insertCall(Function &CurFn, StringRef Func,
6363
false));
6464
CallInst *Call = CallInst::Create(Fn, RetAddr, "", InsertionPt);
6565
Call->setDebugLoc(DL);
66+
} else if (TargetTriple.isSystemZ()) {
67+
M.getOrInsertFunction(Func, Type::getVoidTy(C));
68+
// skip insertion for `mcount` on SystemZ. This will be handled later in
69+
// `emitPrologue`. Add custom attribute to denote this.
70+
CurFn.addFnAttr(
71+
llvm::Attribute::get(C, "systemz-backend", "insert-mcount"));
6672
} else {
6773
FunctionCallee Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C));
6874
CallInst *Call = CallInst::Create(Fn, "", InsertionPt);

llvm/test/CodeGen/SystemZ/mcount.ll

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; Test proper insertion of mcount instrumentation
2+
;
3+
; RUN: llc < %s -mtriple=s390x-linux-gnu -o - | FileCheck %s
4+
;
5+
; CHECK: # %bb.0:
6+
; CHECK-NEXT: stg %r14, 8(%r15)
7+
; CHECK-NEXT: brasl %r14, mcount@PLT
8+
; CHECK-NEXT: lg %r14, 8(%r15)
9+
define dso_local signext i32 @fib(i32 noundef signext %n) #0 {
10+
entry:
11+
%n.addr = alloca i32, align 4
12+
store i32 %n, ptr %n.addr, align 4
13+
%0 = load i32, ptr %n.addr, align 4
14+
%cmp = icmp sle i32 %0, 1
15+
br i1 %cmp, label %cond.true, label %cond.false
16+
17+
cond.true: ; preds = %entry
18+
br label %cond.end
19+
20+
cond.false: ; preds = %entry
21+
%1 = load i32, ptr %n.addr, align 4
22+
%sub = sub nsw i32 %1, 1
23+
%call = call signext i32 @fib(i32 noundef signext %sub)
24+
%2 = load i32, ptr %n.addr, align 4
25+
%sub1 = sub nsw i32 %2, 2
26+
%call2 = call signext i32 @fib(i32 noundef signext %sub1)
27+
%add = add nsw i32 %call, %call2
28+
br label %cond.end
29+
30+
cond.end: ; preds = %cond.false, %cond.true
31+
%cond = phi i32 [ 1, %cond.true ], [ %add, %cond.false ]
32+
ret i32 %cond
33+
}
34+
35+
attributes #0 = { "instrument-function-entry-inlined"="mcount" }

0 commit comments

Comments
 (0)