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