blob: 91512a9fb47189ad7f322bb1d18e8c08816f076b [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 }
Jan Voungc820ddf2014-07-29 14:38:51 -070044 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070045 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070046 if (Iter == End)
47 return NULL;
48 return *Iter;
49 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070050 CfgNode *getNode() const { return Node; }
51 bool atEnd() const { return Cur == End; }
52 InstList::iterator getCur() const { return Cur; }
53 InstList::iterator getEnd() const { return End; }
54 void insert(Inst *Inst);
Jan Vounge6e497d2014-07-30 10:06:03 -070055 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070056 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -070057 void advanceNext() { advanceForward(Next); }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070058 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
59
60private:
61 // Node is the argument to Inst::updateVars().
62 CfgNode *Node;
63 // Cur points to the current instruction being considered. It is
64 // guaranteed to point to a non-deleted instruction, or to be End.
65 InstList::iterator Cur;
66 // Next doubles as a pointer to the next valid instruction (if any),
67 // and the new-instruction insertion point. It is also updated for
68 // the caller in case the lowering consumes more than one high-level
69 // instruction. It is guaranteed to point to a non-deleted
70 // instruction after Cur, or to be End. TODO: Consider separating
71 // the notion of "next valid instruction" and "new instruction
72 // insertion point", to avoid confusion when previously-deleted
73 // instructions come between the two points.
74 InstList::iterator Next;
Jan Vounge6e497d2014-07-30 10:06:03 -070075 // Begin is a copy of Insts.begin(), used if iterators are moved backward.
76 InstList::iterator Begin;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070077 // End is a copy of Insts.end(), used if Next needs to be advanced.
78 InstList::iterator End;
79
Jan Voungc820ddf2014-07-29 14:38:51 -070080 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -070081 void advanceForward(InstList::iterator &I) const;
82 void advanceBackward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070083 LoweringContext(const LoweringContext &) LLVM_DELETED_FUNCTION;
84 LoweringContext &operator=(const LoweringContext &) LLVM_DELETED_FUNCTION;
85};
86
87class TargetLowering {
88public:
89 static TargetLowering *createLowering(TargetArch Target, Cfg *Func);
90 void translate() {
91 switch (Ctx->getOptLevel()) {
92 case Opt_m1:
93 translateOm1();
94 break;
95 case Opt_0:
96 translateO0();
97 break;
98 case Opt_1:
99 translateO1();
100 break;
101 case Opt_2:
102 translateO2();
103 break;
104 default:
105 Func->setError("Target doesn't specify lowering steps.");
106 break;
107 }
108 }
109 virtual void translateOm1() {
110 Func->setError("Target doesn't specify Om1 lowering steps.");
111 }
112 virtual void translateO0() {
113 Func->setError("Target doesn't specify O0 lowering steps.");
114 }
115 virtual void translateO1() {
116 Func->setError("Target doesn't specify O1 lowering steps.");
117 }
118 virtual void translateO2() {
119 Func->setError("Target doesn't specify O2 lowering steps.");
120 }
121
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700122 // Tries to do address mode optimization on a single instruction.
123 void doAddressOpt();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700124 // Lowers a single instruction.
125 void lower();
126
127 // Returns a variable pre-colored to the specified physical
128 // register. This is generally used to get very direct access to
129 // the register such as in the prolog or epilog or for marking
130 // scratch registers as killed by a call.
131 virtual Variable *getPhysicalRegister(SizeT RegNum) = 0;
132 // Returns a printable name for the register.
133 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
134
135 virtual bool hasFramePointer() const { return false; }
136 virtual SizeT getFrameOrStackReg() const = 0;
137 virtual size_t typeWidthInBytesOnStack(Type Ty) = 0;
138 bool hasComputedFrame() const { return HasComputedFrame; }
139 int32_t getStackAdjustment() const { return StackAdjustment; }
140 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
141 void resetStackAdjustment() { StackAdjustment = 0; }
142 LoweringContext &getContext() { return Context; }
143
144 enum RegSet {
145 RegSet_None = 0,
146 RegSet_CallerSave = 1 << 0,
147 RegSet_CalleeSave = 1 << 1,
148 RegSet_StackPointer = 1 << 2,
149 RegSet_FramePointer = 1 << 3,
150 RegSet_All = ~RegSet_None
151 };
152 typedef uint32_t RegSetMask;
153
154 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
155 RegSetMask Exclude) const = 0;
156 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
157 void regAlloc();
158
159 virtual void emitVariable(const Variable *Var, const Cfg *Func) const = 0;
160
Matt Wala45a06232014-07-09 16:33:22 -0700161 // Performs target-specific argument lowering.
162 virtual void lowerArguments() = 0;
163
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700164 virtual void addProlog(CfgNode *Node) = 0;
165 virtual void addEpilog(CfgNode *Node) = 0;
166
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700167 virtual void emitConstants() const = 0;
168
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700169 virtual ~TargetLowering() {}
170
171protected:
172 TargetLowering(Cfg *Func)
173 : Func(Func), Ctx(Func->getContext()), HasComputedFrame(false),
174 StackAdjustment(0) {}
175 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
176 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
177 virtual void lowerAssign(const InstAssign *Inst) = 0;
178 virtual void lowerBr(const InstBr *Inst) = 0;
179 virtual void lowerCall(const InstCall *Inst) = 0;
180 virtual void lowerCast(const InstCast *Inst) = 0;
181 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700182 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700183 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700184 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700185 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700186 virtual void lowerLoad(const InstLoad *Inst) = 0;
187 virtual void lowerPhi(const InstPhi *Inst) = 0;
188 virtual void lowerRet(const InstRet *Inst) = 0;
189 virtual void lowerSelect(const InstSelect *Inst) = 0;
190 virtual void lowerStore(const InstStore *Inst) = 0;
191 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
192 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
193
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700194 virtual void doAddressOptLoad() {}
195 virtual void doAddressOptStore() {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700196 // This gives the target an opportunity to post-process the lowered
197 // expansion before returning. The primary intention is to do some
198 // Register Manager activity as necessary, specifically to eagerly
199 // allocate registers based on affinity and other factors. The
200 // simplest lowering does nothing here and leaves it all to a
201 // subsequent global register allocation pass.
202 virtual void postLower() {}
203
204 Cfg *Func;
205 GlobalContext *Ctx;
206 bool HasComputedFrame;
207 // StackAdjustment keeps track of the current stack offset from its
208 // natural location, as arguments are pushed for a function call.
209 int32_t StackAdjustment;
210 LoweringContext Context;
211
212private:
213 TargetLowering(const TargetLowering &) LLVM_DELETED_FUNCTION;
214 TargetLowering &operator=(const TargetLowering &) LLVM_DELETED_FUNCTION;
215};
216
Jim Stichnothde4ca712014-06-29 08:13:48 -0700217// TargetGlobalInitLowering is used for "lowering" global
218// initializers. It is separated out from TargetLowering because it
219// does not require a Cfg.
220class TargetGlobalInitLowering {
221public:
222 static TargetGlobalInitLowering *createLowering(TargetArch Target,
223 GlobalContext *Ctx);
Jan Voung839c4ce2014-07-28 15:19:43 -0700224 virtual ~TargetGlobalInitLowering();
225
Jim Stichnothde4ca712014-06-29 08:13:48 -0700226 // TODO: Allow relocations to be represented as part of the Data.
227 virtual void lower(const IceString &Name, SizeT Align, bool IsInternal,
228 bool IsConst, bool IsZeroInitializer, SizeT Size,
229 const char *Data, bool DisableTranslation) = 0;
230
231protected:
232 TargetGlobalInitLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
233 GlobalContext *Ctx;
234
235private:
236 TargetGlobalInitLowering(const TargetGlobalInitLowering &)
237 LLVM_DELETED_FUNCTION;
238 TargetGlobalInitLowering &
239 operator=(const TargetGlobalInitLowering &) LLVM_DELETED_FUNCTION;
240};
241
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700242} // end of namespace Ice
243
244#endif // SUBZERO_SRC_ICETARGETLOWERING_H