1 //===- 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"
26 #include "llvm/CodeGen/MachineMemOperand.h"
27 #include "llvm/CodeGen/PseudoSourceValue.h"
28
29 namespace 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.
34 struct 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;
47 const GlobalValue *GV;
48
SystemZAddressModeSystemZAddressMode49 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 ///
58 static inline const MachineInstrBuilder &
addDirectMem(const MachineInstrBuilder & MIB,unsigned Reg)59 addDirectMem(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
65 static inline const MachineInstrBuilder &
addOffset(const MachineInstrBuilder & MIB,int Offset)66 addOffset(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 ///
74 static inline const MachineInstrBuilder &
addRegOffset(const MachineInstrBuilder & MIB,unsigned Reg,bool isKill,int Offset)75 addRegOffset(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].
82 static inline const MachineInstrBuilder &
addRegReg(const MachineInstrBuilder & MIB,unsigned Reg1,bool isKill1,unsigned Reg2,bool isKill2)83 addRegReg(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
89 static inline const MachineInstrBuilder &
addFullAddress(const MachineInstrBuilder & MIB,const SystemZAddressMode & AM)90 addFullAddress(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
98 return MIB.addImm(AM.Disp).addReg(AM.IndexReg);
99 }
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 ///
106 static inline const MachineInstrBuilder &
107 addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset = 0) {
108 MachineInstr *MI = MIB;
109 MachineFunction &MF = *MI->getParent()->getParent();
110 MachineFrameInfo &MFI = *MF.getFrameInfo();
111 const MCInstrDesc &MCID = MI->getDesc();
112 unsigned Flags = 0;
113 if (MCID.mayLoad())
114 Flags |= MachineMemOperand::MOLoad;
115 if (MCID.mayStore())
116 Flags |= MachineMemOperand::MOStore;
117 MachineMemOperand *MMO =
118 MF.getMachineMemOperand(MachinePointerInfo(
119 PseudoSourceValue::getFixedStack(FI), Offset),
120 Flags, MFI.getObjectSize(FI),
121 MFI.getObjectAlignment(FI));
122 return addOffset(MIB.addFrameIndex(FI), Offset)
123 .addMemOperand(MMO);
124 }
125
126 } // End llvm namespace
127
128 #endif
129