blob: 35c15bc269d4b9933fbf85fd6bcbe739b74dc24b [file] [log] [blame]
Eugene Zelenkofa57bd02017-09-27 23:26:01 +00001//===- SelectionDAGBuilder.h - Selection-DAG building -----------*- C++ -*-===//
Dan Gohman575fad32008-09-03 16:12:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohman575fad32008-09-03 16:12:24 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This implements routines for translating from LLVM IR into SelectionDAG IR.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000013#ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H
14#define LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H
Dan Gohman575fad32008-09-03 16:12:24 +000015
Chandler Carruthd9903882015-01-14 11:23:27 +000016#include "StatepointLowering.h"
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000017#include "llvm/ADT/ArrayRef.h"
Dan Gohman575fad32008-09-03 16:12:24 +000018#include "llvm/ADT/DenseMap.h"
Yi Kong74b874a2019-03-26 12:18:08 +000019#include "llvm/ADT/MapVector.h"
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000020#include "llvm/ADT/SmallVector.h"
OCHyams1d1de742022-12-09 15:43:56 +000021#include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
Amara Emersoncfef1802021-09-25 22:55:37 -070022#include "llvm/CodeGen/CodeGenCommonISel.h"
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000023#include "llvm/CodeGen/ISDOpcodes.h"
Dan Gohman575fad32008-09-03 16:12:24 +000024#include "llvm/CodeGen/SelectionDAGNodes.h"
Amara Emerson829037a2019-06-08 00:05:17 +000025#include "llvm/CodeGen/SwitchLoweringUtils.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000026#include "llvm/CodeGen/TargetLowering.h"
Craig Topper2fa14362018-03-29 17:21:10 +000027#include "llvm/CodeGen/ValueTypes.h"
Nico Weber184ca392024-01-25 12:01:31 -050028#include "llvm/CodeGenTypes/MachineValueType.h"
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000029#include "llvm/IR/DebugLoc.h"
30#include "llvm/IR/Instruction.h"
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000031#include "llvm/Support/BranchProbability.h"
32#include "llvm/Support/CodeGen.h"
Torok Edwin56d06592009-07-11 20:10:48 +000033#include "llvm/Support/ErrorHandling.h"
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000034#include <algorithm>
35#include <cassert>
36#include <cstdint>
Krzysztof Parzyszek864aaa22022-12-01 16:18:32 -080037#include <optional>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000038#include <utility>
Dan Gohman575fad32008-09-03 16:12:24 +000039#include <vector>
Dan Gohman575fad32008-09-03 16:12:24 +000040
41namespace llvm {
42
Simon Pilgrimbee79cd2020-09-15 11:44:47 +010043class AAResults;
Dan Gohman575fad32008-09-03 16:12:24 +000044class AllocaInst;
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000045class AtomicCmpXchgInst;
46class AtomicRMWInst;
Matt Arsenaultbcb931c2022-09-19 15:25:37 -040047class AssumptionCache;
Dan Gohman575fad32008-09-03 16:12:24 +000048class BasicBlock;
Dan Gohman575fad32008-09-03 16:12:24 +000049class BranchInst;
50class CallInst;
Craig Topper784929d2019-02-08 20:48:56 +000051class CallBrInst;
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000052class CatchPadInst;
53class CatchReturnInst;
54class CatchSwitchInst;
55class CleanupPadInst;
56class CleanupReturnInst;
57class Constant;
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000058class ConstrainedFPIntrinsic;
Devang Patelb12ff592010-08-26 23:35:15 +000059class DbgValueInst;
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000060class DataLayout;
61class DIExpression;
62class DILocalVariable;
63class DILocation;
64class FenceInst;
Dan Gohmana3624b62009-11-23 17:16:22 +000065class FunctionLoweringInfo;
Dan Gohman575fad32008-09-03 16:12:24 +000066class GCFunctionInfo;
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000067class GCRelocateInst;
68class GCResultInst;
Simon Pilgrimbee79cd2020-09-15 11:44:47 +010069class GCStatepointInst;
Chris Lattnerd04cb6d2009-10-28 00:19:10 +000070class IndirectBrInst;
Dan Gohman575fad32008-09-03 16:12:24 +000071class InvokeInst;
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000072class LandingPadInst;
73class LLVMContext;
Dan Gohman575fad32008-09-03 16:12:24 +000074class LoadInst;
75class MachineBasicBlock;
Dan Gohman575fad32008-09-03 16:12:24 +000076class PHINode;
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000077class ResumeInst;
Dan Gohman575fad32008-09-03 16:12:24 +000078class ReturnInst;
Dale Johannesenbfd4fd72010-07-16 00:02:08 +000079class SDDbgValue;
Simon Pilgrim46de0d52020-04-19 12:38:41 +010080class SelectionDAG;
Dan Gohman575fad32008-09-03 16:12:24 +000081class StoreInst;
Tim Northover3d7a0572019-05-24 08:39:43 +000082class SwiftErrorValueTracking;
Dan Gohman575fad32008-09-03 16:12:24 +000083class SwitchInst;
Owen Andersonbb15fec2011-12-08 22:15:21 +000084class TargetLibraryInfo;
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000085class TargetMachine;
86class Type;
Dan Gohman575fad32008-09-03 16:12:24 +000087class VAArgInst;
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000088class UnreachableInst;
89class Use;
90class User;
91class Value;
Dan Gohman575fad32008-09-03 16:12:24 +000092
Dan Gohman575fad32008-09-03 16:12:24 +000093//===----------------------------------------------------------------------===//
Dan Gohman1a6c47f2009-11-23 18:04:58 +000094/// SelectionDAGBuilder - This is the common target-independent lowering
Dan Gohman575fad32008-09-03 16:12:24 +000095/// implementation that is parameterized by a TargetLowering object.
Dan Gohman575fad32008-09-03 16:12:24 +000096///
Benjamin Kramer079b96e2013-09-11 18:05:11 +000097class SelectionDAGBuilder {
Bjorn Petterssonecd09602019-02-12 22:11:20 +000098 /// The current instruction being visited.
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000099 const Instruction *CurInst = nullptr;
Dale Johannesendb7c5f62009-01-31 02:22:37 +0000100
Dan Gohman575fad32008-09-03 16:12:24 +0000101 DenseMap<const Value*, SDValue> NodeMap;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000102
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000103 /// Maps argument value for unused arguments. This is used
Devang Patelb0c76392010-06-01 19:59:01 +0000104 /// to preserve debug information for incoming arguments.
105 DenseMap<const Value*, SDValue> UnusedArgNodeMap;
Dan Gohman575fad32008-09-03 16:12:24 +0000106
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000107 /// Helper type for DanglingDebugInfoMap.
Dale Johannesenbfd4fd72010-07-16 00:02:08 +0000108 class DanglingDebugInfo {
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000109 unsigned SDNodeOrder = 0;
110
Dale Johannesenbfd4fd72010-07-16 00:02:08 +0000111 public:
Jeremy Morse44954852023-11-21 17:10:25 +0000112 DILocalVariable *Variable;
113 DIExpression *Expression;
114 DebugLoc dl;
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000115 DanglingDebugInfo() = default;
Jeremy Morse44954852023-11-21 17:10:25 +0000116 DanglingDebugInfo(DILocalVariable *Var, DIExpression *Expr, DebugLoc DL,
117 unsigned SDNO)
118 : SDNodeOrder(SDNO), Variable(Var), Expression(Expr),
119 dl(std::move(DL)) {}
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000120
Jeremy Morse44954852023-11-21 17:10:25 +0000121 DILocalVariable *getVariable() const { return Variable; }
122 DIExpression *getExpression() const { return Expression; }
123 DebugLoc getDebugLoc() const { return dl; }
OCHyamsa1ac6ef2022-11-16 10:10:24 +0000124 unsigned getSDNodeOrder() const { return SDNodeOrder; }
125
OCHyams1d1de742022-12-09 15:43:56 +0000126 /// Helper for printing DanglingDebugInfo. This hoop-jumping is to
Jeremy Morse44954852023-11-21 17:10:25 +0000127 /// store a Value pointer, so that we can print a whole DDI as one object.
OCHyams1d1de742022-12-09 15:43:56 +0000128 /// Call SelectionDAGBuilder::printDDI instead of using directly.
129 struct Print {
Jeremy Morse44954852023-11-21 17:10:25 +0000130 Print(const Value *V, const DanglingDebugInfo &DDI) : V(V), DDI(DDI) {}
131 const Value *V;
OCHyams1d1de742022-12-09 15:43:56 +0000132 const DanglingDebugInfo &DDI;
OCHyams1d1de742022-12-09 15:43:56 +0000133 friend raw_ostream &operator<<(raw_ostream &OS,
134 const DanglingDebugInfo::Print &P) {
Jeremy Morse44954852023-11-21 17:10:25 +0000135 OS << "DDI(var=" << *P.DDI.getVariable();
136 if (P.V)
137 OS << ", val=" << *P.V;
138 else
139 OS << ", val=nullptr";
140
141 OS << ", expr=" << *P.DDI.getExpression()
OCHyams1d1de742022-12-09 15:43:56 +0000142 << ", order=" << P.DDI.getSDNodeOrder()
143 << ", loc=" << P.DDI.getDebugLoc() << ")";
144 return OS;
145 }
146 };
Dale Johannesenbfd4fd72010-07-16 00:02:08 +0000147 };
148
OCHyams1d1de742022-12-09 15:43:56 +0000149 /// Returns an object that defines `raw_ostream &operator<<` for printing.
150 /// Usage example:
151 //// errs() << printDDI(MyDanglingInfo) << " is dangling\n";
Jeremy Morse44954852023-11-21 17:10:25 +0000152 DanglingDebugInfo::Print printDDI(const Value *V,
153 const DanglingDebugInfo &DDI) {
154 return DanglingDebugInfo::Print(V, DDI);
OCHyams1d1de742022-12-09 15:43:56 +0000155 }
156
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000157 /// Helper type for DanglingDebugInfoMap.
Bjorn Pettersson5c25f882018-03-21 09:44:34 +0000158 typedef std::vector<DanglingDebugInfo> DanglingDebugInfoVector;
159
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000160 /// Keeps track of dbg_values for which we have not yet seen the referent.
161 /// We defer handling these until we do see it.
Yi Kong74b874a2019-03-26 12:18:08 +0000162 MapVector<const Value*, DanglingDebugInfoVector> DanglingDebugInfoMap;
Dale Johannesenbfd4fd72010-07-16 00:02:08 +0000163
OCHyams06f28f22023-03-29 12:51:59 +0100164 /// Cache the module flag for whether we should use debug-info assignment
165 /// tracking.
166 bool AssignmentTrackingEnabled = false;
167
Chris Lattner1a32ede2009-12-24 00:37:38 +0000168public:
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000169 /// Loads are not emitted to the program immediately. We bunch them up and
170 /// then emit token factor nodes when possible. This allows us to get simple
171 /// disambiguation between loads without worrying about alias analysis.
Dan Gohman575fad32008-09-03 16:12:24 +0000172 SmallVector<SDValue, 8> PendingLoads;
Philip Reames1a1bdb22014-12-02 18:50:36 +0000173
174 /// State used while lowering a statepoint sequence (gc_statepoint,
175 /// gc_relocate, and gc_result). See StatepointLowering.hpp/cpp for details.
176 StatepointLoweringState StatepointLowering;
Dan Gohman575fad32008-09-03 16:12:24 +0000177
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000178private:
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000179 /// CopyToReg nodes that copy values to virtual registers for export to other
180 /// blocks need to be emitted before any terminator instruction, but they have
181 /// no other ordering requirements. We bunch them up and the emit a single
182 /// tokenfactor for them just before terminator instructions.
Dan Gohman575fad32008-09-03 16:12:24 +0000183 SmallVector<SDValue, 8> PendingExports;
184
Ulrich Weigand04a86962020-01-13 14:37:07 +0100185 /// Similar to loads, nodes corresponding to constrained FP intrinsics are
186 /// bunched up and emitted when necessary. These can be moved across each
187 /// other and any (normal) memory operation (load or store), but not across
188 /// calls or instructions having unspecified side effects. As a special
189 /// case, constrained FP intrinsics using fpexcept.strict may not be deleted
190 /// even if otherwise unused, so they need to be chained before any
191 /// terminator instruction (like PendingExports). We track the latter
192 /// set of nodes in a separate list.
193 SmallVector<SDValue, 8> PendingConstrainedFP;
194 SmallVector<SDValue, 8> PendingConstrainedFPStrict;
195
Ulrich Weigand81ee4842020-01-14 14:04:53 +0100196 /// Update root to include all chains from the Pending list.
197 SDValue updateRoot(SmallVectorImpl<SDValue> &Pending);
198
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000199 /// A unique monotonically increasing number used to order the SDNodes we
200 /// create.
Bill Wendling022d18f2009-12-18 23:32:53 +0000201 unsigned SDNodeOrder;
202
Hans Wennborg0867b152015-04-23 16:45:24 +0000203 /// Emit comparison and split W into two subtrees.
Amara Emerson829037a2019-06-08 00:05:17 +0000204 void splitWorkItem(SwitchCG::SwitchWorkList &WorkList,
205 const SwitchCG::SwitchWorkListItem &W, Value *Cond,
206 MachineBasicBlock *SwitchMBB);
Hans Wennborg0867b152015-04-23 16:45:24 +0000207
208 /// Lower W.
Amara Emerson829037a2019-06-08 00:05:17 +0000209 void lowerWorkItem(SwitchCG::SwitchWorkListItem W, Value *Cond,
Hans Wennborg0867b152015-04-23 16:45:24 +0000210 MachineBasicBlock *SwitchMBB,
211 MachineBasicBlock *DefaultMBB);
212
Rong Xu3573d8d2017-11-14 21:44:09 +0000213 /// Peel the top probability case if it exceeds the threshold
Amara Emerson829037a2019-06-08 00:05:17 +0000214 MachineBasicBlock *
215 peelDominantCaseCluster(const SwitchInst &SI,
216 SwitchCG::CaseClusterVector &Clusters,
217 BranchProbability &PeeledCaseProb);
Hans Wennborg0867b152015-04-23 16:45:24 +0000218
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000219private:
Dan Gohmanc3349602010-04-19 19:05:59 +0000220 const TargetMachine &TM;
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000221
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000222public:
Nico Rieckb5262d62014-01-12 14:09:17 +0000223 /// Lowest valid SDNodeOrder. The special case 0 is reserved for scheduling
224 /// nodes without a corresponding SDNode.
225 static const unsigned LowestSDNodeOrder = 1;
226
Dan Gohman575fad32008-09-03 16:12:24 +0000227 SelectionDAG &DAG;
Benjamin Maxwell77813812025-01-23 09:16:09 +0000228 BatchAAResults *BatchAA = nullptr;
Matt Arsenaultbcb931c2022-09-19 15:25:37 -0400229 AssumptionCache *AC = nullptr;
Akshay Khadse8bf7f862023-04-17 16:16:23 +0800230 const TargetLibraryInfo *LibInfo = nullptr;
Dan Gohman575fad32008-09-03 16:12:24 +0000231
Amara Emerson829037a2019-06-08 00:05:17 +0000232 class SDAGSwitchLowering : public SwitchCG::SwitchLowering {
233 public:
234 SDAGSwitchLowering(SelectionDAGBuilder *sdb, FunctionLoweringInfo &funcinfo)
235 : SwitchCG::SwitchLowering(funcinfo), SDB(sdb) {}
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000236
Kazu Hirata3f3930a2022-07-25 23:00:59 -0700237 void addSuccessorWithProb(
Amara Emerson829037a2019-06-08 00:05:17 +0000238 MachineBasicBlock *Src, MachineBasicBlock *Dst,
239 BranchProbability Prob = BranchProbability::getUnknown()) override {
240 SDB->addSuccessorWithProb(Src, Dst, Prob);
241 }
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000242
Amara Emerson829037a2019-06-08 00:05:17 +0000243 private:
Akshay Khadse8bf7f862023-04-17 16:16:23 +0800244 SelectionDAGBuilder *SDB = nullptr;
Amara Emerson829037a2019-06-08 00:05:17 +0000245 };
246
James Y Knighte9536792020-05-12 00:22:54 -0400247 // Data related to deferred switch lowerings. Used to construct additional
248 // Basic Blocks in SelectionDAGISel::FinishBasicBlock.
Amara Emerson829037a2019-06-08 00:05:17 +0000249 std::unique_ptr<SDAGSwitchLowering> SL;
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000250
Michael Gottesmanb27f0f12013-08-20 07:00:16 +0000251 /// A StackProtectorDescriptor structure used to communicate stack protector
252 /// information in between SelectBasicBlock and FinishBasicBlock.
253 StackProtectorDescriptor SPDescriptor;
Evan Cheng270d0f92009-09-18 21:02:19 +0000254
Dan Gohman575fad32008-09-03 16:12:24 +0000255 // Emit PHI-node-operand constants only once even if used by multiple
256 // PHI nodes.
Craig Topper7bd2be42025-03-02 16:16:19 -0800257 DenseMap<const Constant *, Register> ConstantsOut;
Dan Gohman575fad32008-09-03 16:12:24 +0000258
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000259 /// Information about the function as a whole.
Dan Gohman575fad32008-09-03 16:12:24 +0000260 FunctionLoweringInfo &FuncInfo;
Bill Wendling19e0a5b2009-02-19 21:12:54 +0000261
Tim Northover3d7a0572019-05-24 08:39:43 +0000262 /// Information about the swifterror values used throughout the function.
263 SwiftErrorValueTracking &SwiftError;
264
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000265 /// Garbage collection metadata for the function.
Akshay Khadse8bf7f862023-04-17 16:16:23 +0800266 GCFunctionInfo *GFI = nullptr;
Dan Gohman575fad32008-09-03 16:12:24 +0000267
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000268 /// Map a landing pad to the call site indexes.
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000269 DenseMap<MachineBasicBlock *, SmallVector<unsigned, 4>> LPadToCallSiteMap;
Bill Wendling3d11aa72011-10-04 22:00:35 +0000270
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000271 /// This is set to true if a call in the current block has been translated as
272 /// a tail call. In this case, no subsequent DAG nodes should be created.
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000273 bool HasTailCall = false;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000274
Akshay Khadse8bf7f862023-04-17 16:16:23 +0800275 LLVMContext *Context = nullptr;
Owen Anderson53a52212009-07-13 04:09:18 +0000276
Dan Gohmanc3349602010-04-19 19:05:59 +0000277 SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo,
Arthur Eubanks0a1aa6c2023-09-14 14:10:14 -0700278 SwiftErrorValueTracking &swifterror, CodeGenOptLevel ol)
Tim Northover3d7a0572019-05-24 08:39:43 +0000279 : SDNodeOrder(LowestSDNodeOrder), TM(dag.getTarget()), DAG(dag),
Arthur Eubanks0a1aa6c2023-09-14 14:10:14 -0700280 SL(std::make_unique<SDAGSwitchLowering>(this, funcinfo)),
281 FuncInfo(funcinfo), SwiftError(swifterror) {}
Dan Gohman575fad32008-09-03 16:12:24 +0000282
Benjamin Maxwell77813812025-01-23 09:16:09 +0000283 void init(GCFunctionInfo *gfi, BatchAAResults *BatchAA, AssumptionCache *AC,
Owen Andersonbb15fec2011-12-08 22:15:21 +0000284 const TargetLibraryInfo *li);
Dan Gohman575fad32008-09-03 16:12:24 +0000285
Sanjay Patel209b0f92017-03-02 20:48:08 +0000286 /// Clear out the current SelectionDAG and the associated state and prepare
287 /// this SelectionDAGBuilder object to be used for a new block. This doesn't
288 /// clear out information about additional blocks that are needed to complete
289 /// switch lowering or PHI node updating; that information is cleared out as
290 /// it is consumed.
Dan Gohman575fad32008-09-03 16:12:24 +0000291 void clear();
292
Sanjay Patel209b0f92017-03-02 20:48:08 +0000293 /// Clear the dangling debug information map. This function is separated from
294 /// the clear so that debug information that is dangling in a basic block can
295 /// be properly resolved in a different basic block. This allows the
296 /// SelectionDAG to resolve dangling debug information attached to PHI nodes.
Devang Patel799288382011-05-23 17:44:13 +0000297 void clearDanglingDebugInfo();
298
Sanjay Patel209b0f92017-03-02 20:48:08 +0000299 /// Return the current virtual root of the Selection DAG, flushing any
300 /// PendingLoad items. This must be done before emitting a store or any other
Ulrich Weigand04a86962020-01-13 14:37:07 +0100301 /// memory node that may need to be ordered after any prior load instructions.
302 SDValue getMemoryRoot();
303
304 /// Similar to getMemoryRoot, but also flushes PendingConstrainedFP(Strict)
305 /// items. This must be done before emitting any call other any other node
306 /// that may need to be ordered after FP instructions due to other side
307 /// effects.
Dan Gohman575fad32008-09-03 16:12:24 +0000308 SDValue getRoot();
309
Sanjay Patel209b0f92017-03-02 20:48:08 +0000310 /// Similar to getRoot, but instead of flushing all the PendingLoad items,
Ulrich Weigand04a86962020-01-13 14:37:07 +0100311 /// flush all the PendingExports (and PendingConstrainedFPStrict) items.
312 /// It is necessary to do this before emitting a terminator instruction.
Dan Gohman575fad32008-09-03 16:12:24 +0000313 SDValue getControlRoot();
314
Andrew Trick175143b2013-05-25 02:20:36 +0000315 SDLoc getCurSDLoc() const {
Andrew Trick175143b2013-05-25 02:20:36 +0000316 return SDLoc(CurInst, SDNodeOrder);
317 }
318
319 DebugLoc getCurDebugLoc() const {
320 return CurInst ? CurInst->getDebugLoc() : DebugLoc();
321 }
Devang Patelf3292b22011-02-21 23:21:26 +0000322
Craig Topper7bd2be42025-03-02 16:16:19 -0800323 void CopyValueToVirtualRegister(const Value *V, Register Reg,
Craig Topper85eae452022-03-30 11:24:01 -0700324 ISD::NodeType ExtendType = ISD::ANY_EXTEND);
Dan Gohman575fad32008-09-03 16:12:24 +0000325
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000326 void visit(const Instruction &I);
Jeremy Morse44954852023-11-21 17:10:25 +0000327 void visitDbgInfo(const Instruction &I);
Dan Gohman575fad32008-09-03 16:12:24 +0000328
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000329 void visit(unsigned Opcode, const User &I);
Dan Gohman575fad32008-09-03 16:12:24 +0000330
Bjorn Pettersson350352c2019-02-06 17:36:18 +0000331 /// If there was virtual register allocated for the value V emit CopyFromReg
332 /// of the specified type Ty. Return empty SDValue() otherwise.
Igor Laevsky85f7f722015-03-10 16:26:48 +0000333 SDValue getCopyFromRegs(const Value *V, Type *Ty);
334
gbtozers93b170e2020-09-29 15:43:21 +0100335 /// Register a dbg_value which relies on a Value which we have not yet seen.
Jeremy Morse44954852023-11-21 17:10:25 +0000336 void addDanglingDebugInfo(SmallVectorImpl<Value *> &Values,
337 DILocalVariable *Var, DIExpression *Expr,
338 bool IsVariadic, DebugLoc DL, unsigned Order);
gbtozers93b170e2020-09-29 15:43:21 +0100339
Bjorn Petterssona223f8152018-03-12 18:02:39 +0000340 /// If we have dangling debug info that describes \p Variable, or an
341 /// overlapping part of variable considering the \p Expr, then this method
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000342 /// will drop that debug info as it isn't valid any longer.
Bjorn Petterssona223f8152018-03-12 18:02:39 +0000343 void dropDanglingDebugInfo(const DILocalVariable *Variable,
344 const DIExpression *Expr);
345
Jeremy Morse291713a2019-02-13 16:33:05 +0000346 /// If we saw an earlier dbg_value referring to V, generate the debug data
347 /// structures now that we've seen its definition.
Dale Johannesenbfd4fd72010-07-16 00:02:08 +0000348 void resolveDanglingDebugInfo(const Value *V, SDValue Val);
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000349
Jeremy Morse291713a2019-02-13 16:33:05 +0000350 /// For the given dangling debuginfo record, perform last-ditch efforts to
351 /// resolve the debuginfo to something that is represented in this DAG. If
352 /// this cannot be done, produce an Undef debug value record.
Jeremy Morse44954852023-11-21 17:10:25 +0000353 void salvageUnresolvedDbgValue(const Value *V, DanglingDebugInfo &DDI);
Jeremy Morse291713a2019-02-13 16:33:05 +0000354
gbtozers93b170e2020-09-29 15:43:21 +0100355 /// For a given list of Values, attempt to create and record a SDDbgValue in
356 /// the SelectionDAG.
357 bool handleDebugValue(ArrayRef<const Value *> Values, DILocalVariable *Var,
OCHyams97927442022-11-16 09:59:35 +0000358 DIExpression *Expr, DebugLoc DbgLoc, unsigned Order,
359 bool IsVariadic);
Jeremy Morse6d3cd3b2019-02-13 15:53:10 +0000360
OCHyams2b3c13b2023-04-25 17:19:44 +0100361 /// Create a record for a kill location debug intrinsic.
362 void handleKillDebugValue(DILocalVariable *Var, DIExpression *Expr,
363 DebugLoc DbgLoc, unsigned Order);
364
Orlando Cazalet-Hyams5ee08812023-12-12 11:32:19 +0000365 void handleDebugDeclare(Value *Address, DILocalVariable *Variable,
366 DIExpression *Expression, DebugLoc DL);
367
Jeremy Morse291713a2019-02-13 16:33:05 +0000368 /// Evict any dangling debug information, attempting to salvage it first.
369 void resolveOrClearDbgInfo();
370
Dan Gohman575fad32008-09-03 16:12:24 +0000371 SDValue getValue(const Value *V);
Elena Demikhovsky584ce372015-04-28 07:57:37 +0000372
Dan Gohmand4322232010-07-01 01:59:43 +0000373 SDValue getNonRegisterValue(const Value *V);
374 SDValue getValueImpl(const Value *V);
Dan Gohman575fad32008-09-03 16:12:24 +0000375
376 void setValue(const Value *V, SDValue NewN) {
377 SDValue &N = NodeMap[V];
Craig Topperada08572014-04-16 04:21:27 +0000378 assert(!N.getNode() && "Already set a value for this node!");
Dan Gohman575fad32008-09-03 16:12:24 +0000379 N = NewN;
380 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000381
Devang Patelb0c76392010-06-01 19:59:01 +0000382 void setUnusedArgValue(const Value *V, SDValue NewN) {
383 SDValue &N = UnusedArgNodeMap[V];
Craig Topperada08572014-04-16 04:21:27 +0000384 assert(!N.getNode() && "Already set a value for this node!");
Devang Patelb0c76392010-06-01 19:59:01 +0000385 N = NewN;
386 }
Dan Gohman575fad32008-09-03 16:12:24 +0000387
Noah Goldsteina4951ec2023-09-29 14:27:36 -0500388 bool shouldKeepJumpConditionsTogether(
389 const FunctionLoweringInfo &FuncInfo, const BranchInst &I,
390 Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs,
391 TargetLoweringBase::CondMergingParams Params) const;
392
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000393 void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB,
Dan Gohman575fad32008-09-03 16:12:24 +0000394 MachineBasicBlock *FBB, MachineBasicBlock *CurBB,
Pete Cooper69234612015-07-15 01:31:26 +0000395 MachineBasicBlock *SwitchBB,
Fangrui Songcb0bab82018-07-16 18:51:40 +0000396 Instruction::BinaryOps Opc, BranchProbability TProb,
397 BranchProbability FProb, bool InvertCond);
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000398 void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB,
Dan Gohmand01ddb52008-10-17 21:16:08 +0000399 MachineBasicBlock *FBB,
Dan Gohman7c0303a2010-04-19 22:41:47 +0000400 MachineBasicBlock *CurBB,
Manman Ren4ece7452014-01-31 00:42:44 +0000401 MachineBasicBlock *SwitchBB,
Fangrui Songcb0bab82018-07-16 18:51:40 +0000402 BranchProbability TProb, BranchProbability FProb,
Geoff Berry92a286a2017-01-24 16:36:07 +0000403 bool InvertCond);
Amara Emerson829037a2019-06-08 00:05:17 +0000404 bool ShouldEmitAsBranches(const std::vector<SwitchCG::CaseBlock> &Cases);
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000405 bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB);
406 void CopyToExportRegsIfNeeded(const Value *V);
407 void ExportFromCurrentBlock(const Value *V);
Craig Topper113f37a2020-04-13 10:44:59 -0700408 void LowerCallTo(const CallBase &CB, SDValue Callee, bool IsTailCall,
Ahmed Bougachacc548ec2024-05-31 14:08:10 -0700409 bool IsMustTailCall, const BasicBlock *EHPadBB = nullptr,
410 const TargetLowering::PtrAuthInfo *PAI = nullptr);
Dan Gohman575fad32008-09-03 16:12:24 +0000411
Matt Arsenault2bba7792016-02-08 16:28:19 +0000412 // Lower range metadata from 0 to N to assert zext to an integer of nearest
413 // floor power of two.
414 SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I,
415 SDValue Op);
416
Sanjoy Das19c61592016-03-16 20:49:31 +0000417 void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI,
Chandler Carruth31607342019-02-11 07:42:30 +0000418 const CallBase *Call, unsigned ArgIdx,
Sanjoy Das19c61592016-03-16 20:49:31 +0000419 unsigned NumArgs, SDValue Callee,
Markus Böck0ad92c02023-10-14 18:38:18 +0200420 Type *ReturnTy, AttributeSet RetAttrs,
421 bool IsPatchPoint);
Sanjoy Das19c61592016-03-16 20:49:31 +0000422
423 std::pair<SDValue, SDValue>
424 lowerInvokable(TargetLowering::CallLoweringInfo &CLI,
425 const BasicBlock *EHPadBB = nullptr);
Andrew Trick74f4c742013-10-31 17:18:24 +0000426
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000427 /// When an MBB was split during scheduling, update the
Alp Toker798060e2014-01-11 14:01:43 +0000428 /// references that need to refer to the last resulting block.
Jakob Stoklund Olesen665aa6e2010-09-30 19:44:31 +0000429 void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last);
430
Sanjoy Das70697ff2016-03-16 23:08:00 +0000431 /// Describes a gc.statepoint or a gc.statepoint like thing for the purposes
Sanjoy Dasa5b29722016-03-23 02:28:35 +0000432 /// of lowering into a STATEPOINT node.
Sanjoy Das70697ff2016-03-16 23:08:00 +0000433 struct StatepointLoweringInfo {
434 /// Bases[i] is the base pointer for Ptrs[i]. Together they denote the set
435 /// of gc pointers this STATEPOINT has to relocate.
Sanjoy Dase58ca592016-03-23 02:24:07 +0000436 SmallVector<const Value *, 16> Bases;
437 SmallVector<const Value *, 16> Ptrs;
Sanjoy Das70697ff2016-03-16 23:08:00 +0000438
439 /// The set of gc.relocate calls associated with this gc.statepoint.
Sanjoy Dase58ca592016-03-23 02:24:07 +0000440 SmallVector<const GCRelocateInst *, 16> GCRelocates;
Sanjoy Das70697ff2016-03-16 23:08:00 +0000441
Philip Reames824cffe2024-08-26 14:05:17 -0700442 /// The full list of gc-live arguments to the gc.statepoint being lowered.
443 ArrayRef<const Use> GCLives;
Sanjoy Das70697ff2016-03-16 23:08:00 +0000444
445 /// The gc.statepoint instruction.
446 const Instruction *StatepointInstr = nullptr;
447
448 /// The list of gc transition arguments present in the gc.statepoint being
449 /// lowered.
450 ArrayRef<const Use> GCTransitionArgs;
451
452 /// The ID that the resulting STATEPOINT instruction has to report.
Il-Capitano0ef74372024-04-02 15:28:19 +0200453 uint64_t ID = -1;
Sanjoy Das70697ff2016-03-16 23:08:00 +0000454
455 /// Information regarding the underlying call instruction.
456 TargetLowering::CallLoweringInfo CLI;
457
458 /// The deoptimization state associated with this gc.statepoint call, if
459 /// any.
460 ArrayRef<const Use> DeoptState;
461
462 /// Flags associated with the meta arguments being lowered.
463 uint64_t StatepointFlags = -1;
464
465 /// The number of patchable bytes the call needs to get lowered into.
466 unsigned NumPatchBytes = -1;
467
468 /// The exception handling unwind destination, in case this represents an
469 /// invoke of gc.statepoint.
470 const BasicBlock *EHPadBB = nullptr;
471
472 explicit StatepointLoweringInfo(SelectionDAG &DAG) : CLI(DAG) {}
473 };
474
475 /// Lower \p SLI into a STATEPOINT instruction.
Fangrui Songcb0bab82018-07-16 18:51:40 +0000476 SDValue LowerAsSTATEPOINT(StatepointLoweringInfo &SI);
Sanjoy Das70697ff2016-03-16 23:08:00 +0000477
Igor Laevsky7fc58a42015-02-20 15:28:35 +0000478 // This function is responsible for the whole statepoint lowering process.
Igor Laevsky85f7f722015-03-10 16:26:48 +0000479 // It uniformly handles invoke and call statepoints.
Philip Reames98a87c62020-05-27 18:56:50 -0700480 void LowerStatepoint(const GCStatepointInst &I,
Reid Kleckner51189f0a2015-09-08 23:28:38 +0000481 const BasicBlock *EHPadBB = nullptr);
Sanjoy Das38bfc222016-03-22 00:59:13 +0000482
Chandler Carruth31607342019-02-11 07:42:30 +0000483 void LowerCallSiteWithDeoptBundle(const CallBase *Call, SDValue Callee,
Sanjoy Das38bfc222016-03-22 00:59:13 +0000484 const BasicBlock *EHPadBB);
485
Sanjoy Dasdf9ae702016-03-24 20:23:29 +0000486 void LowerDeoptimizeCall(const CallInst *CI);
Sanjoy Das65a60672016-04-06 01:33:49 +0000487 void LowerDeoptimizingReturn();
Sanjoy Dasdf9ae702016-03-24 20:23:29 +0000488
Chandler Carruth31607342019-02-11 07:42:30 +0000489 void LowerCallSiteWithDeoptBundleImpl(const CallBase *Call, SDValue Callee,
Sanjoy Dasfd3eaa82016-03-24 22:51:49 +0000490 const BasicBlock *EHPadBB,
Sanjoy Das65a60672016-04-06 01:33:49 +0000491 bool VarArgDisallowed,
492 bool ForceVoidReturnTy);
Sanjoy Dasfd3eaa82016-03-24 22:51:49 +0000493
Ahmed Bougachacc548ec2024-05-31 14:08:10 -0700494 void LowerCallSiteWithPtrAuthBundle(const CallBase &CB,
495 const BasicBlock *EHPadBB);
496
Sanjoy Das40c32dd2017-04-27 17:17:16 +0000497 /// Returns the type of FrameIndex and TargetFrameIndex nodes.
498 MVT getFrameIndexTy() {
499 return DAG.getTargetLoweringInfo().getFrameIndexTy(DAG.getDataLayout());
500 }
501
Dan Gohman575fad32008-09-03 16:12:24 +0000502private:
503 // Terminator instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000504 void visitRet(const ReturnInst &I);
505 void visitBr(const BranchInst &I);
506 void visitSwitch(const SwitchInst &I);
507 void visitIndirectBr(const IndirectBrInst &I);
Yaron Kerend7ba46b2014-04-19 13:47:43 +0000508 void visitUnreachable(const UnreachableInst &I);
David Majnemer654e1302015-07-31 17:58:14 +0000509 void visitCleanupRet(const CleanupReturnInst &I);
David Majnemer8a1c45d2015-12-12 05:38:55 +0000510 void visitCatchSwitch(const CatchSwitchInst &I);
David Majnemer654e1302015-07-31 17:58:14 +0000511 void visitCatchRet(const CatchReturnInst &I);
512 void visitCatchPad(const CatchPadInst &I);
David Majnemer654e1302015-07-31 17:58:14 +0000513 void visitCleanupPad(const CleanupPadInst &CPI);
Dan Gohman575fad32008-09-03 16:12:24 +0000514
Cong Hou1938f2e2015-11-24 08:51:23 +0000515 BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
516 const MachineBasicBlock *Dst) const;
517 void addSuccessorWithProb(
518 MachineBasicBlock *Src, MachineBasicBlock *Dst,
519 BranchProbability Prob = BranchProbability::getUnknown());
520
Dan Gohman575fad32008-09-03 16:12:24 +0000521public:
Amara Emerson829037a2019-06-08 00:05:17 +0000522 void visitSwitchCase(SwitchCG::CaseBlock &CB, MachineBasicBlock *SwitchBB);
Michael Gottesmanb27f0f12013-08-20 07:00:16 +0000523 void visitSPDescriptorParent(StackProtectorDescriptor &SPD,
524 MachineBasicBlock *ParentBB);
525 void visitSPDescriptorFailure(StackProtectorDescriptor &SPD);
Amara Emerson829037a2019-06-08 00:05:17 +0000526 void visitBitTestHeader(SwitchCG::BitTestBlock &B,
527 MachineBasicBlock *SwitchBB);
528 void visitBitTestCase(SwitchCG::BitTestBlock &BB, MachineBasicBlock *NextMBB,
Craig Topper292ee932024-09-18 09:43:21 -0700529 BranchProbability BranchProbToNext, Register Reg,
Amara Emerson829037a2019-06-08 00:05:17 +0000530 SwitchCG::BitTestCase &B, MachineBasicBlock *SwitchBB);
531 void visitJumpTable(SwitchCG::JumpTable &JT);
532 void visitJumpTableHeader(SwitchCG::JumpTable &JT,
533 SwitchCG::JumpTableHeader &JTH,
Dan Gohman7c0303a2010-04-19 22:41:47 +0000534 MachineBasicBlock *SwitchBB);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000535
Dan Gohman575fad32008-09-03 16:12:24 +0000536private:
537 // These all get lowered before this pass.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000538 void visitInvoke(const InvokeInst &I);
Craig Topper784929d2019-02-08 20:48:56 +0000539 void visitCallBr(const CallBrInst &I);
Nick Desaulniers5cc10162023-02-16 17:47:37 -0800540 void visitCallBrLandingPad(const CallInst &I);
Bill Wendlingf891bf82011-07-31 06:30:59 +0000541 void visitResume(const ResumeInst &I);
Dan Gohman575fad32008-09-03 16:12:24 +0000542
Cameron McInallycbde0d92018-11-13 18:15:47 +0000543 void visitUnary(const User &I, unsigned Opcode);
544 void visitFNeg(const User &I) { visitUnary(I, ISD::FNEG); }
545
Sanjay Patelc4e4c5b2018-05-11 22:45:22 +0000546 void visitBinary(const User &I, unsigned Opcode);
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000547 void visitShift(const User &I, unsigned Opcode);
548 void visitAdd(const User &I) { visitBinary(I, ISD::ADD); }
549 void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); }
550 void visitSub(const User &I) { visitBinary(I, ISD::SUB); }
Cameron McInally31c7a2f2020-08-03 10:19:33 -0500551 void visitFSub(const User &I) { visitBinary(I, ISD::FSUB); }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000552 void visitMul(const User &I) { visitBinary(I, ISD::MUL); }
553 void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); }
554 void visitURem(const User &I) { visitBinary(I, ISD::UREM); }
555 void visitSRem(const User &I) { visitBinary(I, ISD::SREM); }
556 void visitFRem(const User &I) { visitBinary(I, ISD::FREM); }
557 void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); }
Benjamin Kramer9960a252011-07-08 10:31:30 +0000558 void visitSDiv(const User &I);
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000559 void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); }
560 void visitAnd (const User &I) { visitBinary(I, ISD::AND); }
561 void visitOr (const User &I) { visitBinary(I, ISD::OR); }
562 void visitXor (const User &I) { visitBinary(I, ISD::XOR); }
563 void visitShl (const User &I) { visitShift(I, ISD::SHL); }
564 void visitLShr(const User &I) { visitShift(I, ISD::SRL); }
565 void visitAShr(const User &I) { visitShift(I, ISD::SRA); }
Nikita Popovdeab4512024-06-04 08:31:03 +0200566 void visitICmp(const ICmpInst &I);
567 void visitFCmp(const FCmpInst &I);
Dan Gohman575fad32008-09-03 16:12:24 +0000568 // Visit the conversion instructions
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000569 void visitTrunc(const User &I);
570 void visitZExt(const User &I);
571 void visitSExt(const User &I);
572 void visitFPTrunc(const User &I);
573 void visitFPExt(const User &I);
574 void visitFPToUI(const User &I);
575 void visitFPToSI(const User &I);
576 void visitUIToFP(const User &I);
577 void visitSIToFP(const User &I);
578 void visitPtrToInt(const User &I);
579 void visitIntToPtr(const User &I);
580 void visitBitCast(const User &I);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000581 void visitAddrSpaceCast(const User &I);
Dan Gohman575fad32008-09-03 16:12:24 +0000582
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000583 void visitExtractElement(const User &I);
584 void visitInsertElement(const User &I);
585 void visitShuffleVector(const User &I);
Dan Gohman575fad32008-09-03 16:12:24 +0000586
Nikita Popov16033ffd2022-06-29 10:42:46 +0200587 void visitExtractValue(const ExtractValueInst &I);
Nikita Popov7283f482022-06-28 11:25:54 +0200588 void visitInsertValue(const InsertValueInst &I);
Fangrui Songcb0bab82018-07-16 18:51:40 +0000589 void visitLandingPad(const LandingPadInst &LP);
Dan Gohman575fad32008-09-03 16:12:24 +0000590
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000591 void visitGetElementPtr(const User &I);
592 void visitSelect(const User &I);
Dan Gohman575fad32008-09-03 16:12:24 +0000593
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000594 void visitAlloca(const AllocaInst &I);
595 void visitLoad(const LoadInst &I);
596 void visitStore(const StoreInst &I);
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000597 void visitMaskedLoad(const CallInst &I, bool IsExpanding = false);
598 void visitMaskedStore(const CallInst &I, bool IsCompressing = false);
Elena Demikhovsky584ce372015-04-28 07:57:37 +0000599 void visitMaskedGather(const CallInst &I);
600 void visitMaskedScatter(const CallInst &I);
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000601 void visitAtomicCmpXchg(const AtomicCmpXchgInst &I);
602 void visitAtomicRMW(const AtomicRMWInst &I);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000603 void visitFence(const FenceInst &I);
Dan Gohmanf41ad472010-04-20 15:00:41 +0000604 void visitPHI(const PHINode &I);
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000605 void visitCall(const CallInst &I);
Dávid Bolvanský179e15d2020-09-25 17:54:21 +0200606 bool visitMemCmpBCmpCall(const CallInst &I);
Andrew Kaylorb99d1cc2016-07-29 18:23:18 +0000607 bool visitMemPCpyCall(const CallInst &I);
Richard Sandiford6f6d5512013-08-20 09:38:48 +0000608 bool visitMemChrCall(const CallInst &I);
Richard Sandifordbb83a502013-08-16 11:29:37 +0000609 bool visitStrCpyCall(const CallInst &I, bool isStpcpy);
Richard Sandifordca232712013-08-16 11:21:54 +0000610 bool visitStrCmpCall(const CallInst &I);
Richard Sandiford0dec06a2013-08-16 11:41:43 +0000611 bool visitStrLenCall(const CallInst &I);
612 bool visitStrNLenCall(const CallInst &I);
Bob Wilson874886c2012-08-03 23:29:17 +0000613 bool visitUnaryFloatCall(const CallInst &I, unsigned Opcode);
Matt Arsenault7c936902014-10-21 23:01:01 +0000614 bool visitBinaryFloatCall(const CallInst &I, unsigned Opcode);
Eli Friedman342e8df2011-08-24 20:50:09 +0000615 void visitAtomicLoad(const LoadInst &I);
616 void visitAtomicStore(const StoreInst &I);
Manman Rene221a872016-04-05 18:13:16 +0000617 void visitLoadFromSwiftError(const LoadInst &I);
618 void visitStoreToSwiftError(const StoreInst &I);
aqjunee87d7162019-11-07 01:17:49 +0900619 void visitFreeze(const FreezeInst &I);
Eli Friedman342e8df2011-08-24 20:50:09 +0000620
cynecx8ec9fd42021-05-13 19:05:11 +0100621 void visitInlineAsm(const CallBase &Call,
622 const BasicBlock *EHPadBB = nullptr);
Felipe de Azevedo Piovezan35f4ef12023-08-23 14:14:32 -0400623
Jeremy Morse8c1b7fb2024-01-22 15:39:35 +0000624 bool visitEntryValueDbgValue(ArrayRef<const Value *> Values,
625 DILocalVariable *Variable, DIExpression *Expr,
626 DebugLoc DbgLoc);
Guillaume Chatelete386a012019-05-20 11:01:30 +0000627 void visitIntrinsicCall(const CallInst &I, unsigned Intrinsic);
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000628 void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic);
Andrew Kaylorf4660012017-05-25 21:31:00 +0000629 void visitConstrainedFPIntrinsic(const ConstrainedFPIntrinsic &FPI);
Sameer Sahasrabuddhe60822632024-03-06 12:17:37 +0530630 void visitConvergenceControl(const CallInst &I, unsigned Intrinsic);
Graham Hunterfbb37e92024-05-13 11:35:28 +0100631 void visitVectorHistogram(const CallInst &I, unsigned IntrinsicID);
Graham Huntered5aadd2024-11-14 17:48:43 +0000632 void visitVectorExtractLastActive(const CallInst &I, unsigned Intrinsic);
Philip Reames143f3bf2022-09-21 09:04:42 -0700633 void visitVPLoad(const VPIntrinsic &VPIntrin, EVT VT,
Craig Topperbb64fd572023-04-03 13:59:55 -0700634 const SmallVectorImpl<SDValue> &OpValues);
Philip Reames143f3bf2022-09-21 09:04:42 -0700635 void visitVPStore(const VPIntrinsic &VPIntrin,
Craig Topperbb64fd572023-04-03 13:59:55 -0700636 const SmallVectorImpl<SDValue> &OpValues);
Philip Reames143f3bf2022-09-21 09:04:42 -0700637 void visitVPGather(const VPIntrinsic &VPIntrin, EVT VT,
Craig Topperbb64fd572023-04-03 13:59:55 -0700638 const SmallVectorImpl<SDValue> &OpValues);
Philip Reames143f3bf2022-09-21 09:04:42 -0700639 void visitVPScatter(const VPIntrinsic &VPIntrin,
Craig Topperbb64fd572023-04-03 13:59:55 -0700640 const SmallVectorImpl<SDValue> &OpValues);
Lorenzo Albano28cfa762022-03-10 16:39:12 +0100641 void visitVPStridedLoad(const VPIntrinsic &VPIntrin, EVT VT,
Craig Topperbb64fd572023-04-03 13:59:55 -0700642 const SmallVectorImpl<SDValue> &OpValues);
Lorenzo Albano28cfa762022-03-10 16:39:12 +0100643 void visitVPStridedStore(const VPIntrinsic &VPIntrin,
Craig Topperbb64fd572023-04-03 13:59:55 -0700644 const SmallVectorImpl<SDValue> &OpValues);
Fraser Cormack6be5e872022-03-30 17:01:30 +0100645 void visitVPCmp(const VPCmpIntrinsic &VPIntrin);
Simon Moll3ffbc792020-12-09 11:36:30 +0100646 void visitVectorPredicationIntrinsic(const VPIntrinsic &VPIntrin);
Dan Gohman575fad32008-09-03 16:12:24 +0000647
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000648 void visitVAStart(const CallInst &I);
649 void visitVAArg(const VAArgInst &I);
650 void visitVAEnd(const CallInst &I);
651 void visitVACopy(const CallInst &I);
Andrew Trick74f4c742013-10-31 17:18:24 +0000652 void visitStackmap(const CallInst &I);
Craig Topper806763e2020-04-10 23:12:07 -0700653 void visitPatchpoint(const CallBase &CB, const BasicBlock *EHPadBB = nullptr);
Dan Gohman575fad32008-09-03 16:12:24 +0000654
Sanjoy Das3a020192016-03-17 00:47:14 +0000655 // These two are implemented in StatepointLowering.cpp
Fangrui Songcb0bab82018-07-16 18:51:40 +0000656 void visitGCRelocate(const GCRelocateInst &Relocate);
Philip Reames92d1f0c2016-04-12 18:05:10 +0000657 void visitGCResult(const GCResultInst &I);
Philip Reames1a1bdb22014-12-02 18:50:36 +0000658
Amara Emersoncf9daa32017-05-09 10:43:25 +0000659 void visitVectorReduce(const CallInst &I, unsigned Intrinsic);
Caroline Concatto2d728bb2021-01-15 16:46:42 +0000660 void visitVectorReverse(const CallInst &I);
Cullen Rhodes2750f3ed2021-01-08 14:06:13 +0000661 void visitVectorSplice(const CallInst &I);
Min-Yih Hsu5a1e16f2025-02-05 15:30:33 -0800662 void visitVectorInterleave(const CallInst &I, unsigned Factor);
663 void visitVectorDeinterleave(const CallInst &I, unsigned Factor);
David Sherwood748ae522021-02-08 15:46:24 +0000664 void visitStepVector(const CallInst &I);
Amara Emersoncf9daa32017-05-09 10:43:25 +0000665
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000666 void visitUserOp1(const Instruction &I) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000667 llvm_unreachable("UserOp1 should not exist at instruction selection time!");
Dan Gohman575fad32008-09-03 16:12:24 +0000668 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000669 void visitUserOp2(const Instruction &I) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000670 llvm_unreachable("UserOp2 should not exist at instruction selection time!");
Dan Gohman575fad32008-09-03 16:12:24 +0000671 }
Dan Gohman5b43aa02010-04-22 20:55:53 +0000672
Richard Sandiforde3827752013-08-16 10:55:47 +0000673 void processIntegerCallValue(const Instruction &I,
674 SDValue Value, bool IsSigned);
675
Bill Wendling0c0c57f2020-09-24 14:35:15 -0700676 void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
Evan Cheng6e822452010-04-28 23:08:54 +0000677
Craig Topperf49f6cf2020-04-10 18:29:02 -0700678 void emitInlineAsmError(const CallBase &Call, const Twine &Message);
Renato Golin38ed8022016-05-17 19:52:01 +0000679
Michael Gottesmane24f5342022-04-01 15:57:52 -0700680 /// An enum that states to emit func argument dbg value the kind of intrinsic
681 /// it originally had. This controls the internal behavior of
682 /// EmitFuncArgumentDbgValue.
683 enum class FuncArgumentDbgValueKind {
684 Value, // This was originally a llvm.dbg.value.
Michael Gottesmane24f5342022-04-01 15:57:52 -0700685 Declare, // This was originally a llvm.dbg.declare.
686 };
687
Adrian Prantla6175762017-07-28 21:27:35 +0000688 /// If V is an function argument then create corresponding DBG_VALUE machine
689 /// instruction for it now. At the end of instruction selection, they will be
690 /// inserted to the entry BB.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000691 bool EmitFuncArgumentDbgValue(const Value *V, DILocalVariable *Variable,
692 DIExpression *Expr, DILocation *DL,
Michael Gottesmane24f5342022-04-01 15:57:52 -0700693 FuncArgumentDbgValueKind Kind,
694 const SDValue &N);
Hans Wennborgb4db1422015-03-19 20:41:48 +0000695
696 /// Return the next block after MBB, or nullptr if there is none.
697 MachineBasicBlock *NextBlock(MachineBasicBlock *MBB);
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000698
699 /// Update the DAG and DAG builder with the relevant information after
700 /// a new root node has been created which could be a tail call.
701 void updateDAGForMaybeTailCall(SDValue MaybeTC);
Wolfgang Piebdfad9b22016-08-15 18:18:26 +0000702
703 /// Return the appropriate SDDbgValue based on N.
704 SDDbgValue *getDbgValue(SDValue N, DILocalVariable *Variable,
Adrian Prantla6175762017-07-28 21:27:35 +0000705 DIExpression *Expr, const DebugLoc &dl,
706 unsigned DbgSDNodeOrder);
Guillaume Chatelete386a012019-05-20 11:01:30 +0000707
cynecx8ec9fd42021-05-13 19:05:11 +0100708 SDValue lowerStartEH(SDValue Chain, const BasicBlock *EHPadBB,
709 MCSymbol *&BeginLabel);
710 SDValue lowerEndEH(SDValue Chain, const InvokeInst *II,
711 const BasicBlock *EHPadBB, MCSymbol *BeginLabel);
Dan Gohman575fad32008-09-03 16:12:24 +0000712};
713
Bjorn Petterssonecd09602019-02-12 22:11:20 +0000714/// This struct represents the registers (physical or virtual)
Sanjoy Das3936a972015-05-05 23:06:54 +0000715/// that a particular set of values is assigned, and the type information about
716/// the value. The most common situation is to represent one value at a time,
717/// but struct or array values are handled element-wise as multiple values. The
718/// splitting of aggregates is performed recursively, so that we never have
719/// aggregate-typed registers. The values at this point do not necessarily have
720/// legal types, so each value may require one or more registers of some legal
721/// type.
722///
723struct RegsForValue {
Sanjay Patel209b0f92017-03-02 20:48:08 +0000724 /// The value types of the values, which may not be legal, and
Sanjoy Das3936a972015-05-05 23:06:54 +0000725 /// may need be promoted or synthesized from one or more registers.
Sanjoy Das3936a972015-05-05 23:06:54 +0000726 SmallVector<EVT, 4> ValueVTs;
727
Sanjay Patel209b0f92017-03-02 20:48:08 +0000728 /// The value types of the registers. This is the same size as ValueVTs and it
729 /// records, for each value, what the type of the assigned register or
730 /// registers are. (Individual values are never synthesized from more than one
731 /// type of register.)
Sanjoy Das3936a972015-05-05 23:06:54 +0000732 ///
733 /// With virtual registers, the contents of RegVTs is redundant with TLI's
734 /// getRegisterType member function, however when with physical registers
735 /// it is necessary to have a separate record of the types.
Sanjoy Das3936a972015-05-05 23:06:54 +0000736 SmallVector<MVT, 4> RegVTs;
737
Sanjay Patel209b0f92017-03-02 20:48:08 +0000738 /// This list holds the registers assigned to the values.
Sanjoy Das3936a972015-05-05 23:06:54 +0000739 /// Each legal or promoted value requires one register, and each
740 /// expanded value requires multiple registers.
Craig Topper9d3ab1c2024-09-17 23:45:58 -0700741 SmallVector<Register, 4> Regs;
Sanjoy Das3936a972015-05-05 23:06:54 +0000742
Simon Dardis212cccb2017-06-09 14:37:08 +0000743 /// This list holds the number of registers for each value.
744 SmallVector<unsigned, 4> RegCount;
745
746 /// Records if this value needs to be treated in an ABI dependant manner,
747 /// different to normal type legalization.
Krzysztof Parzyszek864aaa22022-12-01 16:18:32 -0800748 std::optional<CallingConv::ID> CallConv;
Simon Dardis212cccb2017-06-09 14:37:08 +0000749
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000750 RegsForValue() = default;
Craig Topper9d3ab1c2024-09-17 23:45:58 -0700751 RegsForValue(const SmallVector<Register, 4> &regs, MVT regvt, EVT valuevt,
Krzysztof Parzyszek864aaa22022-12-01 16:18:32 -0800752 std::optional<CallingConv::ID> CC = std::nullopt);
Mehdi Amini56228da2015-07-09 01:57:34 +0000753 RegsForValue(LLVMContext &Context, const TargetLowering &TLI,
Craig Topper9d3ab1c2024-09-17 23:45:58 -0700754 const DataLayout &DL, Register Reg, Type *Ty,
Krzysztof Parzyszek864aaa22022-12-01 16:18:32 -0800755 std::optional<CallingConv::ID> CC);
Matt Arsenault81920b02018-07-28 13:25:19 +0000756
Kazu Hiratad66cbc52022-06-20 20:26:05 -0700757 bool isABIMangled() const { return CallConv.has_value(); }
Sanjoy Das3936a972015-05-05 23:06:54 +0000758
Sanjay Patel209b0f92017-03-02 20:48:08 +0000759 /// Add the specified values to this one.
Sanjoy Das3936a972015-05-05 23:06:54 +0000760 void append(const RegsForValue &RHS) {
761 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
762 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
763 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
Simon Dardis212cccb2017-06-09 14:37:08 +0000764 RegCount.push_back(RHS.Regs.size());
Sanjoy Das3936a972015-05-05 23:06:54 +0000765 }
766
Sanjay Patel209b0f92017-03-02 20:48:08 +0000767 /// Emit a series of CopyFromReg nodes that copies from this value and returns
768 /// the result as a ValueVTs value. This uses Chain/Flag as the input and
769 /// updates them for the output Chain/Flag. If the Flag pointer is NULL, no
770 /// flag is used.
Sanjoy Das3936a972015-05-05 23:06:54 +0000771 SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo,
Craig Topperb5f207e2023-04-02 19:45:36 -0700772 const SDLoc &dl, SDValue &Chain, SDValue *Glue,
Sanjoy Das3936a972015-05-05 23:06:54 +0000773 const Value *V = nullptr) const;
774
Sanjay Patel209b0f92017-03-02 20:48:08 +0000775 /// Emit a series of CopyToReg nodes that copies the specified value into the
776 /// registers specified by this object. This uses Chain/Flag as the input and
777 /// updates them for the output Chain/Flag. If the Flag pointer is nullptr, no
778 /// flag is used. If V is not nullptr, then it is used in printing better
779 /// diagnostic messages on error.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000780 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, const SDLoc &dl,
Craig Topperb5f207e2023-04-02 19:45:36 -0700781 SDValue &Chain, SDValue *Glue, const Value *V = nullptr,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000782 ISD::NodeType PreferredExtendType = ISD::ANY_EXTEND) const;
Sanjoy Das3936a972015-05-05 23:06:54 +0000783
Sanjay Patel209b0f92017-03-02 20:48:08 +0000784 /// Add this value to the specified inlineasm node operand list. This adds the
785 /// code marker, matching input operand index (if applicable), and includes
786 /// the number of values added into it.
Nick Desaulniers2fad6e62023-08-31 08:39:52 -0700787 void AddInlineAsmOperands(InlineAsm::Kind Code, bool HasMatching,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000788 unsigned MatchingIdx, const SDLoc &dl,
789 SelectionDAG &DAG, std::vector<SDValue> &Ops) const;
Bjorn Pettersson304877e2018-05-03 17:04:16 +0000790
791 /// Check if the total RegCount is greater than one.
792 bool occupiesMultipleRegs() const {
793 return std::accumulate(RegCount.begin(), RegCount.end(), 0) > 1;
794 }
Bjorn Pettersson27a841f2018-05-04 08:50:48 +0000795
796 /// Return a list of registers and their sizes.
Craig Topper9d3ab1c2024-09-17 23:45:58 -0700797 SmallVector<std::pair<Register, TypeSize>, 4> getRegsAndSizes() const;
Sanjoy Das3936a972015-05-05 23:06:54 +0000798};
799
Dan Gohman575fad32008-09-03 16:12:24 +0000800} // end namespace llvm
801
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000802#endif // LLVM_LIB_CODEGEN_SELECTIONDAG_SELECTIONDAGBUILDER_H