10b57cec5SDimitry Andric //===-- FunctionLoweringInfo.cpp ------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This implements routines for translating functions from LLVM IR into
100b57cec5SDimitry Andric // Machine IR.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/CodeGen/FunctionLoweringInfo.h"
155ffd83dbSDimitry Andric #include "llvm/ADT/APInt.h"
168bcb0991SDimitry Andric #include "llvm/Analysis/LegacyDivergenceAnalysis.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/Analysis.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/WasmEHFuncInfo.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/WinEHFuncInfo.h"
290b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
300b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
310b57cec5SDimitry Andric #include "llvm/IR/Function.h"
320b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
330b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
340b57cec5SDimitry Andric #include "llvm/IR/Module.h"
350b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
360b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
370b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
380b57cec5SDimitry Andric #include <algorithm>
390b57cec5SDimitry Andric using namespace llvm;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric #define DEBUG_TYPE "function-lowering-info"
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
440b57cec5SDimitry Andric /// PHI nodes or outside of the basic block that defines it, or used by a
450b57cec5SDimitry Andric /// switch or atomic instruction, which may expand to multiple basic blocks.
460b57cec5SDimitry Andric static bool isUsedOutsideOfDefiningBlock(const Instruction *I) {
470b57cec5SDimitry Andric   if (I->use_empty()) return false;
480b57cec5SDimitry Andric   if (isa<PHINode>(I)) return true;
490b57cec5SDimitry Andric   const BasicBlock *BB = I->getParent();
500b57cec5SDimitry Andric   for (const User *U : I->users())
510b57cec5SDimitry Andric     if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U))
520b57cec5SDimitry Andric       return true;
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   return false;
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric 
5781ad6265SDimitry Andric static ISD::NodeType getPreferredExtendForValue(const Instruction *I) {
580b57cec5SDimitry Andric   // For the users of the source value being used for compare instruction, if
590b57cec5SDimitry Andric   // the number of signed predicate is greater than unsigned predicate, we
600b57cec5SDimitry Andric   // prefer to use SIGN_EXTEND.
610b57cec5SDimitry Andric   //
620b57cec5SDimitry Andric   // With this optimization, we would be able to reduce some redundant sign or
630b57cec5SDimitry Andric   // zero extension instruction, and eventually more machine CSE opportunities
640b57cec5SDimitry Andric   // can be exposed.
650b57cec5SDimitry Andric   ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
660b57cec5SDimitry Andric   unsigned NumOfSigned = 0, NumOfUnsigned = 0;
6781ad6265SDimitry Andric   for (const User *U : I->users()) {
680b57cec5SDimitry Andric     if (const auto *CI = dyn_cast<CmpInst>(U)) {
690b57cec5SDimitry Andric       NumOfSigned += CI->isSigned();
700b57cec5SDimitry Andric       NumOfUnsigned += CI->isUnsigned();
710b57cec5SDimitry Andric     }
720b57cec5SDimitry Andric   }
730b57cec5SDimitry Andric   if (NumOfSigned > NumOfUnsigned)
740b57cec5SDimitry Andric     ExtendKind = ISD::SIGN_EXTEND;
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   return ExtendKind;
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
800b57cec5SDimitry Andric                                SelectionDAG *DAG) {
810b57cec5SDimitry Andric   Fn = &fn;
820b57cec5SDimitry Andric   MF = &mf;
830b57cec5SDimitry Andric   TLI = MF->getSubtarget().getTargetLowering();
840b57cec5SDimitry Andric   RegInfo = &MF->getRegInfo();
850b57cec5SDimitry Andric   const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
860b57cec5SDimitry Andric   DA = DAG->getDivergenceAnalysis();
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   // Check whether the function can return without sret-demotion.
890b57cec5SDimitry Andric   SmallVector<ISD::OutputArg, 4> Outs;
900b57cec5SDimitry Andric   CallingConv::ID CC = Fn->getCallingConv();
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   GetReturnInfo(CC, Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI,
930b57cec5SDimitry Andric                 mf.getDataLayout());
940b57cec5SDimitry Andric   CanLowerReturn =
950b57cec5SDimitry Andric       TLI->CanLowerReturn(CC, *MF, Fn->isVarArg(), Outs, Fn->getContext());
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   // If this personality uses funclets, we need to do a bit more work.
980b57cec5SDimitry Andric   DenseMap<const AllocaInst *, TinyPtrVector<int *>> CatchObjects;
990b57cec5SDimitry Andric   EHPersonality Personality = classifyEHPersonality(
1000b57cec5SDimitry Andric       Fn->hasPersonalityFn() ? Fn->getPersonalityFn() : nullptr);
1010b57cec5SDimitry Andric   if (isFuncletEHPersonality(Personality)) {
1020b57cec5SDimitry Andric     // Calculate state numbers if we haven't already.
1030b57cec5SDimitry Andric     WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo();
1040b57cec5SDimitry Andric     if (Personality == EHPersonality::MSVC_CXX)
1050b57cec5SDimitry Andric       calculateWinCXXEHStateNumbers(&fn, EHInfo);
1060b57cec5SDimitry Andric     else if (isAsynchronousEHPersonality(Personality))
1070b57cec5SDimitry Andric       calculateSEHStateNumbers(&fn, EHInfo);
1080b57cec5SDimitry Andric     else if (Personality == EHPersonality::CoreCLR)
1090b57cec5SDimitry Andric       calculateClrEHStateNumbers(&fn, EHInfo);
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric     // Map all BB references in the WinEH data to MBBs.
1120b57cec5SDimitry Andric     for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) {
1130b57cec5SDimitry Andric       for (WinEHHandlerType &H : TBME.HandlerArray) {
1140b57cec5SDimitry Andric         if (const AllocaInst *AI = H.CatchObj.Alloca)
1150b57cec5SDimitry Andric           CatchObjects.insert({AI, {}}).first->second.push_back(
1160b57cec5SDimitry Andric               &H.CatchObj.FrameIndex);
1170b57cec5SDimitry Andric         else
1180b57cec5SDimitry Andric           H.CatchObj.FrameIndex = INT_MAX;
1190b57cec5SDimitry Andric       }
1200b57cec5SDimitry Andric     }
1210b57cec5SDimitry Andric   }
1220b57cec5SDimitry Andric   if (Personality == EHPersonality::Wasm_CXX) {
1230b57cec5SDimitry Andric     WasmEHFuncInfo &EHInfo = *MF->getWasmEHFuncInfo();
1240b57cec5SDimitry Andric     calculateWasmEHInfo(&fn, EHInfo);
1250b57cec5SDimitry Andric   }
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric   // Initialize the mapping of values to registers.  This is only set up for
1280b57cec5SDimitry Andric   // instruction values that are used outside of the block that defines
1290b57cec5SDimitry Andric   // them.
1305ffd83dbSDimitry Andric   const Align StackAlign = TFI->getStackAlign();
1310b57cec5SDimitry Andric   for (const BasicBlock &BB : *Fn) {
1320b57cec5SDimitry Andric     for (const Instruction &I : BB) {
1330b57cec5SDimitry Andric       if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
1340b57cec5SDimitry Andric         Type *Ty = AI->getAllocatedType();
1355ffd83dbSDimitry Andric         Align TyPrefAlign = MF->getDataLayout().getPrefTypeAlign(Ty);
1365ffd83dbSDimitry Andric         // The "specified" alignment is the alignment written on the alloca,
1375ffd83dbSDimitry Andric         // or the preferred alignment of the type if none is specified.
1385ffd83dbSDimitry Andric         //
1395ffd83dbSDimitry Andric         // (Unspecified alignment on allocas will be going away soon.)
1405ffd83dbSDimitry Andric         Align SpecifiedAlign = AI->getAlign();
1415ffd83dbSDimitry Andric 
1425ffd83dbSDimitry Andric         // If the preferred alignment of the type is higher than the specified
1435ffd83dbSDimitry Andric         // alignment of the alloca, promote the alignment, as long as it doesn't
1445ffd83dbSDimitry Andric         // require realigning the stack.
1455ffd83dbSDimitry Andric         //
1465ffd83dbSDimitry Andric         // FIXME: Do we really want to second-guess the IR in isel?
1475ffd83dbSDimitry Andric         Align Alignment =
1485ffd83dbSDimitry Andric             std::max(std::min(TyPrefAlign, StackAlign), SpecifiedAlign);
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric         // Static allocas can be folded into the initial stack frame
1510b57cec5SDimitry Andric         // adjustment. For targets that don't realign the stack, don't
1520b57cec5SDimitry Andric         // do this if there is an extra alignment requirement.
1530b57cec5SDimitry Andric         if (AI->isStaticAlloca() &&
1545ffd83dbSDimitry Andric             (TFI->isStackRealignable() || (Alignment <= StackAlign))) {
1550b57cec5SDimitry Andric           const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize());
156480093f4SDimitry Andric           uint64_t TySize =
157480093f4SDimitry Andric               MF->getDataLayout().getTypeAllocSize(Ty).getKnownMinSize();
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric           TySize *= CUI->getZExtValue();   // Get total allocated size.
1600b57cec5SDimitry Andric           if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
1610b57cec5SDimitry Andric           int FrameIndex = INT_MAX;
1620b57cec5SDimitry Andric           auto Iter = CatchObjects.find(AI);
1630b57cec5SDimitry Andric           if (Iter != CatchObjects.end() && TLI->needsFixedCatchObjects()) {
1640b57cec5SDimitry Andric             FrameIndex = MF->getFrameInfo().CreateFixedObject(
1650b57cec5SDimitry Andric                 TySize, 0, /*IsImmutable=*/false, /*isAliased=*/true);
1665ffd83dbSDimitry Andric             MF->getFrameInfo().setObjectAlignment(FrameIndex, Alignment);
1670b57cec5SDimitry Andric           } else {
1685ffd83dbSDimitry Andric             FrameIndex = MF->getFrameInfo().CreateStackObject(TySize, Alignment,
1695ffd83dbSDimitry Andric                                                               false, AI);
1700b57cec5SDimitry Andric           }
1710b57cec5SDimitry Andric 
172480093f4SDimitry Andric           // Scalable vectors may need a special StackID to distinguish
173480093f4SDimitry Andric           // them from other (fixed size) stack objects.
1745ffd83dbSDimitry Andric           if (isa<ScalableVectorType>(Ty))
175480093f4SDimitry Andric             MF->getFrameInfo().setStackID(FrameIndex,
176480093f4SDimitry Andric                                           TFI->getStackIDForScalableVectors());
177480093f4SDimitry Andric 
1780b57cec5SDimitry Andric           StaticAllocaMap[AI] = FrameIndex;
1790b57cec5SDimitry Andric           // Update the catch handler information.
1800b57cec5SDimitry Andric           if (Iter != CatchObjects.end()) {
1810b57cec5SDimitry Andric             for (int *CatchObjPtr : Iter->second)
1820b57cec5SDimitry Andric               *CatchObjPtr = FrameIndex;
1830b57cec5SDimitry Andric           }
1840b57cec5SDimitry Andric         } else {
1850b57cec5SDimitry Andric           // FIXME: Overaligned static allocas should be grouped into
1860b57cec5SDimitry Andric           // a single dynamic allocation instead of using a separate
1870b57cec5SDimitry Andric           // stack allocation for each one.
1880b57cec5SDimitry Andric           // Inform the Frame Information that we have variable-sized objects.
1895ffd83dbSDimitry Andric           MF->getFrameInfo().CreateVariableSizedObject(
1905ffd83dbSDimitry Andric               Alignment <= StackAlign ? Align(1) : Alignment, AI);
1910b57cec5SDimitry Andric         }
192fe6060f1SDimitry Andric       } else if (auto *Call = dyn_cast<CallBase>(&I)) {
1930b57cec5SDimitry Andric         // Look for inline asm that clobbers the SP register.
1945ffd83dbSDimitry Andric         if (Call->isInlineAsm()) {
195e8d8bef9SDimitry Andric           Register SP = TLI->getStackPointerRegisterToSaveRestore();
1960b57cec5SDimitry Andric           const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
1970b57cec5SDimitry Andric           std::vector<TargetLowering::AsmOperandInfo> Ops =
1985ffd83dbSDimitry Andric               TLI->ParseConstraints(Fn->getParent()->getDataLayout(), TRI,
1995ffd83dbSDimitry Andric                                     *Call);
2000b57cec5SDimitry Andric           for (TargetLowering::AsmOperandInfo &Op : Ops) {
2010b57cec5SDimitry Andric             if (Op.Type == InlineAsm::isClobber) {
2020b57cec5SDimitry Andric               // Clobbers don't have SDValue operands, hence SDValue().
2030b57cec5SDimitry Andric               TLI->ComputeConstraintToUse(Op, SDValue(), DAG);
2040b57cec5SDimitry Andric               std::pair<unsigned, const TargetRegisterClass *> PhysReg =
2050b57cec5SDimitry Andric                   TLI->getRegForInlineAsmConstraint(TRI, Op.ConstraintCode,
2060b57cec5SDimitry Andric                                                     Op.ConstraintVT);
2070b57cec5SDimitry Andric               if (PhysReg.first == SP)
2080b57cec5SDimitry Andric                 MF->getFrameInfo().setHasOpaqueSPAdjustment(true);
2090b57cec5SDimitry Andric             }
2100b57cec5SDimitry Andric           }
2110b57cec5SDimitry Andric         }
2120b57cec5SDimitry Andric         // Look for calls to the @llvm.va_start intrinsic. We can omit some
2130b57cec5SDimitry Andric         // prologue boilerplate for variadic functions that don't examine their
2140b57cec5SDimitry Andric         // arguments.
2150b57cec5SDimitry Andric         if (const auto *II = dyn_cast<IntrinsicInst>(&I)) {
2160b57cec5SDimitry Andric           if (II->getIntrinsicID() == Intrinsic::vastart)
2170b57cec5SDimitry Andric             MF->getFrameInfo().setHasVAStart(true);
2180b57cec5SDimitry Andric         }
2190b57cec5SDimitry Andric 
220fe6060f1SDimitry Andric         // If we have a musttail call in a variadic function, we need to ensure
221fe6060f1SDimitry Andric         // we forward implicit register parameters.
2220b57cec5SDimitry Andric         if (const auto *CI = dyn_cast<CallInst>(&I)) {
2230b57cec5SDimitry Andric           if (CI->isMustTailCall() && Fn->isVarArg())
2240b57cec5SDimitry Andric             MF->getFrameInfo().setHasMustTailInVarArgFunc(true);
2250b57cec5SDimitry Andric         }
226fe6060f1SDimitry Andric       }
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric       // Mark values used outside their block as exported, by allocating
2290b57cec5SDimitry Andric       // a virtual register for them.
2300b57cec5SDimitry Andric       if (isUsedOutsideOfDefiningBlock(&I))
2310b57cec5SDimitry Andric         if (!isa<AllocaInst>(I) || !StaticAllocaMap.count(cast<AllocaInst>(&I)))
2320b57cec5SDimitry Andric           InitializeRegForValue(&I);
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric       // Decide the preferred extend type for a value.
2350b57cec5SDimitry Andric       PreferredExtendType[&I] = getPreferredExtendForValue(&I);
2360b57cec5SDimitry Andric     }
2370b57cec5SDimitry Andric   }
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric   // Create an initial MachineBasicBlock for each LLVM BasicBlock in F.  This
2400b57cec5SDimitry Andric   // also creates the initial PHI MachineInstrs, though none of the input
2410b57cec5SDimitry Andric   // operands are populated.
2420b57cec5SDimitry Andric   for (const BasicBlock &BB : *Fn) {
2430b57cec5SDimitry Andric     // Don't create MachineBasicBlocks for imaginary EH pad blocks. These blocks
2440b57cec5SDimitry Andric     // are really data, and no instructions can live here.
2450b57cec5SDimitry Andric     if (BB.isEHPad()) {
2460b57cec5SDimitry Andric       const Instruction *PadInst = BB.getFirstNonPHI();
2470b57cec5SDimitry Andric       // If this is a non-landingpad EH pad, mark this function as using
2480b57cec5SDimitry Andric       // funclets.
2490b57cec5SDimitry Andric       // FIXME: SEH catchpads do not create EH scope/funclets, so we could avoid
2500b57cec5SDimitry Andric       // setting this in such cases in order to improve frame layout.
2510b57cec5SDimitry Andric       if (!isa<LandingPadInst>(PadInst)) {
2520b57cec5SDimitry Andric         MF->setHasEHScopes(true);
2530b57cec5SDimitry Andric         MF->setHasEHFunclets(true);
2540b57cec5SDimitry Andric         MF->getFrameInfo().setHasOpaqueSPAdjustment(true);
2550b57cec5SDimitry Andric       }
2560b57cec5SDimitry Andric       if (isa<CatchSwitchInst>(PadInst)) {
2570b57cec5SDimitry Andric         assert(&*BB.begin() == PadInst &&
2580b57cec5SDimitry Andric                "WinEHPrepare failed to remove PHIs from imaginary BBs");
2590b57cec5SDimitry Andric         continue;
2600b57cec5SDimitry Andric       }
2610b57cec5SDimitry Andric       if (isa<FuncletPadInst>(PadInst))
2620b57cec5SDimitry Andric         assert(&*BB.begin() == PadInst && "WinEHPrepare failed to demote PHIs");
2630b57cec5SDimitry Andric     }
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric     MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(&BB);
2660b57cec5SDimitry Andric     MBBMap[&BB] = MBB;
2670b57cec5SDimitry Andric     MF->push_back(MBB);
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric     // Transfer the address-taken flag. This is necessary because there could
2700b57cec5SDimitry Andric     // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
2710b57cec5SDimitry Andric     // the first one should be marked.
2720b57cec5SDimitry Andric     if (BB.hasAddressTaken())
2730b57cec5SDimitry Andric       MBB->setHasAddressTaken();
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric     // Mark landing pad blocks.
2760b57cec5SDimitry Andric     if (BB.isEHPad())
2770b57cec5SDimitry Andric       MBB->setIsEHPad();
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric     // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
2800b57cec5SDimitry Andric     // appropriate.
2810b57cec5SDimitry Andric     for (const PHINode &PN : BB.phis()) {
2820b57cec5SDimitry Andric       if (PN.use_empty())
2830b57cec5SDimitry Andric         continue;
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric       // Skip empty types
2860b57cec5SDimitry Andric       if (PN.getType()->isEmptyTy())
2870b57cec5SDimitry Andric         continue;
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric       DebugLoc DL = PN.getDebugLoc();
2900b57cec5SDimitry Andric       unsigned PHIReg = ValueMap[&PN];
2910b57cec5SDimitry Andric       assert(PHIReg && "PHI node does not have an assigned virtual register!");
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric       SmallVector<EVT, 4> ValueVTs;
2940b57cec5SDimitry Andric       ComputeValueVTs(*TLI, MF->getDataLayout(), PN.getType(), ValueVTs);
2950b57cec5SDimitry Andric       for (EVT VT : ValueVTs) {
2960b57cec5SDimitry Andric         unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
2970b57cec5SDimitry Andric         const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
2980b57cec5SDimitry Andric         for (unsigned i = 0; i != NumRegisters; ++i)
2990b57cec5SDimitry Andric           BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
3000b57cec5SDimitry Andric         PHIReg += NumRegisters;
3010b57cec5SDimitry Andric       }
3020b57cec5SDimitry Andric     }
3030b57cec5SDimitry Andric   }
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric   if (isFuncletEHPersonality(Personality)) {
3060b57cec5SDimitry Andric     WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo();
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric     // Map all BB references in the WinEH data to MBBs.
3090b57cec5SDimitry Andric     for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) {
3100b57cec5SDimitry Andric       for (WinEHHandlerType &H : TBME.HandlerArray) {
3110b57cec5SDimitry Andric         if (H.Handler)
3120b57cec5SDimitry Andric           H.Handler = MBBMap[H.Handler.get<const BasicBlock *>()];
3130b57cec5SDimitry Andric       }
3140b57cec5SDimitry Andric     }
3150b57cec5SDimitry Andric     for (CxxUnwindMapEntry &UME : EHInfo.CxxUnwindMap)
3160b57cec5SDimitry Andric       if (UME.Cleanup)
3170b57cec5SDimitry Andric         UME.Cleanup = MBBMap[UME.Cleanup.get<const BasicBlock *>()];
3180b57cec5SDimitry Andric     for (SEHUnwindMapEntry &UME : EHInfo.SEHUnwindMap) {
3190b57cec5SDimitry Andric       const auto *BB = UME.Handler.get<const BasicBlock *>();
3200b57cec5SDimitry Andric       UME.Handler = MBBMap[BB];
3210b57cec5SDimitry Andric     }
3220b57cec5SDimitry Andric     for (ClrEHUnwindMapEntry &CME : EHInfo.ClrEHUnwindMap) {
3230b57cec5SDimitry Andric       const auto *BB = CME.Handler.get<const BasicBlock *>();
3240b57cec5SDimitry Andric       CME.Handler = MBBMap[BB];
3250b57cec5SDimitry Andric     }
3260b57cec5SDimitry Andric   }
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric   else if (Personality == EHPersonality::Wasm_CXX) {
3290b57cec5SDimitry Andric     WasmEHFuncInfo &EHInfo = *MF->getWasmEHFuncInfo();
330fe6060f1SDimitry Andric     // Map all BB references in the Wasm EH data to MBBs.
331fe6060f1SDimitry Andric     DenseMap<BBOrMBB, BBOrMBB> SrcToUnwindDest;
332fe6060f1SDimitry Andric     for (auto &KV : EHInfo.SrcToUnwindDest) {
3330b57cec5SDimitry Andric       const auto *Src = KV.first.get<const BasicBlock *>();
334fe6060f1SDimitry Andric       const auto *Dest = KV.second.get<const BasicBlock *>();
335fe6060f1SDimitry Andric       SrcToUnwindDest[MBBMap[Src]] = MBBMap[Dest];
3360b57cec5SDimitry Andric     }
337fe6060f1SDimitry Andric     EHInfo.SrcToUnwindDest = std::move(SrcToUnwindDest);
338fe6060f1SDimitry Andric     DenseMap<BBOrMBB, SmallPtrSet<BBOrMBB, 4>> UnwindDestToSrcs;
339fe6060f1SDimitry Andric     for (auto &KV : EHInfo.UnwindDestToSrcs) {
340fe6060f1SDimitry Andric       const auto *Dest = KV.first.get<const BasicBlock *>();
341fe6060f1SDimitry Andric       UnwindDestToSrcs[MBBMap[Dest]] = SmallPtrSet<BBOrMBB, 4>();
342fe6060f1SDimitry Andric       for (const auto P : KV.second)
343fe6060f1SDimitry Andric         UnwindDestToSrcs[MBBMap[Dest]].insert(
344fe6060f1SDimitry Andric             MBBMap[P.get<const BasicBlock *>()]);
345fe6060f1SDimitry Andric     }
346fe6060f1SDimitry Andric     EHInfo.UnwindDestToSrcs = std::move(UnwindDestToSrcs);
3470b57cec5SDimitry Andric   }
3480b57cec5SDimitry Andric }
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric /// clear - Clear out all the function-specific state. This returns this
3510b57cec5SDimitry Andric /// FunctionLoweringInfo to an empty state, ready to be used for a
3520b57cec5SDimitry Andric /// different function.
3530b57cec5SDimitry Andric void FunctionLoweringInfo::clear() {
3540b57cec5SDimitry Andric   MBBMap.clear();
3550b57cec5SDimitry Andric   ValueMap.clear();
3560b57cec5SDimitry Andric   VirtReg2Value.clear();
3570b57cec5SDimitry Andric   StaticAllocaMap.clear();
3580b57cec5SDimitry Andric   LiveOutRegInfo.clear();
3590b57cec5SDimitry Andric   VisitedBBs.clear();
3600b57cec5SDimitry Andric   ArgDbgValues.clear();
3610b57cec5SDimitry Andric   DescribedArgs.clear();
3620b57cec5SDimitry Andric   ByValArgFrameIndexMap.clear();
3630b57cec5SDimitry Andric   RegFixups.clear();
3640b57cec5SDimitry Andric   RegsWithFixups.clear();
3650b57cec5SDimitry Andric   StatepointStackSlots.clear();
366e8d8bef9SDimitry Andric   StatepointRelocationMaps.clear();
3670b57cec5SDimitry Andric   PreferredExtendType.clear();
3680b57cec5SDimitry Andric }
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric /// CreateReg - Allocate a single virtual register for the given type.
3715ffd83dbSDimitry Andric Register FunctionLoweringInfo::CreateReg(MVT VT, bool isDivergent) {
3720b57cec5SDimitry Andric   return RegInfo->createVirtualRegister(
3730b57cec5SDimitry Andric       MF->getSubtarget().getTargetLowering()->getRegClassFor(VT, isDivergent));
3740b57cec5SDimitry Andric }
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric /// CreateRegs - Allocate the appropriate number of virtual registers of
3770b57cec5SDimitry Andric /// the correctly promoted or expanded types.  Assign these registers
3780b57cec5SDimitry Andric /// consecutive vreg numbers and return the first assigned number.
3790b57cec5SDimitry Andric ///
3800b57cec5SDimitry Andric /// In the case that the given value has struct or array type, this function
3810b57cec5SDimitry Andric /// will assign registers for each member or element.
3820b57cec5SDimitry Andric ///
3835ffd83dbSDimitry Andric Register FunctionLoweringInfo::CreateRegs(Type *Ty, bool isDivergent) {
3840b57cec5SDimitry Andric   const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric   SmallVector<EVT, 4> ValueVTs;
3870b57cec5SDimitry Andric   ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs);
3880b57cec5SDimitry Andric 
3895ffd83dbSDimitry Andric   Register FirstReg;
3900b57cec5SDimitry Andric   for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
3910b57cec5SDimitry Andric     EVT ValueVT = ValueVTs[Value];
3920b57cec5SDimitry Andric     MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric     unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
3950b57cec5SDimitry Andric     for (unsigned i = 0; i != NumRegs; ++i) {
3965ffd83dbSDimitry Andric       Register R = CreateReg(RegisterVT, isDivergent);
3970b57cec5SDimitry Andric       if (!FirstReg) FirstReg = R;
3980b57cec5SDimitry Andric     }
3990b57cec5SDimitry Andric   }
4000b57cec5SDimitry Andric   return FirstReg;
4010b57cec5SDimitry Andric }
4020b57cec5SDimitry Andric 
4035ffd83dbSDimitry Andric Register FunctionLoweringInfo::CreateRegs(const Value *V) {
4045ffd83dbSDimitry Andric   return CreateRegs(V->getType(), DA && DA->isDivergent(V) &&
4055ffd83dbSDimitry Andric                     !TLI->requiresUniformRegister(*MF, V));
4060b57cec5SDimitry Andric }
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
4090b57cec5SDimitry Andric /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
4100b57cec5SDimitry Andric /// the register's LiveOutInfo is for a smaller bit width, it is extended to
4110b57cec5SDimitry Andric /// the larger bit width by zero extension. The bit width must be no smaller
4120b57cec5SDimitry Andric /// than the LiveOutInfo's existing bit width.
4130b57cec5SDimitry Andric const FunctionLoweringInfo::LiveOutInfo *
4145ffd83dbSDimitry Andric FunctionLoweringInfo::GetLiveOutRegInfo(Register Reg, unsigned BitWidth) {
4150b57cec5SDimitry Andric   if (!LiveOutRegInfo.inBounds(Reg))
4160b57cec5SDimitry Andric     return nullptr;
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric   LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
4190b57cec5SDimitry Andric   if (!LOI->IsValid)
4200b57cec5SDimitry Andric     return nullptr;
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric   if (BitWidth > LOI->Known.getBitWidth()) {
4230b57cec5SDimitry Andric     LOI->NumSignBits = 1;
4245ffd83dbSDimitry Andric     LOI->Known = LOI->Known.anyext(BitWidth);
4250b57cec5SDimitry Andric   }
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric   return LOI;
4280b57cec5SDimitry Andric }
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
4310b57cec5SDimitry Andric /// register based on the LiveOutInfo of its operands.
4320b57cec5SDimitry Andric void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
4330b57cec5SDimitry Andric   Type *Ty = PN->getType();
4340b57cec5SDimitry Andric   if (!Ty->isIntegerTy() || Ty->isVectorTy())
4350b57cec5SDimitry Andric     return;
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric   SmallVector<EVT, 1> ValueVTs;
4380b57cec5SDimitry Andric   ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs);
4390b57cec5SDimitry Andric   assert(ValueVTs.size() == 1 &&
4400b57cec5SDimitry Andric          "PHIs with non-vector integer types should have a single VT.");
4410b57cec5SDimitry Andric   EVT IntVT = ValueVTs[0];
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric   if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
4440b57cec5SDimitry Andric     return;
4450b57cec5SDimitry Andric   IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
4460b57cec5SDimitry Andric   unsigned BitWidth = IntVT.getSizeInBits();
4470b57cec5SDimitry Andric 
44881ad6265SDimitry Andric   auto It = ValueMap.find(PN);
44981ad6265SDimitry Andric   if (It == ValueMap.end())
4500b57cec5SDimitry Andric     return;
45181ad6265SDimitry Andric 
45281ad6265SDimitry Andric   Register DestReg = It->second;
45381ad6265SDimitry Andric   if (DestReg == 0)
45481ad6265SDimitry Andric     return
45581ad6265SDimitry Andric   assert(Register::isVirtualRegister(DestReg) && "Expected a virtual reg");
4560b57cec5SDimitry Andric   LiveOutRegInfo.grow(DestReg);
4570b57cec5SDimitry Andric   LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   Value *V = PN->getIncomingValue(0);
4600b57cec5SDimitry Andric   if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
4610b57cec5SDimitry Andric     DestLOI.NumSignBits = 1;
4620b57cec5SDimitry Andric     DestLOI.Known = KnownBits(BitWidth);
4630b57cec5SDimitry Andric     return;
4640b57cec5SDimitry Andric   }
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
46781ad6265SDimitry Andric     APInt Val;
46881ad6265SDimitry Andric     if (TLI->signExtendConstant(CI))
46981ad6265SDimitry Andric       Val = CI->getValue().sext(BitWidth);
47081ad6265SDimitry Andric     else
47181ad6265SDimitry Andric       Val = CI->getValue().zext(BitWidth);
4720b57cec5SDimitry Andric     DestLOI.NumSignBits = Val.getNumSignBits();
473e8d8bef9SDimitry Andric     DestLOI.Known = KnownBits::makeConstant(Val);
4740b57cec5SDimitry Andric   } else {
4750b57cec5SDimitry Andric     assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
4760b57cec5SDimitry Andric                                 "CopyToReg node was created.");
4775ffd83dbSDimitry Andric     Register SrcReg = ValueMap[V];
4788bcb0991SDimitry Andric     if (!Register::isVirtualRegister(SrcReg)) {
4790b57cec5SDimitry Andric       DestLOI.IsValid = false;
4800b57cec5SDimitry Andric       return;
4810b57cec5SDimitry Andric     }
4820b57cec5SDimitry Andric     const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
4830b57cec5SDimitry Andric     if (!SrcLOI) {
4840b57cec5SDimitry Andric       DestLOI.IsValid = false;
4850b57cec5SDimitry Andric       return;
4860b57cec5SDimitry Andric     }
4870b57cec5SDimitry Andric     DestLOI = *SrcLOI;
4880b57cec5SDimitry Andric   }
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric   assert(DestLOI.Known.Zero.getBitWidth() == BitWidth &&
4910b57cec5SDimitry Andric          DestLOI.Known.One.getBitWidth() == BitWidth &&
4920b57cec5SDimitry Andric          "Masks should have the same bit width as the type.");
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric   for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
4950b57cec5SDimitry Andric     Value *V = PN->getIncomingValue(i);
4960b57cec5SDimitry Andric     if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
4970b57cec5SDimitry Andric       DestLOI.NumSignBits = 1;
4980b57cec5SDimitry Andric       DestLOI.Known = KnownBits(BitWidth);
4990b57cec5SDimitry Andric       return;
5000b57cec5SDimitry Andric     }
5010b57cec5SDimitry Andric 
5020b57cec5SDimitry Andric     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
50381ad6265SDimitry Andric       APInt Val;
50481ad6265SDimitry Andric       if (TLI->signExtendConstant(CI))
50581ad6265SDimitry Andric         Val = CI->getValue().sext(BitWidth);
50681ad6265SDimitry Andric       else
50781ad6265SDimitry Andric         Val = CI->getValue().zext(BitWidth);
5080b57cec5SDimitry Andric       DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
5090b57cec5SDimitry Andric       DestLOI.Known.Zero &= ~Val;
5100b57cec5SDimitry Andric       DestLOI.Known.One &= Val;
5110b57cec5SDimitry Andric       continue;
5120b57cec5SDimitry Andric     }
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric     assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
5150b57cec5SDimitry Andric                                 "its CopyToReg node was created.");
5165ffd83dbSDimitry Andric     Register SrcReg = ValueMap[V];
5175ffd83dbSDimitry Andric     if (!SrcReg.isVirtual()) {
5180b57cec5SDimitry Andric       DestLOI.IsValid = false;
5190b57cec5SDimitry Andric       return;
5200b57cec5SDimitry Andric     }
5210b57cec5SDimitry Andric     const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
5220b57cec5SDimitry Andric     if (!SrcLOI) {
5230b57cec5SDimitry Andric       DestLOI.IsValid = false;
5240b57cec5SDimitry Andric       return;
5250b57cec5SDimitry Andric     }
5260b57cec5SDimitry Andric     DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
527e8d8bef9SDimitry Andric     DestLOI.Known = KnownBits::commonBits(DestLOI.Known, SrcLOI->Known);
5280b57cec5SDimitry Andric   }
5290b57cec5SDimitry Andric }
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric /// setArgumentFrameIndex - Record frame index for the byval
5320b57cec5SDimitry Andric /// argument. This overrides previous frame index entry for this argument,
5330b57cec5SDimitry Andric /// if any.
5340b57cec5SDimitry Andric void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
5350b57cec5SDimitry Andric                                                  int FI) {
5360b57cec5SDimitry Andric   ByValArgFrameIndexMap[A] = FI;
5370b57cec5SDimitry Andric }
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric /// getArgumentFrameIndex - Get frame index for the byval argument.
5400b57cec5SDimitry Andric /// If the argument does not have any assigned frame index then 0 is
5410b57cec5SDimitry Andric /// returned.
5420b57cec5SDimitry Andric int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
5430b57cec5SDimitry Andric   auto I = ByValArgFrameIndexMap.find(A);
5440b57cec5SDimitry Andric   if (I != ByValArgFrameIndexMap.end())
5450b57cec5SDimitry Andric     return I->second;
5460b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
5470b57cec5SDimitry Andric   return INT_MAX;
5480b57cec5SDimitry Andric }
5490b57cec5SDimitry Andric 
5505ffd83dbSDimitry Andric Register FunctionLoweringInfo::getCatchPadExceptionPointerVReg(
5510b57cec5SDimitry Andric     const Value *CPI, const TargetRegisterClass *RC) {
5520b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF->getRegInfo();
5530b57cec5SDimitry Andric   auto I = CatchPadExceptionPointers.insert({CPI, 0});
5545ffd83dbSDimitry Andric   Register &VReg = I.first->second;
5550b57cec5SDimitry Andric   if (I.second)
5560b57cec5SDimitry Andric     VReg = MRI.createVirtualRegister(RC);
5570b57cec5SDimitry Andric   assert(VReg && "null vreg in exception pointer table!");
5580b57cec5SDimitry Andric   return VReg;
5590b57cec5SDimitry Andric }
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric const Value *
5625ffd83dbSDimitry Andric FunctionLoweringInfo::getValueFromVirtualReg(Register Vreg) {
5630b57cec5SDimitry Andric   if (VirtReg2Value.empty()) {
5640b57cec5SDimitry Andric     SmallVector<EVT, 4> ValueVTs;
5650b57cec5SDimitry Andric     for (auto &P : ValueMap) {
5660b57cec5SDimitry Andric       ValueVTs.clear();
5670b57cec5SDimitry Andric       ComputeValueVTs(*TLI, Fn->getParent()->getDataLayout(),
5680b57cec5SDimitry Andric                       P.first->getType(), ValueVTs);
5690b57cec5SDimitry Andric       unsigned Reg = P.second;
5700b57cec5SDimitry Andric       for (EVT VT : ValueVTs) {
5710b57cec5SDimitry Andric         unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
5720b57cec5SDimitry Andric         for (unsigned i = 0, e = NumRegisters; i != e; ++i)
5730b57cec5SDimitry Andric           VirtReg2Value[Reg++] = P.first;
5740b57cec5SDimitry Andric       }
5750b57cec5SDimitry Andric     }
5760b57cec5SDimitry Andric   }
5770b57cec5SDimitry Andric   return VirtReg2Value.lookup(Vreg);
5780b57cec5SDimitry Andric }
579