blob: e07bdeb49641b0f29afc7ea3d4f45cbaae543aa5 [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"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070022#include "IceInst.h" // for the names of the Inst subtypes
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070023#include "IceTypes.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070024
25namespace Ice {
26
Jan Voung8acded02014-09-22 18:02:25 -070027class Assembler;
28
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070029// LoweringContext makes it easy to iterate through non-deleted
30// instructions in a node, and insert new (lowered) instructions at
31// the current point. Along with the instruction list container and
32// associated iterators, it holds the current node, which is needed
33// when inserting new instructions in order to track whether variables
34// are used as single-block or multi-block.
35class LoweringContext {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070036 LoweringContext(const LoweringContext &) = delete;
37 LoweringContext &operator=(const LoweringContext &) = delete;
38
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070039public:
Jim Stichnothae953202014-12-20 06:17:49 -080040 LoweringContext() : Node(nullptr), LastInserted(nullptr) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070041 ~LoweringContext() {}
42 void init(CfgNode *Node);
43 Inst *getNextInst() const {
44 if (Next == End)
Jim Stichnothae953202014-12-20 06:17:49 -080045 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080046 return Next;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070047 }
Jan Voungc820ddf2014-07-29 14:38:51 -070048 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070049 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070050 if (Iter == End)
Jim Stichnothae953202014-12-20 06:17:49 -080051 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080052 return Iter;
Jan Voungc820ddf2014-07-29 14:38:51 -070053 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070054 CfgNode *getNode() const { return Node; }
55 bool atEnd() const { return Cur == End; }
56 InstList::iterator getCur() const { return Cur; }
Jim Stichnoth5d2fa0c2014-12-01 09:30:55 -080057 InstList::iterator getNext() const { return Next; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070058 InstList::iterator getEnd() const { return End; }
59 void insert(Inst *Inst);
Jan Vounge6e497d2014-07-30 10:06:03 -070060 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070061 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -070062 void advanceNext() { advanceForward(Next); }
Jim Stichnoth336f6c42014-10-30 15:01:31 -070063 void rewind();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070064 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
65
66private:
67 // Node is the argument to Inst::updateVars().
68 CfgNode *Node;
Jim Stichnoth98712a32014-10-24 10:59:02 -070069 Inst *LastInserted;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070070 // Cur points to the current instruction being considered. It is
71 // guaranteed to point to a non-deleted instruction, or to be End.
72 InstList::iterator Cur;
73 // Next doubles as a pointer to the next valid instruction (if any),
74 // and the new-instruction insertion point. It is also updated for
75 // the caller in case the lowering consumes more than one high-level
76 // instruction. It is guaranteed to point to a non-deleted
77 // instruction after Cur, or to be End. TODO: Consider separating
78 // the notion of "next valid instruction" and "new instruction
79 // insertion point", to avoid confusion when previously-deleted
80 // instructions come between the two points.
81 InstList::iterator Next;
Jan Vounge6e497d2014-07-30 10:06:03 -070082 // Begin is a copy of Insts.begin(), used if iterators are moved backward.
83 InstList::iterator Begin;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070084 // End is a copy of Insts.end(), used if Next needs to be advanced.
85 InstList::iterator End;
86
Jan Voungc820ddf2014-07-29 14:38:51 -070087 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -070088 void advanceForward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070089};
90
91class TargetLowering {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070092 TargetLowering(const TargetLowering &) = delete;
93 TargetLowering &operator=(const TargetLowering &) = delete;
94
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070095public:
96 static TargetLowering *createLowering(TargetArch Target, Cfg *Func);
Jan Voung8acded02014-09-22 18:02:25 -070097 static Assembler *createAssembler(TargetArch Target, Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070098 void translate() {
99 switch (Ctx->getOptLevel()) {
100 case Opt_m1:
101 translateOm1();
102 break;
103 case Opt_0:
104 translateO0();
105 break;
106 case Opt_1:
107 translateO1();
108 break;
109 case Opt_2:
110 translateO2();
111 break;
112 default:
113 Func->setError("Target doesn't specify lowering steps.");
114 break;
115 }
116 }
117 virtual void translateOm1() {
118 Func->setError("Target doesn't specify Om1 lowering steps.");
119 }
120 virtual void translateO0() {
121 Func->setError("Target doesn't specify O0 lowering steps.");
122 }
123 virtual void translateO1() {
124 Func->setError("Target doesn't specify O1 lowering steps.");
125 }
126 virtual void translateO2() {
127 Func->setError("Target doesn't specify O2 lowering steps.");
128 }
129
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700130 // Tries to do address mode optimization on a single instruction.
131 void doAddressOpt();
Matt Walac3302742014-08-15 16:21:56 -0700132 // Randomly insert NOPs.
133 void doNopInsertion();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700134 // Lowers a single non-Phi instruction.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700135 void lower();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700136 // Does preliminary lowering of the set of Phi instructions in the
137 // current node. The main intention is to do what's needed to keep
138 // the unlowered Phi instructions consistent with the lowered
139 // non-Phi instructions, e.g. to lower 64-bit operands on a 32-bit
140 // target.
141 virtual void prelowerPhis() {}
142 // Lowers a list of "parallel" assignment instructions representing
143 // a topological sort of the Phi instructions.
144 virtual void lowerPhiAssignments(CfgNode *Node,
145 const AssignList &Assignments) = 0;
Jim Stichnothff9c7062014-09-18 04:50:49 -0700146 // Tries to do branch optimization on a single instruction. Returns
147 // true if some optimization was done.
148 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
149 return false;
150 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700151
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700152 virtual SizeT getNumRegisters() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700153 // Returns a variable pre-colored to the specified physical
154 // register. This is generally used to get very direct access to
155 // the register such as in the prolog or epilog or for marking
Jim Stichnoth98712a32014-10-24 10:59:02 -0700156 // scratch registers as killed by a call. If a Type is not
157 // provided, a target-specific default type is used.
158 virtual Variable *getPhysicalRegister(SizeT RegNum,
159 Type Ty = IceType_void) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700160 // Returns a printable name for the register.
161 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
162
163 virtual bool hasFramePointer() const { return false; }
164 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700165 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700166 bool hasComputedFrame() const { return HasComputedFrame; }
Matt Walac3302742014-08-15 16:21:56 -0700167 bool shouldDoNopInsertion() const;
Jan Voung44d53e12014-09-11 19:18:03 -0700168 // Returns true if this function calls a function that has the
169 // "returns twice" attribute.
170 bool callsReturnsTwice() const { return CallsReturnsTwice; }
171 void setCallsReturnsTwice(bool RetTwice) {
172 CallsReturnsTwice = RetTwice;
173 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700174 int32_t getStackAdjustment() const { return StackAdjustment; }
175 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
176 void resetStackAdjustment() { StackAdjustment = 0; }
177 LoweringContext &getContext() { return Context; }
178
179 enum RegSet {
180 RegSet_None = 0,
181 RegSet_CallerSave = 1 << 0,
182 RegSet_CalleeSave = 1 << 1,
183 RegSet_StackPointer = 1 << 2,
184 RegSet_FramePointer = 1 << 3,
185 RegSet_All = ~RegSet_None
186 };
187 typedef uint32_t RegSetMask;
188
189 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
190 RegSetMask Exclude) const = 0;
191 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800192 void regAlloc(RegAllocKind Kind);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700193
Jim Stichnothe6d24782014-12-19 05:42:24 -0800194 virtual void makeRandomRegisterPermutation(
195 llvm::SmallVectorImpl<int32_t> &Permutation,
196 const llvm::SmallBitVector &ExcludeRegisters) const = 0;
197
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700198 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700199
Matt Wala45a06232014-07-09 16:33:22 -0700200 // Performs target-specific argument lowering.
201 virtual void lowerArguments() = 0;
202
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700203 virtual void addProlog(CfgNode *Node) = 0;
204 virtual void addEpilog(CfgNode *Node) = 0;
205
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700206 virtual void emitConstants() const = 0;
207
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700208 virtual ~TargetLowering() {}
209
210protected:
Jim Stichnothe6d24782014-12-19 05:42:24 -0800211 TargetLowering(Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700212 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
213 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
214 virtual void lowerAssign(const InstAssign *Inst) = 0;
215 virtual void lowerBr(const InstBr *Inst) = 0;
216 virtual void lowerCall(const InstCall *Inst) = 0;
217 virtual void lowerCast(const InstCast *Inst) = 0;
218 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700219 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700220 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700221 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700222 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700223 virtual void lowerLoad(const InstLoad *Inst) = 0;
224 virtual void lowerPhi(const InstPhi *Inst) = 0;
225 virtual void lowerRet(const InstRet *Inst) = 0;
226 virtual void lowerSelect(const InstSelect *Inst) = 0;
227 virtual void lowerStore(const InstStore *Inst) = 0;
228 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
229 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
230
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700231 virtual void doAddressOptLoad() {}
232 virtual void doAddressOptStore() {}
Matt Walac3302742014-08-15 16:21:56 -0700233 virtual void randomlyInsertNop(float Probability) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700234 // This gives the target an opportunity to post-process the lowered
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800235 // expansion before returning.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700236 virtual void postLower() {}
237
238 Cfg *Func;
239 GlobalContext *Ctx;
Jim Stichnothe6d24782014-12-19 05:42:24 -0800240 const bool RandomizeRegisterAllocation;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700241 bool HasComputedFrame;
Jan Voung44d53e12014-09-11 19:18:03 -0700242 bool CallsReturnsTwice;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700243 // StackAdjustment keeps track of the current stack offset from its
244 // natural location, as arguments are pushed for a function call.
245 int32_t StackAdjustment;
246 LoweringContext Context;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700247};
248
Jim Stichnothde4ca712014-06-29 08:13:48 -0700249// TargetGlobalInitLowering is used for "lowering" global
250// initializers. It is separated out from TargetLowering because it
251// does not require a Cfg.
252class TargetGlobalInitLowering {
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700253 TargetGlobalInitLowering(const TargetGlobalInitLowering &) = delete;
254 TargetGlobalInitLowering &operator=(const TargetGlobalInitLowering &) =
255 delete;
256
Jim Stichnothde4ca712014-06-29 08:13:48 -0700257public:
258 static TargetGlobalInitLowering *createLowering(TargetArch Target,
259 GlobalContext *Ctx);
Jan Voung839c4ce2014-07-28 15:19:43 -0700260 virtual ~TargetGlobalInitLowering();
261
Karl Schimpf9d98d792014-10-13 15:01:08 -0700262 virtual void lower(const VariableDeclaration &Var) = 0;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700263
264protected:
265 TargetGlobalInitLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
266 GlobalContext *Ctx;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700267};
268
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700269} // end of namespace Ice
270
271#endif // SUBZERO_SRC_ICETARGETLOWERING_H