blob: da52adfcc7ae838a8781085a8cd2954d9d48823e [file] [log] [blame]
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -07001//===- subzero/src/IceTargetLowering.h - Lowering interface -----*- C++ -*-===//
2//
3// The Subzero Code Generator
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 declares the TargetLowering and LoweringContext
11// classes. TargetLowering is an abstract class used to drive the
12// translation/lowering process. LoweringContext maintains a
13// context for lowering each instruction, offering conveniences such
14// as iterating over non-deleted instructions.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef SUBZERO_SRC_ICETARGETLOWERING_H
19#define SUBZERO_SRC_ICETARGETLOWERING_H
20
21#include "IceDefs.h"
22#include "IceTypes.h"
23
24#include "IceInst.h" // for the names of the Inst subtypes
25
26namespace Ice {
27
28// LoweringContext makes it easy to iterate through non-deleted
29// instructions in a node, and insert new (lowered) instructions at
30// the current point. Along with the instruction list container and
31// associated iterators, it holds the current node, which is needed
32// when inserting new instructions in order to track whether variables
33// are used as single-block or multi-block.
34class LoweringContext {
35public:
36 LoweringContext() : Node(NULL) {}
37 ~LoweringContext() {}
38 void init(CfgNode *Node);
39 Inst *getNextInst() const {
40 if (Next == End)
41 return NULL;
42 return *Next;
43 }
44 CfgNode *getNode() const { return Node; }
45 bool atEnd() const { return Cur == End; }
46 InstList::iterator getCur() const { return Cur; }
47 InstList::iterator getEnd() const { return End; }
48 void insert(Inst *Inst);
49 void advanceCur() { Cur = Next; }
50 void advanceNext() { advance(Next); }
51 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
52
53private:
54 // Node is the argument to Inst::updateVars().
55 CfgNode *Node;
56 // Cur points to the current instruction being considered. It is
57 // guaranteed to point to a non-deleted instruction, or to be End.
58 InstList::iterator Cur;
59 // Next doubles as a pointer to the next valid instruction (if any),
60 // and the new-instruction insertion point. It is also updated for
61 // the caller in case the lowering consumes more than one high-level
62 // instruction. It is guaranteed to point to a non-deleted
63 // instruction after Cur, or to be End. TODO: Consider separating
64 // the notion of "next valid instruction" and "new instruction
65 // insertion point", to avoid confusion when previously-deleted
66 // instructions come between the two points.
67 InstList::iterator Next;
68 // End is a copy of Insts.end(), used if Next needs to be advanced.
69 InstList::iterator End;
70
71 void skipDeleted(InstList::iterator &I);
72 void advance(InstList::iterator &I);
73 LoweringContext(const LoweringContext &) LLVM_DELETED_FUNCTION;
74 LoweringContext &operator=(const LoweringContext &) LLVM_DELETED_FUNCTION;
75};
76
77class TargetLowering {
78public:
79 static TargetLowering *createLowering(TargetArch Target, Cfg *Func);
80 void translate() {
81 switch (Ctx->getOptLevel()) {
82 case Opt_m1:
83 translateOm1();
84 break;
85 case Opt_0:
86 translateO0();
87 break;
88 case Opt_1:
89 translateO1();
90 break;
91 case Opt_2:
92 translateO2();
93 break;
94 default:
95 Func->setError("Target doesn't specify lowering steps.");
96 break;
97 }
98 }
99 virtual void translateOm1() {
100 Func->setError("Target doesn't specify Om1 lowering steps.");
101 }
102 virtual void translateO0() {
103 Func->setError("Target doesn't specify O0 lowering steps.");
104 }
105 virtual void translateO1() {
106 Func->setError("Target doesn't specify O1 lowering steps.");
107 }
108 virtual void translateO2() {
109 Func->setError("Target doesn't specify O2 lowering steps.");
110 }
111
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700112 // Tries to do address mode optimization on a single instruction.
113 void doAddressOpt();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700114 // Lowers a single instruction.
115 void lower();
116
117 // Returns a variable pre-colored to the specified physical
118 // register. This is generally used to get very direct access to
119 // the register such as in the prolog or epilog or for marking
120 // scratch registers as killed by a call.
121 virtual Variable *getPhysicalRegister(SizeT RegNum) = 0;
122 // Returns a printable name for the register.
123 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
124
125 virtual bool hasFramePointer() const { return false; }
126 virtual SizeT getFrameOrStackReg() const = 0;
127 virtual size_t typeWidthInBytesOnStack(Type Ty) = 0;
128 bool hasComputedFrame() const { return HasComputedFrame; }
129 int32_t getStackAdjustment() const { return StackAdjustment; }
130 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
131 void resetStackAdjustment() { StackAdjustment = 0; }
132 LoweringContext &getContext() { return Context; }
133
134 enum RegSet {
135 RegSet_None = 0,
136 RegSet_CallerSave = 1 << 0,
137 RegSet_CalleeSave = 1 << 1,
138 RegSet_StackPointer = 1 << 2,
139 RegSet_FramePointer = 1 << 3,
140 RegSet_All = ~RegSet_None
141 };
142 typedef uint32_t RegSetMask;
143
144 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
145 RegSetMask Exclude) const = 0;
146 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
147 void regAlloc();
148
149 virtual void emitVariable(const Variable *Var, const Cfg *Func) const = 0;
150
Matt Wala45a06232014-07-09 16:33:22 -0700151 // Performs target-specific argument lowering.
152 virtual void lowerArguments() = 0;
153
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700154 virtual void addProlog(CfgNode *Node) = 0;
155 virtual void addEpilog(CfgNode *Node) = 0;
156
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700157 virtual void emitConstants() const = 0;
158
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700159 virtual ~TargetLowering() {}
160
161protected:
162 TargetLowering(Cfg *Func)
163 : Func(Func), Ctx(Func->getContext()), HasComputedFrame(false),
164 StackAdjustment(0) {}
165 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
166 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
167 virtual void lowerAssign(const InstAssign *Inst) = 0;
168 virtual void lowerBr(const InstBr *Inst) = 0;
169 virtual void lowerCall(const InstCall *Inst) = 0;
170 virtual void lowerCast(const InstCast *Inst) = 0;
171 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700172 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700173 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700174 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700175 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700176 virtual void lowerLoad(const InstLoad *Inst) = 0;
177 virtual void lowerPhi(const InstPhi *Inst) = 0;
178 virtual void lowerRet(const InstRet *Inst) = 0;
179 virtual void lowerSelect(const InstSelect *Inst) = 0;
180 virtual void lowerStore(const InstStore *Inst) = 0;
181 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
182 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
183
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700184 virtual void doAddressOptLoad() {}
185 virtual void doAddressOptStore() {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700186 // This gives the target an opportunity to post-process the lowered
187 // expansion before returning. The primary intention is to do some
188 // Register Manager activity as necessary, specifically to eagerly
189 // allocate registers based on affinity and other factors. The
190 // simplest lowering does nothing here and leaves it all to a
191 // subsequent global register allocation pass.
192 virtual void postLower() {}
193
194 Cfg *Func;
195 GlobalContext *Ctx;
196 bool HasComputedFrame;
197 // StackAdjustment keeps track of the current stack offset from its
198 // natural location, as arguments are pushed for a function call.
199 int32_t StackAdjustment;
200 LoweringContext Context;
201
202private:
203 TargetLowering(const TargetLowering &) LLVM_DELETED_FUNCTION;
204 TargetLowering &operator=(const TargetLowering &) LLVM_DELETED_FUNCTION;
205};
206
Jim Stichnothde4ca712014-06-29 08:13:48 -0700207// TargetGlobalInitLowering is used for "lowering" global
208// initializers. It is separated out from TargetLowering because it
209// does not require a Cfg.
210class TargetGlobalInitLowering {
211public:
212 static TargetGlobalInitLowering *createLowering(TargetArch Target,
213 GlobalContext *Ctx);
Jan Voung839c4ce2014-07-28 15:19:43 -0700214 virtual ~TargetGlobalInitLowering();
215
Jim Stichnothde4ca712014-06-29 08:13:48 -0700216 // TODO: Allow relocations to be represented as part of the Data.
217 virtual void lower(const IceString &Name, SizeT Align, bool IsInternal,
218 bool IsConst, bool IsZeroInitializer, SizeT Size,
219 const char *Data, bool DisableTranslation) = 0;
220
221protected:
222 TargetGlobalInitLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
223 GlobalContext *Ctx;
224
225private:
226 TargetGlobalInitLowering(const TargetGlobalInitLowering &)
227 LLVM_DELETED_FUNCTION;
228 TargetGlobalInitLowering &
229 operator=(const TargetGlobalInitLowering &) LLVM_DELETED_FUNCTION;
230};
231
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700232} // end of namespace Ice
233
234#endif // SUBZERO_SRC_ICETARGETLOWERING_H