blob: da1d3f0ae5d67c9d687416ea4ba43abb4b568e71 [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
Jim Stichnothf44f3712014-10-01 14:05:51 -070027typedef uint8_t AsmCodeByte;
28
Jan Voung8acded02014-09-22 18:02:25 -070029class Assembler;
30
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070031// LoweringContext makes it easy to iterate through non-deleted
32// instructions in a node, and insert new (lowered) instructions at
33// the current point. Along with the instruction list container and
34// associated iterators, it holds the current node, which is needed
35// when inserting new instructions in order to track whether variables
36// are used as single-block or multi-block.
37class LoweringContext {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070038 LoweringContext(const LoweringContext &) = delete;
39 LoweringContext &operator=(const LoweringContext &) = delete;
40
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070041public:
Jim Stichnoth98712a32014-10-24 10:59:02 -070042 LoweringContext() : Node(NULL), LastInserted(NULL) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070043 ~LoweringContext() {}
44 void init(CfgNode *Node);
45 Inst *getNextInst() const {
46 if (Next == End)
47 return NULL;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080048 return Next;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070049 }
Jan Voungc820ddf2014-07-29 14:38:51 -070050 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070051 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070052 if (Iter == End)
53 return NULL;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080054 return Iter;
Jan Voungc820ddf2014-07-29 14:38:51 -070055 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070056 CfgNode *getNode() const { return Node; }
57 bool atEnd() const { return Cur == End; }
58 InstList::iterator getCur() const { return Cur; }
Jim Stichnoth5d2fa0c2014-12-01 09:30:55 -080059 InstList::iterator getNext() const { return Next; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070060 InstList::iterator getEnd() const { return End; }
61 void insert(Inst *Inst);
Jan Vounge6e497d2014-07-30 10:06:03 -070062 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070063 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -070064 void advanceNext() { advanceForward(Next); }
Jim Stichnoth336f6c42014-10-30 15:01:31 -070065 void rewind();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070066 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
67
68private:
69 // Node is the argument to Inst::updateVars().
70 CfgNode *Node;
Jim Stichnoth98712a32014-10-24 10:59:02 -070071 Inst *LastInserted;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070072 // Cur points to the current instruction being considered. It is
73 // guaranteed to point to a non-deleted instruction, or to be End.
74 InstList::iterator Cur;
75 // Next doubles as a pointer to the next valid instruction (if any),
76 // and the new-instruction insertion point. It is also updated for
77 // the caller in case the lowering consumes more than one high-level
78 // instruction. It is guaranteed to point to a non-deleted
79 // instruction after Cur, or to be End. TODO: Consider separating
80 // the notion of "next valid instruction" and "new instruction
81 // insertion point", to avoid confusion when previously-deleted
82 // instructions come between the two points.
83 InstList::iterator Next;
Jan Vounge6e497d2014-07-30 10:06:03 -070084 // Begin is a copy of Insts.begin(), used if iterators are moved backward.
85 InstList::iterator Begin;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070086 // End is a copy of Insts.end(), used if Next needs to be advanced.
87 InstList::iterator End;
88
Jan Voungc820ddf2014-07-29 14:38:51 -070089 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -070090 void advanceForward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070091};
92
93class TargetLowering {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070094 TargetLowering(const TargetLowering &) = delete;
95 TargetLowering &operator=(const TargetLowering &) = delete;
96
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070097public:
98 static TargetLowering *createLowering(TargetArch Target, Cfg *Func);
Jan Voung8acded02014-09-22 18:02:25 -070099 static Assembler *createAssembler(TargetArch Target, Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700100 void translate() {
101 switch (Ctx->getOptLevel()) {
102 case Opt_m1:
103 translateOm1();
104 break;
105 case Opt_0:
106 translateO0();
107 break;
108 case Opt_1:
109 translateO1();
110 break;
111 case Opt_2:
112 translateO2();
113 break;
114 default:
115 Func->setError("Target doesn't specify lowering steps.");
116 break;
117 }
118 }
119 virtual void translateOm1() {
120 Func->setError("Target doesn't specify Om1 lowering steps.");
121 }
122 virtual void translateO0() {
123 Func->setError("Target doesn't specify O0 lowering steps.");
124 }
125 virtual void translateO1() {
126 Func->setError("Target doesn't specify O1 lowering steps.");
127 }
128 virtual void translateO2() {
129 Func->setError("Target doesn't specify O2 lowering steps.");
130 }
131
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700132 // Tries to do address mode optimization on a single instruction.
133 void doAddressOpt();
Matt Walac3302742014-08-15 16:21:56 -0700134 // Randomly insert NOPs.
135 void doNopInsertion();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700136 // Lowers a single non-Phi instruction.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700137 void lower();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700138 // Does preliminary lowering of the set of Phi instructions in the
139 // current node. The main intention is to do what's needed to keep
140 // the unlowered Phi instructions consistent with the lowered
141 // non-Phi instructions, e.g. to lower 64-bit operands on a 32-bit
142 // target.
143 virtual void prelowerPhis() {}
144 // Lowers a list of "parallel" assignment instructions representing
145 // a topological sort of the Phi instructions.
146 virtual void lowerPhiAssignments(CfgNode *Node,
147 const AssignList &Assignments) = 0;
Jim Stichnothff9c7062014-09-18 04:50:49 -0700148 // Tries to do branch optimization on a single instruction. Returns
149 // true if some optimization was done.
150 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
151 return false;
152 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700153
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700154 virtual SizeT getNumRegisters() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700155 // Returns a variable pre-colored to the specified physical
156 // register. This is generally used to get very direct access to
157 // the register such as in the prolog or epilog or for marking
Jim Stichnoth98712a32014-10-24 10:59:02 -0700158 // scratch registers as killed by a call. If a Type is not
159 // provided, a target-specific default type is used.
160 virtual Variable *getPhysicalRegister(SizeT RegNum,
161 Type Ty = IceType_void) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700162 // Returns a printable name for the register.
163 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
164
165 virtual bool hasFramePointer() const { return false; }
166 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700167 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
Jan Voungb17f61d2014-08-28 16:00:53 -0700168 virtual SizeT getBundleAlignLog2Bytes() const = 0;
Jim Stichnothf44f3712014-10-01 14:05:51 -0700169 virtual llvm::ArrayRef<AsmCodeByte> getNonExecBundlePadding() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700170 bool hasComputedFrame() const { return HasComputedFrame; }
Matt Walac3302742014-08-15 16:21:56 -0700171 bool shouldDoNopInsertion() const;
Jan Voung44d53e12014-09-11 19:18:03 -0700172 // Returns true if this function calls a function that has the
173 // "returns twice" attribute.
174 bool callsReturnsTwice() const { return CallsReturnsTwice; }
175 void setCallsReturnsTwice(bool RetTwice) {
176 CallsReturnsTwice = RetTwice;
177 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700178 int32_t getStackAdjustment() const { return StackAdjustment; }
179 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
180 void resetStackAdjustment() { StackAdjustment = 0; }
181 LoweringContext &getContext() { return Context; }
182
183 enum RegSet {
184 RegSet_None = 0,
185 RegSet_CallerSave = 1 << 0,
186 RegSet_CalleeSave = 1 << 1,
187 RegSet_StackPointer = 1 << 2,
188 RegSet_FramePointer = 1 << 3,
189 RegSet_All = ~RegSet_None
190 };
191 typedef uint32_t RegSetMask;
192
193 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
194 RegSetMask Exclude) const = 0;
195 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800196 void regAlloc(RegAllocKind Kind);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700197
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:
211 TargetLowering(Cfg *Func)
212 : Func(Func), Ctx(Func->getContext()), HasComputedFrame(false),
Jan Voung44d53e12014-09-11 19:18:03 -0700213 CallsReturnsTwice(false), StackAdjustment(0) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700214 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
215 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
216 virtual void lowerAssign(const InstAssign *Inst) = 0;
217 virtual void lowerBr(const InstBr *Inst) = 0;
218 virtual void lowerCall(const InstCall *Inst) = 0;
219 virtual void lowerCast(const InstCast *Inst) = 0;
220 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700221 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700222 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700223 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700224 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700225 virtual void lowerLoad(const InstLoad *Inst) = 0;
226 virtual void lowerPhi(const InstPhi *Inst) = 0;
227 virtual void lowerRet(const InstRet *Inst) = 0;
228 virtual void lowerSelect(const InstSelect *Inst) = 0;
229 virtual void lowerStore(const InstStore *Inst) = 0;
230 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
231 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
232
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700233 virtual void doAddressOptLoad() {}
234 virtual void doAddressOptStore() {}
Matt Walac3302742014-08-15 16:21:56 -0700235 virtual void randomlyInsertNop(float Probability) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700236 // This gives the target an opportunity to post-process the lowered
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800237 // expansion before returning.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700238 virtual void postLower() {}
239
240 Cfg *Func;
241 GlobalContext *Ctx;
242 bool HasComputedFrame;
Jan Voung44d53e12014-09-11 19:18:03 -0700243 bool CallsReturnsTwice;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700244 // StackAdjustment keeps track of the current stack offset from its
245 // natural location, as arguments are pushed for a function call.
246 int32_t StackAdjustment;
247 LoweringContext Context;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700248};
249
Jim Stichnothde4ca712014-06-29 08:13:48 -0700250// TargetGlobalInitLowering is used for "lowering" global
251// initializers. It is separated out from TargetLowering because it
252// does not require a Cfg.
253class TargetGlobalInitLowering {
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700254 TargetGlobalInitLowering(const TargetGlobalInitLowering &) = delete;
255 TargetGlobalInitLowering &operator=(const TargetGlobalInitLowering &) =
256 delete;
257
Jim Stichnothde4ca712014-06-29 08:13:48 -0700258public:
259 static TargetGlobalInitLowering *createLowering(TargetArch Target,
260 GlobalContext *Ctx);
Jan Voung839c4ce2014-07-28 15:19:43 -0700261 virtual ~TargetGlobalInitLowering();
262
Karl Schimpf9d98d792014-10-13 15:01:08 -0700263 virtual void lower(const VariableDeclaration &Var) = 0;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700264
265protected:
266 TargetGlobalInitLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
267 GlobalContext *Ctx;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700268};
269
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700270} // end of namespace Ice
271
272#endif // SUBZERO_SRC_ICETARGETLOWERING_H