blob: ab45ec5984e3f21f4656857117d364ddcc155db7 [file] [log] [blame]
Anton Korobeynikov501f55d2009-07-16 14:03:24 +00001//===- SystemZInstrBuilder.h - Functions to aid building insts -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file exposes functions that may be used with BuildMI from the
11// MachineInstrBuilder.h file to handle SystemZ'isms in a clean way.
12//
13// The BuildMem function may be used with the BuildMI function to add entire
14// memory references in a single, typed, function call.
15//
16// For reference, the order of operands for memory references is:
17// (Operand), Base, Displacement, Index.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef SYSTEMZINSTRBUILDER_H
22#define SYSTEMZINSTRBUILDER_H
23
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000026#include "llvm/CodeGen/MachineMemOperand.h"
Anton Korobeynikov501f55d2009-07-16 14:03:24 +000027#include "llvm/CodeGen/PseudoSourceValue.h"
28
29namespace llvm {
30
31/// SystemZAddressMode - This struct holds a generalized full x86 address mode.
32/// The base register can be a frame index, which will eventually be replaced
33/// with R15 or R11 and Disp being offsetted accordingly.
34struct SystemZAddressMode {
35 enum {
36 RegBase,
37 FrameIndexBase
38 } BaseType;
39
40 union {
41 unsigned Reg;
42 int FrameIndex;
43 } Base;
44
45 unsigned IndexReg;
46 int32_t Disp;
Dan Gohman46510a72010-04-15 01:51:59 +000047 const GlobalValue *GV;
Anton Korobeynikov501f55d2009-07-16 14:03:24 +000048
49 SystemZAddressMode() : BaseType(RegBase), IndexReg(0), Disp(0) {
50 Base.Reg = 0;
51 }
52};
53
54/// addDirectMem - This function is used to add a direct memory reference to the
55/// current instruction -- that is, a dereference of an address in a register,
56/// with no index or displacement.
57///
58static inline const MachineInstrBuilder &
59addDirectMem(const MachineInstrBuilder &MIB, unsigned Reg) {
60 // Because memory references are always represented with 3
61 // values, this adds: Reg, [0, NoReg] to the instruction.
62 return MIB.addReg(Reg).addImm(0).addReg(0);
63}
64
65static inline const MachineInstrBuilder &
66addOffset(const MachineInstrBuilder &MIB, int Offset) {
67 return MIB.addImm(Offset).addReg(0);
68}
69
70/// addRegOffset - This function is used to add a memory reference of the form
71/// [Reg + Offset], i.e., one with no or index, but with a
72/// displacement. An example is: 10(%r15).
73///
74static inline const MachineInstrBuilder &
75addRegOffset(const MachineInstrBuilder &MIB,
76 unsigned Reg, bool isKill, int Offset) {
77 return addOffset(MIB.addReg(Reg, getKillRegState(isKill)), Offset);
78}
79
80/// addRegReg - This function is used to add a memory reference of the form:
81/// [Reg + Reg].
82static inline const MachineInstrBuilder &
83addRegReg(const MachineInstrBuilder &MIB,
84 unsigned Reg1, bool isKill1, unsigned Reg2, bool isKill2) {
85 return MIB.addReg(Reg1, getKillRegState(isKill1)).addImm(0)
86 .addReg(Reg2, getKillRegState(isKill2));
87}
88
89static inline const MachineInstrBuilder &
90addFullAddress(const MachineInstrBuilder &MIB, const SystemZAddressMode &AM) {
91 if (AM.BaseType == SystemZAddressMode::RegBase)
92 MIB.addReg(AM.Base.Reg);
93 else if (AM.BaseType == SystemZAddressMode::FrameIndexBase)
94 MIB.addFrameIndex(AM.Base.FrameIndex);
95 else
96 assert(0);
97
Anton Korobeynikovbb496a32009-07-17 18:28:59 +000098 return MIB.addImm(AM.Disp).addReg(AM.IndexReg);
Anton Korobeynikov501f55d2009-07-16 14:03:24 +000099}
100
101/// addFrameReference - This function is used to add a reference to the base of
102/// an abstract object on the stack frame of the current function. This
103/// reference has base register as the FrameIndex offset until it is resolved.
104/// This allows a constant offset to be specified as well...
105///
106static inline const MachineInstrBuilder &
107addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset = 0) {
108 MachineInstr *MI = MIB;
109 MachineFunction &MF = *MI->getParent()->getParent();
110 MachineFrameInfo &MFI = *MF.getFrameInfo();
Evan Chenge837dea2011-06-28 19:10:37 +0000111 const MCInstrDesc &MCID = MI->getDesc();
Anton Korobeynikov501f55d2009-07-16 14:03:24 +0000112 unsigned Flags = 0;
Evan Chenge837dea2011-06-28 19:10:37 +0000113 if (MCID.mayLoad())
Anton Korobeynikov501f55d2009-07-16 14:03:24 +0000114 Flags |= MachineMemOperand::MOLoad;
Evan Chenge837dea2011-06-28 19:10:37 +0000115 if (MCID.mayStore())
Anton Korobeynikov501f55d2009-07-16 14:03:24 +0000116 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +0000117 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +0000118 MF.getMachineMemOperand(MachinePointerInfo(
119 PseudoSourceValue::getFixedStack(FI), Offset),
120 Flags, MFI.getObjectSize(FI),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000121 MFI.getObjectAlignment(FI));
Anton Korobeynikov501f55d2009-07-16 14:03:24 +0000122 return addOffset(MIB.addFrameIndex(FI), Offset)
123 .addMemOperand(MMO);
124}
125
126} // End llvm namespace
127
128#endif